Array mixed with integers and strings?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff

    Array mixed with integers and strings?


    ....still new to vb.net 2005

    I understand the concept of arrays, and have used them in other languages,
    but was hoping that someone could get me started with something.

    I have a fairly long list of values that start in textboxes and listboxes
    that are both integers and strings. I need to store them in an array to pass
    to subs and stored procedures used in a database. Can I mix the integers and
    strings (both of varying lengths) in the same array? If so, could someone
    provide a simple example that I could build upon or point me in the right
    direction?

    Thanks

    Jeff


    --
    Posted via a free Usenet account from http://www.teranews.com

  • diAb0Lo

    #2
    Re: Array mixed with integers and strings?

    On 18 mayo, 09:44, "Jeff" <n...@nothingX. comwrote:
    ...still new to vb.net 2005
    >
    I understand the concept of arrays, and have used them in other languages,
    but was hoping that someone could get me started with something.
    >
    I have a fairly long list of values that start in textboxes and listboxes
    that are both integers and strings. I need to store them in an array to pass
    to subs and stored procedures used in a database. Can I mix the integers and
    strings (both of varying lengths) in the same array? If so, could someone
    provide a simple example that I could build upon or point me in the right
    direction?
    >
    Thanks
    >
    Jeff
    >
    --
    Posted via a free Usenet account fromhttp://www.teranews.co m
    Hi there,

    You can define the array as String, Store everything as String and do
    the convert to Int for all the items in the array. If you get an error
    trying to convert, it means that is a String. For example:

    Dim myArray(50) as String
    Dim tmpConv as Integer

    On Error Goto errStrConv
    for i = 1 to 50
    tmpConv = CInt(myArray(i) )
    ' if passyu know that is a integer
    Exit Sub
    errIntConv:
    ' here you know is a String
    next

    Or

    Dim myArray(50) as String
    Dim tmpConv as Integer

    for i = 1 to 50
    Try
    tmpConv = CInt(myArray(i) )
    ' if passyu know that is a integer
    Catch ex As Exception
    ' here you know is a String
    End Try
    next

    Cheers

    Comment

    • Robin Tucker

      #3
      Re: Array mixed with integers and strings?


      Hi Jeff,


      You could use a list of objects to do this. On a design level, if the two
      sets of values are distinct (i.e. your integers refer to one thing, your
      strings to another) then I would separate them out into two lists. I
      wouldn't be too comfortable lugging lists of Object around an application.
      It's got unhandled exception written all over it ;). Although this was the
      way with .NET 1.1, generics make it pointless. I suppose if you are
      disciplined you can get away with it. Alternatively you could store them
      all as string, parsing out the values into integers if needed. In the case
      of object:

      Dim myList As New List(Of Object)

      myList.Add ( 1 )
      myList.Add ( "One" )

      For Each theObject As Object In myList

      If TypeOf (theObject) Is Integer Then

      ' It's an integer

      Dim theInteger = DirectCast(theO bject, Integer)

      ElseIf TypeOf (theObject) Is String Then

      ' It's a string

      Dim theString As String = DirectCast(theO bject, String)

      Else

      Throw New InvalidOperatio nException ( "Object type not supported" )

      End If

      Next




      and for strings:



      Dim myList As New List(Of String)

      myList.Add ( 1.ToString )
      myList.Add ( "one" )

      For Each theString As String In myList

      Dim theInteger As Integer

      If Integer.TryPars e ( theString, theInteger ) Then
      ' It's an integer string
      Else
      ' It's a string
      End

      Robin


      "Jeff" <none@nothingX. comwrote in message
      news:464da17c$0 $16403$88260bb3 @free.teranews. com...
      >
      ...still new to vb.net 2005
      >
      I understand the concept of arrays, and have used them in other languages,
      but was hoping that someone could get me started with something.
      >
      I have a fairly long list of values that start in textboxes and listboxes
      that are both integers and strings. I need to store them in an array to
      pass to subs and stored procedures used in a database. Can I mix the
      integers and strings (both of varying lengths) in the same array? If so,
      could someone provide a simple example that I could build upon or point me
      in the right direction?
      >
      Thanks
      >
      Jeff
      >
      >
      --
      Posted via a free Usenet account from http://www.teranews.com
      >



      Comment

      • Miro

        #4
        Re: Array mixed with integers and strings?

        I tried this too jeff when i first started in vb.net

        something like
        Array := { "Hello", 2 }

        It cant be done easily...

        so the solution I used, is I created a Class and the created 2 properties of
        it.
        1 a string and the other a numeric
        Then I arrayified the Class...and worked like a charm.

        Another solution I think will work is to create a variable that is a
        datagrid, and save the data in there like data lines.

        Miro

        "Jeff" <none@nothingX. comwrote in message
        news:464da17c$0 $16403$88260bb3 @free.teranews. com...
        >
        ...still new to vb.net 2005
        >
        I understand the concept of arrays, and have used them in other languages,
        but was hoping that someone could get me started with something.
        >
        I have a fairly long list of values that start in textboxes and listboxes
        that are both integers and strings. I need to store them in an array to
        pass to subs and stored procedures used in a database. Can I mix the
        integers and strings (both of varying lengths) in the same array? If so,
        could someone provide a simple example that I could build upon or point me
        in the right direction?
        >
        Thanks
        >
        Jeff
        >
        >
        --
        Posted via a free Usenet account from http://www.teranews.com
        >

        Comment

        Working...