Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

615 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