Question Working with structures definintions, instances, looping etc..,

robtyketto

Member
Joined
Jul 23, 2009
Messages
23
Programming Experience
Beginner
Greetings,

I would like to use structures within my app to group/relate items that are of different data types i.e. string, integer, picture box etc..,

I have the knowledge to create a structure but not to loop through the instances of a structure.

As I have created instances of the structure in my example code (see link)

Im confused how to reference them as I only know how to declare a instance of the structure with a different name.

I believe you cannot use an array to stores instances of a structure.

How can i reference instances of the structure via the index value in a for each loop for an example?

Is it possible to store a collection of instances under one name and reference via index?

Also as structures can use methods, properties etc.., were they used in the past prior to classes being implemented in visual basic?

Code link:
VB.NET pastebin - collaborative debugging tool

Any feedback would greatly be appreciated.

Thanks
Rob
 
You can create an UDT array and I'll write ya up an example. I do have one question about what you mean about including a picturebox in the UDT though? That of course is a control, not a datatype. Do you want to perhaps include a filename to point to a picture? Also have you given consideration to using a dataset/datatable instead of a UDT? A UDT has less overhead but a datatable is so much easier to load, export, filter, call records & fields, has more validation etc...
 
You can create an UDT array and I'll write ya up an example. I do have one question about what you mean about including a picturebox in the UDT though? That of course is a control, not a datatype. Do you want to perhaps include a filename to point to a picture? Also have you given consideration to using a dataset/datatable instead of a UDT? A UDT has less overhead but a datatable is so much easier to load, export, filter, call records & fields, has more validation etc...

Thanks for the reply.

Stupidly the example I gave was not what I really wanted it for.

I'm creating a dice game (1-6 sided die) and some of the information I thought could be grouped for a single die would be the following:

DiceImage = Assigned to a picturebox control, which would then be assigned with a random Image chosen from an Imagelist (1-6, Index O image would be the form icon)

DiceValue = I intend to use this to assign the random number generated as the dice value i.e. 6 spots = 6, 3 spots = 3 etc..,

DiceHeld = Boolean if the dice value is to be kept, else it can be rolled again.

I have used dataset/datatable for a simple cd database I wrote and now assume since I revealed it would be for a dice game (only 6 instances would exist) this method would perhaps be overkill?
 
VB.NET:
Structure MyPerson
        Public Name As String
        Public Age As Integer
End Structure 'MyStruct 

Dim m_Persons() As MyPerson


Private Sub LoadUdtArray

    'Set the size of UDT array for 50 people
    Redim m_Persons(49)

    'Load UDT Array
    For intIndex = 0 to 49
        m_Persons(intIndex).Name = "Name " & intIndex
        m_Persons(intIndex).Age = intIndex
    Next intIndex

End Sub
 
Back
Top