473,387 Members | 1,606 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,387 software developers and data experts.

How to access struct elements sequentially?

I would like to be able to access the members of a struct (or class)
based on their order within the struct. For example, say I had a
struct something like:

public struct PhoneTypes
{
public string work;
public string home;
public string mobile;
}

Is there some way I can directly access the first element (work), then
the next (home), etc, without using the specific element names?

Thanks!

~ Rick
Nov 15 '05 #1
1 3826
Here's an example.

public struct PhoneTypes
{
public string work;
public string home;
public string mobile;
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
PhoneTypes p;
p.work = "425-555-1212";
p.home = "206-555-1212";
p.mobile = String.Empty;

Type type = p.GetType();
System.Reflection.FieldInfo[] fields = type.GetFields();
foreach (System.Reflection.FieldInfo fieldInfo in fields)
{
object fieldValue = fieldInfo.GetValue(p);
System.Diagnostics.Trace.WriteLine(fieldValue);
}
}
}
"Rick" <rb****@allstate.com> wrote in message
news:50*************************@posting.google.co m...
I would like to be able to access the members of a struct (or class)
based on their order within the struct. For example, say I had a
struct something like:

public struct PhoneTypes
{
public string work;
public string home;
public string mobile;
}

Is there some way I can directly access the first element (work), then
the next (home), etc, without using the specific element names?

Thanks!

~ Rick

Nov 15 '05 #2

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

Similar topics

2
by: Robert | last post by:
I have the XML file which has a structure similar to one I pasted at the end of this post. As you can see many elements are using different id attributes. For example element Window uses WinID...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
5
by: dam_fool_2003 | last post by:
Hai, Several advanced programs access the struct's member like the one following: #define str(p) (p.data) struct node { unsigned int data; }; int main(void) {
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
3
by: Bruce | last post by:
I am building a WinForms app that uses Web Services access to a server for most of its data input/output, but I also need to persist some of its data to the local disk (basically as a cache of some...
9
by: gk245 | last post by:
I have something like this: struct block { int x; int y; float z; }
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
24
by: Kavya | last post by:
int main (){ int a={{1,2,3},{4,5,6}}; int (*ptr)=a; /* This should be fine and give 3 as output*/ printf("%d\n",(*ptr)); ++ptr;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.