Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a variable

 dim list as string() = new string() {"list1", "list2"}

how can I add to this while looping through a recordset??

 

Cheers

 

Simon

Posted

        Dim list As String() = New String() {"list1", "list2"}
       Dim dt As DataTable
       Dim iLp As Integer


       'presumes table is filled before this
       For iLp = 0 To dt.Rows.Count - 1
           ReDim Preserve list(list.Length)
           list(list.Length - 1) = dt.Rows(iLp).Item(0)
       Next iLp

 

BTW, why are you using a recordset in .NET?

Here's what I'm up to.
  • Leaders
Posted
        Dim list As String() = New String() {"list1", "list2"}
       Dim dt As DataTable
       Dim iLp As Integer 

       'presumes table is filled before this
       For iLp = 0 To dt.Rows.Count - 1
           ReDim Preserve list(list.Length)
           list(list.Length - 1) = dt.Rows(iLp).Item(0)
       Next iLp

 

BTW, why are you using a recordset in .NET?

 

This code ReDims the array for each item to be added. That means that for each item you add, VB needs to allocate a new array and copy the contents of the old array to the new one. If you have 20 items to add, that means allocating and copying 20 items.

 

I'm not sure if this is 100% right, but it using an idea like this can save the gc and ram some trouble.

        Dim list As String() = New String() {"list1", "list2"}
       Dim dt As DataTable

       'Fill your table at some point

       'Resize the array and get the insertion point (Offset) where the new items will go
       Dim Offset As Integer = list.Length
       ReDim Preserve list(list.Length + dt.Rows.Count - 1)

       'Fill the array
       For iLp As Integer = 0 To dt.Rows.Count - 1
           list(Offset + iLp) = dt.Rows(iLp).Item(0)
       Next iLp

[sIGPIC]e[/sIGPIC]
Posted

Or, you can just use an ArrayList ;)

 

  Dim list As String()
  Dim dt As DataTable

  With New ArrayList(New String() {"list1", "list2"})
      For Each row As DataRow In dt.Rows()
          .Add(row(0))
      Next
      list = DirectCast(.ToArray(GetType(String)), String())
 End With

-=splice=-

It's not my fault I'm a Genius!

  • Leaders
Posted
Array lists are useful, but they aren't strongly typed. All the casting involved give me a headache. I find myself implementing my own collection classes often. It is simple enough to do, and can save you a lot of DirectCasts.
[sIGPIC]e[/sIGPIC]
Posted

::cough:: C# ::cough:: (string[])x as opposed to DirectCast(x, String()) :) You'd think they'd have made it a little more straight forward in VB...

 

Or better yet, generic collections in .Net 2.0

;)

"Who is John Galt?"

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...