lidds Posted May 25, 2005 Posted May 25, 2005 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 Quote
Machaira Posted May 25, 2005 Posted May 25, 2005 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? Quote Here's what I'm up to.
lidds Posted May 25, 2005 Author Posted May 25, 2005 Sorry got terminoligy mixed up, have just been doing asp Thanks for your help Simon Quote
Leaders snarfblam Posted May 25, 2005 Leaders Posted May 25, 2005 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 Quote [sIGPIC]e[/sIGPIC]
Machaira Posted May 25, 2005 Posted May 25, 2005 Yeah, that's more efficient and clean. That's what I get for coding off the top of my head. :) Quote Here's what I'm up to.
splice Posted May 27, 2005 Posted May 27, 2005 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 Quote -=splice=- It's not my fault I'm a Genius!
Leaders snarfblam Posted May 27, 2005 Leaders Posted May 27, 2005 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. Quote [sIGPIC]e[/sIGPIC]
IngisKahn Posted May 27, 2005 Posted May 27, 2005 ::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 ;) Quote "Who is John Galt?"
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.