473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dynamically reading structure fields

Hi,

I have a need to read the fields of a structure in a for loop.
for eg. the structure looks like:
str.a1, str.a2, str.a3 where a1, a2, a3 are of type string
How do I do it?

Thanks in advance,
Sam

Apr 18 '06 #1
4 1996
Private Structure mystruct
Public field1 As Short
Public field2 As String
Public pairForListItem As String
End Structure

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim myarraylist As New ArrayList
Dim InstanceofMyClass As mystruct
Dim i As Integer
For i = 0 To 5
With InstanceofMyClass
.field1 = i
.field2 = "field 2 value " & i
.pairForListItem = "value " & i & "Text " & i
End With
myarraylist.Add(InstanceofMyClass)
Next
End Sub

--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

<sa*********@gmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
Hi,

I have a need to read the fields of a structure in a for loop.
for eg. the structure looks like:
str.a1, str.a2, str.a3 where a1, a2, a3 are of type string
How do I do it?

Thanks in advance,
Sam

Apr 18 '06 #2
Sam
Hi,

Thanks for taking the time out to help me.

In your code, it seems like you are assigning values to the fields of
the structure and what I need is a way to read the fields values in a
For loop. Do you think that is doable?

Sam

vbnetdev wrote:
Private Structure mystruct
Public field1 As Short
Public field2 As String
Public pairForListItem As String
End Structure

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim myarraylist As New ArrayList
Dim InstanceofMyClass As mystruct
Dim i As Integer
For i = 0 To 5
With InstanceofMyClass
.field1 = i
.field2 = "field 2 value " & i
.pairForListItem = "value " & i & "Text " & i
End With
myarraylist.Add(InstanceofMyClass)
Next
End Sub

--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

<sa*********@gmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
Hi,

I have a need to read the fields of a structure in a for loop.
for eg. the structure looks like:
str.a1, str.a2, str.a3 where a1, a2, a3 are of type string
How do I do it?

Thanks in advance,
Sam


Apr 18 '06 #3
> I have a need to read the fields of a structure in a for loop.
for eg. the structure looks like:
str.a1, str.a2, str.a3 where a1, a2, a3 are of type string


You should be able to do this with reflection. Below is a quick demo of how
to pull fields from a structure.

Module Module1

Sub Main()

Dim StructInstance As MyStruct
StructInstance.Prop1 = "Foo"
StructInstance.Prop2 = "Bar"

Dim propInfo As Reflection.FieldInfo() = Type.GetType("ConsoleApplication1.MyStruct").GetFi elds
For Each item As Reflection.FieldInfo In propInfo
Console.WriteLine(item.GetValue(StructInstance))
Next
Console.ReadKey()
End Sub

End Module

Public Structure MyStruct
Public Prop1 As String
Public Prop2 As String
End Structure

Be aware that there is a boxing penalty with structures and you may want
to use classes instead. You should read up on boxing and the performance
penalties they incur. You will be able to do the same kind of reflection
code with classes if you decide to change to that route.

Jim Wooley
http://devauthority.com/blogs/jwooley
Apr 18 '06 #4
Sam
Jim,

I appreciate your help and advice. This helps a bunch. Many thanks.

Sam

Apr 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Oschler | last post by:
Hello, I am a Python newbie (by experience, not chronologically :) ), so if any of this doesn't make sense my apologies in advance. I am reading the chapter in The Python Cookbook on databases...
14
by: Peter Olcott | last post by:
I want to be able to efficiently build data structures at run-time. These data structures need to be accessed with minimal time. The only a few ways that come immediately to mind would be some...
1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
2
by: Chris Haynes | last post by:
Hello all, I have a structure: typedef struct UVstruct { float u, v; } uv; Inside a function (A) i declare a pointer to an instance of this structure:
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
by: AlabiChin | last post by:
Hello, I'm trying to find out if it is possible to dynamically add or remove fields for a structure. I'm in a situation where I don't know how many items I want to store and, therefore, will not...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.