Solved

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

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

825 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