הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Monday, March 19, 2007 6:49 PM
I'm pretty new to VB.Net and from what i understand arraylists are the same thing as vectors. Basically, Dynamic arrays. Now, here is my situation. I'm working with databases, and i temporarily store my information in arrays to do things like duplication checking or prepping for inserting into the database. I really like the idea of arraylists, because they're so dynamic. I'm going to be dealing with tens of thousands of records and i can't be redim-ing arrays to massive sizes that might never be filled all the way. (i tried it that way, and my program would use about 200MB of RAM).
maybe i'm not fully understanding how to insert things into the array though. i can do a simple
myArray.Add("Hello") and it will work. But when i try to pull information from a datareader like this:
While x < dr.FieldCount
drDataTypes(x, 1).Add(dr.GetDataTypeName(x).ToString)
drDataTypes(x, 2).Add(dr.GetName(x).ToString)
x += 1
End While
i get a NullReferenceException Error.
If i try this with just a normal array of type string, it works fine. I've looked for a howto or tutorial on how to use arraylists with more than one dimension, but i haven't really found anything. The reason i'm wanting to use arraylists, is because i'm also going to be using them to store records from a database, clean the records(look for duplication), then insert them into a clean table.
i'm also not understanding how to do comparisons. for instance i try to set an if statement like this:
If dupRecord(ckrow, fldnms.IndexOf(fldnms, "MOD_DT")) > dupRecord(row, fldnms.IndexOf(fldnms, "MOD_DT")) Then 'checks modification dates dupRecord(ckrow, 1).RemoveAt(ckrow)'delete the record from the arraylist Else dupRecord(row, 1).RemoveAt(row) 'delete the record from the arraylist end if
i get a syntax error of, "Operator '>' is not defined for types 'system.collections.arraylist' and 'system.collections.arraylist'" so how am i supposed to see which date is newer, if i can't use < or >? or am i going about this all wrong?
All replies (5)
Monday, March 19, 2007 10:32 PM ✅Answered | 2 votes
Yes, you need exactly what you have.
Unfortunately, when you do:
dim drdatatypes(,) as arraylist
you are saying, "create a multi-dimensional array of references to arraylists". Each of the references do not correspond to a real array list - that is, they are "null references."
you then need to go through each element and create an array list for each element of the data; that's what the loop does.
Unfortunately, there's no way to do what you like without creating static arrays of the object that you want.
Here, you are creating a 2 dimensional array list (ie, a 2 dimensional array of a 1 dimensional array list).
Alternatively, if you know that you only have two definitions for each data type, can you create a structure of it, and use just a single list?
Structure ElementType
public name as String
public type as String
public sub new(name as string, type as string)
Me.name = name
Me.type = type
end sub
End Structure
Dim datatypes as new List(Of ElementType)
this way, you have a single list of structures. You can then do:
datatypes.Add(new ElementType("name", "type"))
etc. I hope the examples help? Let me know if you have any more questions!
Monday, March 19, 2007 9:08 PM
Can you show how you defined drDataTypes?
Chances are you'll need to new each element in your multi-dimensional array. So for example, you will need to do:
for x = 0 to [arraydim]
for y = 0 to [arraydim]
drDataTypes(x,y) = new ArrayList()
next
next
did you do something similar to that?
Monday, March 19, 2007 9:50 PM
no, i didn't have anything like that, i'm a total noob when it comes to this.
the way i had it declared was
dim drdatatypes (,) as arraylist
so, you're saying i'd need to do something like this
for x = 0 to totalcolumns
for y = 0 to 2 'there are only two definitions for each datatype (name and type)
drdatatype(x,y) = new arraylist()
next
next
so, this is creating a new list for each element in the multidimensional array? how is that dynamic?
is there a way that i can just declare something and then add and remove from it as i please? with out having to do all sorts of crazy loops? my program is already slow because of them :(
Wednesday, November 11, 2009 5:18 AM
hi, so, how can i access that multidimensional arraylist?
for example, i have created:
datatypes.Add(new ElementType("name1","type1"))
datatypes.Add(new ElementType("name2","type2"))
how can i get the value of "name2" and "type2".
thanks,
best regards,
rycopiero
Tuesday, March 29, 2011 2:46 PM | 1 vote
Structure FriendsStruct
Public Name As String
Public Address As String
Public Phone As String
Public Sub New(ByVal name As String, ByVal address As String, ByVal phone As String)
Me.Name = name
Me.Address = address
Me.Phone = phone
End Sub
End Structure
Dim newFriend As New List(Of FriendsStruct)
newFriend.Add(New FriendsStruct("yudha", "indonesia", "10809832091"))
newFriend.Add(New FriendsStruct("alex", "usa", "10809832092"))
newFriend.Add(New FriendsStruct("bill", "philipina", "10809832093"))
Dim friendsCollection As ICollection = newFriend
For Each s As FriendsStruct In friendsCollection
Debug.Print(s.Name & Space(1) & s.Address & Space(1) & s.Phone)
Next
'outputs:
'yudha indonesia 10809832091
'alex usa 10809832092
'bill philipina 10809832093