473,320 Members | 1,974 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.

structure element index / iterate

Anyone know of a way to loop through the elements of a structure?
maybe something like a 'for each element in elements' kind of thing?

Thx,
Michael

May 25 '06 #1
8 1158
You could use reflection (GetFields() etc).

Cheers,

Greg
<ml********@yahoo.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Anyone know of a way to loop through the elements of a structure?
maybe something like a 'for each element in elements' kind of thing?

Thx,
Michael

May 25 '06 #2
alternative?...that kinda painful

May 25 '06 #3

foreach( element e in elements ) {}

ml********@yahoo.com wrote:
Anyone know of a way to loop through the elements of a structure?
maybe something like a 'for each element in elements' kind of thing?

Thx,
Michael

May 25 '06 #4
but what is 'element' and 'elements'...how r they defined? I don't
believe those are available as is pertains to a structure

May 25 '06 #5
Just to be clear ... you want to iterate all of its public fields?

There are other possiblities depending what you are trying to do with the
data?

Cheers,

Greg
<ml********@yahoo.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
alternative?...that kinda painful

May 25 '06 #6
I was just wanting to concatenate each element in the structure into a
string...there are hundreds of elements and I didn't want to reference
each one of them by name.

May 26 '06 #7
Reflections is going to be the way to go
http://msdn2.microsoft.com/en-US/library/ch9714z3.aspx
<ml********@yahoo.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
I was just wanting to concatenate each element in the structure into a
string...there are hundreds of elements and I didn't want to reference
each one of them by name.

May 26 '06 #8
Works great..thanks...here's the code sample:

Imports System.Reflection

Module Module1

Structure record
Dim s1 As String
Dim si As Int16
Dim sTest As String
End Structure

Public Sub Main()

Try
Dim i As Integer
Dim myType As Type = GetType(record) 'Get the type of the object we
care about
Dim myField As FieldInfo() = myType.GetFields() 'Get the fields from
my 'type'

For i = 0 To myField.Length - 1
Console.WriteLine("Field name = {0} and type = {1}.",
myField(i).Name, myField(i).FieldType)
Next i
Catch e As Exception
Console.WriteLine("Exception : {0} ", e.Message.ToString()) End Try

End Sub 'Main

End Module

Jun 6 '06 #9

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

Similar topics

9
by: kosh | last post by:
I was wondering if there is or there could be some way to pass a generator an optional starting index so that if it supported that slicing could be made more efficient. Right now if you do use a...
5
by: Jeff | last post by:
I've created a beast! Here is my data structure: $VAR1 = 'bunkers'; $VAR2 = { 'items' => , \ ] }; $VAR3 = 'simpsons'; $VAR4 = {
4
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure struct segment { .... vector <meta_segment>::iterator it_Z; .... };
11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
7
by: Adam | last post by:
Hi all, In my VB.NET code below, I try to change the user name in my arraylist from Ted to Bob, but instead it adds a new user to the arraylist named Bob. Can anyone explain why this happens and...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
1
by: kenneth.m.mcdonald | last post by:
I'd like to iterate over a sequence by both element and index, something like for (element, index) in somefun(sequence): ... Obviously this is easy enough to code, but I seem to remember...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
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: 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)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
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.