Link to home
Start Free TrialLog in
Avatar of CRX4LIFE18
CRX4LIFE18

asked on

Using Data Structures as I would an array

Hello,
     I have written a .dll and have been using arrays for a project I've been working on for a while now, though I've recently discovered how to create my own data structure. I like that fact that I can have a description of sorts as opposed to having a huge list of numbers/text, but I need to be able to loop through the datastructure to get all of the informatoin out without having to designate each item as my current setup my arrays are very large and this would take a lot of time. My goal is to be able to somehow get these into an indexable array if possible to throw them in a loop to dump all data as below:

Public Structure Test
     Public WidgetName as String
     Public WidgetColor as String
     Public WidgetWeight as Integer
End Structure

Test.WidgetName = Widget1
Test.WidgetColor = Green
Test.WidgetWeight = 15

Dim Counter1 as Integer = -1
Dim DataString as String
Do
     Counter1 = Counter1 + 1
     DataString = DataString & "," & Test(Counter1)
Loop until Counter1 = DataString.Length - 1

Is there any way to do this? Any help would be greatly appreciated!
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Create the array:

Dim listTest As New List(Of Test)

Then you can do this enumeration:

For each test As Test in listTest
' do something
Next
Avatar of CRX4LIFE18
CRX4LIFE18

ASKER

I suppose I could do that, but is there any way to have the structure carry the indexed data with it? For example, is there any way to say:

Public Structure Test
     Public WidgetName as String
     Public WidgetColor as String
     Public WidgetWeight as Integer
End Structure

Test.WidgetName = "Widget1"
Test.WidgetColor = "Green"
Test.WidgetWeight = 15

Name = Test(0)

And Name would be assigned the value "Widget1"?
Well.... the easiest way is probably to move from a structure to a full-blown object. Then you've got all sorts of options.

Your approach carries with it the idea of needing to know what each column index is.... whereas a datarow (for example) allows you to refer to each field by name or ordinal position.

The simplest version is this:

Public Class Test
     Public WidgetName as String
     Public WidgetColor as String
     Public WidgetWeight as Integer

     Public Function GetStringArray() As String
          Return WidgetName & "," & WidgetColor & "," & WidgetWeight.ToString()
     End Function
End Class
I see what you're saying and I do appreciate your help and suggestions...  I was looking for an easier way for others to access and understand the .dll, and thought that the datastructure would work well if I could get it to work I tried a few approaches to do what I'm trying to do, and one error I'm getting is the following:

Error: Structure cannot be indexed because it has no default property

This error gives me the impression that I can do what I'm trying to do (index the items in the data structure) if I somehow assign it a default property, or if I can somehow assign the items in the structure an index, for example:

Public Structure Test
     Public WidgetName(0) as String
     Public WidgetColor(1) as String
     Public WidgetWeight(2) as Integer
End Structure

I've worked a bit with datasets, and I guess I'm thinking that I should somehow be able to do the same thing here. For example, with datasets I can call a value by doing one of the following:

Dataset.Tables("DataTable").Rows(0).Item(1)
OR
Dataset.Tables("DataTable").Rows(0).Item("WidgetColor")

I'm guessing I'm just not able to do it, but thought I'd throw it out there... Again, thanks for all of your help...
ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial