Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

19 Experts available now in Live!

Get 1:1 Help Now