Which type of object for these values?

  • Thread starter Thread starter Brett
  • Start date Start date
B

Brett

I need an object/structure such as an array but each item within the array
is a complex structure. For example, I have 5 items, say:
A thru E. The number of items may change. Each item has certain
properties. Such as:

A.p1
A.p2
A.p3
.......
E.p1
E.p2
E.p3

I'll assign values, via a loop, to what ever structure will hold these
complex types. For example:

For i = 0 To structure.count 'where count is 5
structure(i).p1 = something
structure(i).p2 = something
structure(i).p3 = something
Next

What type of structure will I need for the above?

Thanks,
Brett
 
Hi,

I would store the info in a class.

Public Class MyData

Dim mp1, mp2, mp3 As Point



Public Sub New()

mp1 = New Point

mp2 = New Point

mp3 = New Point

End Sub

Public Property p1() As Point

Get

Return mp1

End Get

Set(ByVal Value As Point)

mp1 = Value

End Set

End Property

Public Property p2() As Point

Get

Return mp2

End Get

Set(ByVal Value As Point)

mp2 = Value

End Set

End Property

Public Property p3() As Point

Get

Return mp3

End Get

Set(ByVal Value As Point)

mp3 = Value

End Set

End Property

End Class



Ken
-----------------
I need an object/structure such as an array but each item within the array
is a complex structure. For example, I have 5 items, say:
A thru E. The number of items may change. Each item has certain
properties. Such as:

A.p1
A.p2
A.p3
.......
E.p1
E.p2
E.p3

I'll assign values, via a loop, to what ever structure will hold these
complex types. For example:

For i = 0 To structure.count 'where count is 5
structure(i).p1 = something
structure(i).p2 = something
structure(i).p3 = something
Next

What type of structure will I need for the above?

Thanks,
Brett
 
As I mentioned, the total number of items aren't defined. You've defined
three "Point"s. There may be 1 or 100 or anything in between. I also need
the ability to loop through each item and reference the particular item's
properties.

Would an array with structures for each item work? For example, say I had
three items for one case. An item will always have three properties. The
structure would look like this:

array.item(0).p1 = someval1
array.item(0).p2 = someval2
array.item(0).p3 = someval3

array.item(1).p1 = someval11
array.item(1).p2 = someval21
array.item(1).p3 = someval31

array.item(2).p1 = someval12
array.item(2).p2 = someval22
array.item(2).p3 = someval32

Brett
 
As ken says put your item data in a class then load them into either an
arraylist or hashtable (check the docs) as appropriate, that way you dont
need to worry about the number of items you are storing
(hashtable gives 'indexed' access)

hth

guy
 
Brett,

In my opinion is a structure a structure, not a variable array.

Can you make that point about the variable P1, P2, P3, Px more clear.

Cor
 
Guess it's more that I don't understand how it works. Could you provide an
example of what the array loop with references to each of the three
properties for an item would look like?

Thanks,
Brett
 
Brett,
As Ken suggests define a Structure or Class to hold a single item.

' From Ken's post
Public Class MyData
...
End Class

Then define an array to hold a collection of these items.

Dim list(5 - 1) As MyData ' list of 5 MyData objects

' explicitly create each element of array
For i = 0 To list.count - 1 'where count is 5
list(i) = New MyData(...)
Next

' do something to each property of each element
For i = 0 To list.count - 1 'where count is 5
list(i).p1 = something
list(i).p2 = something
list(i).p3 = something
Next

Because we defined MyData as Class, you need to create an instance of the
class in each array element first. If we had defined MyData as a Structure,
each element would be automatically created. Its the primary difference
between Reference types (classes) and Value types (structures).

As the others have suggested, using an ArrayList or Hashtable (or other
collections from System.Collections & System.Collections.Specialized) may be
a better fit for the problem at hand. Normally I create a specialized
collection that inherits from DictionaryBase or CollectionBase so my
collection is strongly typed...

Hope this helps
Jay
 
For the simplest possible solution (if I understand you right, and I'm not
sure that I do), you would have a class that contains an indexed property.
You would then have an array of classes.

So

MyClassArray(0).Property(0) = "blah blah"

Look up Indexed properties in the VB.NET language reference for more info on
how to do this.

if on the other hand you are talking about having a structure that could
contain any other kind of object, but sometimes none at all, get hold of a
copy of the book "Design Patterns" and look at the Composite pattern. That
might help for nested structures of different object types.

That help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top