Home All Groups Group Topic Archive Search About

Dynamically reading structure fields

Author
18 Apr 2006 3:38 PM
sameerishah
Hi,

I have a need to read the fields of a structure in a for loop.
for eg. the structure looks like:
str.a1, str.a2, str.a3 where a1, a2, a3 are of type string
How do I do it?

Thanks in advance,
Sam

Author
18 Apr 2006 4:12 PM
vbnetdev
Private Structure mystruct
        Public field1 As Short
        Public field2 As String
        Public pairForListItem As String
    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim myarraylist As New ArrayList
        Dim InstanceofMyClass As mystruct
        Dim i As Integer
        For i = 0 To 5
            With InstanceofMyClass
                .field1 = i
                .field2 = "field 2 value " & i
                .pairForListItem = "value " & i & "Text " & i
            End With
            myarraylist.Add(InstanceofMyClass)
        Next
End Sub

--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com



<sameeris***@gmail.com> wrote in message
Show quoteHide quote
news:1145374703.892145.14510@v46g2000cwv.googlegroups.com...
> Hi,
>
> I have a need to read the fields of a structure in a for loop.
> for eg. the structure looks like:
> str.a1, str.a2, str.a3 where a1, a2, a3 are of type string
> How do I do it?
>
> Thanks in advance,
> Sam
>
Author
18 Apr 2006 4:28 PM
Sam
Hi,

Thanks for taking the time out to help me.

In your code, it seems like you are assigning values to the fields of
the structure and what I need is a way to read the fields values in a
For loop. Do you think that is doable?

Sam

vbnetdev wrote:
Show quoteHide quote
> Private Structure mystruct
>         Public field1 As Short
>         Public field2 As String
>         Public pairForListItem As String
>     End Structure
>
>     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>         Dim myarraylist As New ArrayList
>         Dim InstanceofMyClass As mystruct
>         Dim i As Integer
>         For i = 0 To 5
>             With InstanceofMyClass
>                 .field1 = i
>                 .field2 = "field 2 value " & i
>                 .pairForListItem = "value " & i & "Text " & i
>             End With
>             myarraylist.Add(InstanceofMyClass)
>         Next
> End Sub
>
> --
> Get a powerful web, database, application, and email hosting with KJM
> Solutions
> http://www.kjmsolutions.com
>
>
>
> <sameeris***@gmail.com> wrote in message
> news:1145374703.892145.14510@v46g2000cwv.googlegroups.com...
> > Hi,
> >
> > I have a need to read the fields of a structure in a for loop.
> > for eg. the structure looks like:
> > str.a1, str.a2, str.a3 where a1, a2, a3 are of type string
> > How do I do it?
> >
> > Thanks in advance,
> > Sam
> >
Author
18 Apr 2006 5:20 PM
Jim Wooley
> I have a need to read the fields of a structure in a for loop.
> for eg. the structure looks like:
> str.a1, str.a2, str.a3 where a1, a2, a3 are of type string

You should be able to do this with reflection. Below is a quick demo of how
to pull fields from a structure.

Module Module1

    Sub Main()

        Dim StructInstance As MyStruct
        StructInstance.Prop1 = "Foo"
        StructInstance.Prop2 = "Bar"

        Dim propInfo As Reflection.FieldInfo() = Type.GetType("ConsoleApplication1.MyStruct").GetFields
        For Each item As Reflection.FieldInfo In propInfo
            Console.WriteLine(item.GetValue(StructInstance))
        Next
        Console.ReadKey()
    End Sub

End Module

Public Structure MyStruct
    Public Prop1 As String
    Public Prop2 As String
End Structure

Be aware that there is a boxing penalty with structures and you may want
to use classes instead. You should read up on boxing and the performance
penalties they incur. You will be able to do the same kind of reflection
code with classes if you decide to change to that route.

Jim Wooley
http://devauthority.com/blogs/jwooley
Author
18 Apr 2006 5:49 PM
Sam
Jim,

I appreciate your help and advice. This helps a bunch. Many thanks.

Sam