Question arraylist of structure within array of structure

janus85_ren

Member
Joined
May 9, 2010
Messages
24
Programming Experience
1-3
I want to make a structure within a structure. Basically it will appear like this:

Structure ID
dim CardType as string
dim CardCode as string
dim ExpireDate as Datetime
End Structure

Structure Citizen
dim Name as string
dim Ethnic as string
dim Occupation as string
dim Identity as ID
End Structure

So every Citizen can have several IDs like passport, Social security card, driver's license and registrations, visas, etc. I want to have the instance of the Citizen as array and ID as an arraylist. So basically I'll have an array of citizens that contains an arraylist of ID. I want ID as an arraylist so that I can use the method .remove. Any help? thanks
 
Don't use the ArrayList class at all beyond .NET 1.1. From .NET 2.0, stick to generic collections, in this case a List(Of ID). It behaves pretty much just like an ArrayList but it is type-safe. That means that you can't add anything but ID objects to a List(Of ID), where an ArrayList will accept anything, and each item is returned as type ID, rather than an Object reference. That also means that the List avoids boxing of value types.

You should also be using properties rather than public fields to expose your data. I won't go into why because that information can be found in many places.

Also, while you can use structures, you might want to consider using classes instead. You may end up with issues if you don't fully understand the implications of using value types.

Here's a sample of what your final code might look like:
VB.NET:
Public Class ID

    Private _cardType As String

    Public Property CardType() As String
        Get
            Return Me._cardType
        End Get
        Set(ByVal value As String)
            Me._cardType = value
        End Set
    End Property

    '...

End Class


Public Class Citizen

    Private _ids As New List(Of ID)

    Public ReadOnly Property IDs() As List(Of ID)
        Get
            Return Me._ids
        End Get
    End Property

    '...

End Class
 
Oh, this is new. So Arraylist should be avoided if possible. OK, I have no problem with that. One more thing. I choose structure over class because I don't have any routines embedded in the data structure. their purpose is solely to store data. but maybe usage of class would provide more features should there be changes in the future.
I'm still having trouble instantiating the list. Could you please help me with the variable assignment too? this is my first time using these kind of datatype since I'm migrating recently from VB 6 to .Net. thanks
 
The code I posted previously shows you how to declare the field, the property and create the object. This sort of property gets used all the time throughout the Framework, e.g. the Controls collection of a form, the Items collection of a Listbox or ComboBox, then Tables collection of a DataSet, the Rows and Columns collection of a DataTable. To use it in your case it would be something like:
VB.NET:
myCitizen.IDs.Add(newID)
The reason I suggested that you use classes instead of structures is because of the different behaviour of value types (structures) and reference types (classes). With classes you could do this:
VB.NET:
myCitizenArray(citizenIndex).IDs(idIndex).CardType = cardType
whereas with structures you would have to do this:
VB.NET:
Dim myCitizen = myCitizenArray(citizenIndex)
Dim myID = myCitizen.IDs(idIndex)

myID.CardType = cardType
myCitizen.IDs(idIndex) = myID
myCitizenArray(citizenIndex) = myCitizen
Value types have there place and are superior to reference types in certain circumstances, but working with multi-level object graphs is not one of them.
 
I see. And one more thing. I'd like to know if I can still use the List.remove method in this case. The kind of remove I want to use is like in normal ListOf :
List.add("temp")
list.Remove("Temp")
Is this clear enough? So I just specify the item within the ListOf containing "Temp" and remove it. So this way, I can save the time of looping the entire content of the ListOf in order to find the "Temp" item before removing it. This is the only reason I want the instance of ID to be an Arraylist / ListOf or any other collection that can remove item without having to loop inside first.
So in my case, lets just say I want to remove the ID where the identity type is passport. what I want to do is something like:
VB.NET:
MyCitizen(i).ListOfID.Remove("Passport")
thanks.
 
If IDs is a List then you can use any members of the List class on it... because it's a List. A List is a List. Look at the first code snippet in my previous post, for instance.
 
Lets say for Citizen "Albert", I have a list of IDs containing :
1. Passport
2. Driver's License
3. Social Security

Roughly the List will look like this:
List
VB.NET:
cardType="Passport"
CardNumber="Passport-123456"

cardType="Drivers license"
CardNumber="Drive-23456"

cardType="Social Security"
CardNumber="Social-123456"

How can I remove the "Driver's license" without having to search the index?(in this case, driver's license occupy the second spot in ID List)
Do I need to define my own Remove method?
how can I tell the program to remove item in the list whose cardtype is "Drivers license" just like List.remove("Drivers License")?
thanks
 
You can't. The List class doesn't work like that. You could create your own custom class derived from the generic Collection class and add that functionality yourself. You could also use a keyed collection instead, like a Dictionary.
 
Dictionary ? another collection in VB.Net? apparently VB 6.0 is very different from .Net. But you do get what I'm trying to do right? Is there an object, method, collection or whatever in VB.net that have the remove capability like I mentioned above. So I can really save up the looping time. Because there's a process that will require adding and deleting list of ID. And also I'm having trouble instantiating the Citizen class using dynamic array. Could you please help me with this issue? thanks
 
Is there an object, method, collection or whatever in VB.net that have the remove capability like I mentioned above.
Um, yes: the Dictionary class.
I'm having trouble instantiating the Citizen class using dynamic array.
Could you perhaps narrow down what the issue might be? Instantiating classes has nothing whatsoever to do with arrays. Also, if you don't know how many objects you want to store or that number varies then you shouldn't be using an array in the first place. You should be using a List. That's exactly what they are for: to provide array-like behaviour but be able to grow and shrink dynamically.
 
In this case it does sound like you should be using a collection, e.g. generic List, rather than an array. On the subject of arrays though, you will need to use them in various situations so I suggest that you do some reading on the topic.

Arrays in VB .NET
 
This dictionary class is quite good. but does this object have index number? like in arraylist you can remove at certain index like Arraylist.Remove(1). Can the same be done with Dictionary? I searched the web and some suggested to use OrderedDictionary, a specialized collection.
 
Why does the order matter? You can get the second key for example dict.Keys(1), but generally with dictionaries only the key index matter, not position index.
 
Back
Top