Solved

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

Posted on 2014-01-23
4
276 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
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 62

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 27

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now