Solved

Iterate a structure to get the names and values of its members

Posted on 2014-01-23
4
283 Views
Last Modified: 2014-01-23
I want to iterate a structure in order to build a string out of the name and value of its members. The following code seems to be what I need but the .GetValue method expects an object parameter that I don't understand:

Dim sb As New System.Text.StringBuilder
Dim t As Type = GetType(myStructure)
For Each f As System.Reflection.FieldInfo In t.GetFields
        sb.AppendLine(f.Name & " = " & f.GetValue(??) & ", ")
Next f

Open in new window

0
Comment
Question by:TimHudspith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 400 total points
ID: 39804552
In order to get a value, you need to have an object. The parameter you are wondering about is simply the instance that contains the values.

Supposing that your structure has 2 simple fields x and y, the following code would retrieve the values in object o:

Dim sb As New System.Text.StringBuilder
Dim o As MyStructure
		o.x = 10
		o.y = 20
Dim t As Type = GetType(MyStructure)

For Each f As System.Reflection.FieldInfo In t.GetFields
	sb.AppendLine(f.Name & " = " & f.GetValue(o).ToString & ", ")
Next f

Open in new window

0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 50 total points
ID: 39804711
Hi TimHudspith;

This should give you what you need.

Imports System.Reflection

Dim sb As New System.Text.StringBuilder
' Where myStructureTypeName is the Type name of the structure and NOT an instance variable name
Dim t As Type = GetType(myStructureTypeName)

For Each f As FieldInfo In t.GetFields((BindingFlags.Public Or BindingFlags.Instance))
        ' Where myStructureInstanceName is the instance variable of that structure.
        sb.AppendLine(f.Name & " = " & f.GetValue(myStructureInstanceName) & ", ")
Next f

Open in new window

0
 
LVL 28

Assisted Solution

by:Ark
Ark earned 50 total points
ID: 39805503
Just a small addition - for shared members you can use Nothing instead of actual instance
0
 

Author Comment

by:TimHudspith
ID: 39805646
Got it. Thanks all.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question