structure element index / iterate

  • Thread starter Thread starter mlafarlett
  • Start date Start date
M

mlafarlett

Anyone know of a way to loop through the elements of a structure?
maybe something like a 'for each element in elements' kind of thing?

Thx,
Michael
 
but what is 'element' and 'elements'...how r they defined? I don't
believe those are available as is pertains to a structure
 
Just to be clear ... you want to iterate all of its public fields?

There are other possiblities depending what you are trying to do with the
data?

Cheers,

Greg
 
I was just wanting to concatenate each element in the structure into a
string...there are hundreds of elements and I didn't want to reference
each one of them by name.
 
Works great..thanks...here's the code sample:

Imports System.Reflection

Module Module1

Structure record
Dim s1 As String
Dim si As Int16
Dim sTest As String
End Structure

Public Sub Main()

Try
Dim i As Integer
Dim myType As Type = GetType(record) 'Get the type of the object we
care about
Dim myField As FieldInfo() = myType.GetFields() 'Get the fields from
my 'type'

For i = 0 To myField.Length - 1
Console.WriteLine("Field name = {0} and type = {1}.",
myField(i).Name, myField(i).FieldType)
Next i
Catch e As Exception
Console.WriteLine("Exception : {0} ", e.Message.ToString()) End Try

End Sub 'Main

End Module
 
Back
Top