step through a struct using foreach

T

titan nyquist

Can I step through a struct using foreach? I want a method that steps
through a struct, and prints out the name/value of each member. Can
it be done?

The error I get when trying this is: "foreach statement cannot
operate on variables of type '...' because '...' does not contain a
public definition for 'GetEnumerator'. And it points me to
http://msdn2.microsoft.com/en-us/library/t51esaeb(vs.80).aspx

I guess this is the answer, but it is over my head at this moment.
 
M

Marc Gravell

What do you mean, "steps through a struct"? If you mean the
sub-properties, then something like

foreach(PropertyDescriptor property in
TypeDescriptor.GetProperties(myValue)) {
Debug.WriteLine(property.GetValue(myValue),
property.Name);
}

may suffice...

Marc
 
T

titan nyquist

Marc,

Here's example code (that does not work) of what I was trying to do:

namespace DisplayStructContentsTest
{
class Program
{

struct Employee
{
public string name;
public int age;
public string location;
};

static void Main(string[] args)
{
Employee employee;

employee.name = "Jim Smith";
employee.age = 35;
employee.location = "California";

foreach (object obj in employee)
{
Console.WriteLine(obj + " = " + obj.ToString());

// Output I wish for:
//
// name = Jim Smith
// age = 35
// location = California
}


}
}
}
 
T

titan nyquist

The point is, if I modify the struct, I want the code to automatically
handle the new structure. For example, if I add a "birthdate" field,
the code should (without modification) display the birthdate.
 
M

Marc Gravell

Exposing fields forces you to use reflection; very similar to the
component model code:

foreach (FieldInfo fi in employee.GetType().GetFields()) {
Console.WriteLine(fi.Name + " = " +
Convert.ToString(fi.GetValue(employee)));
}

But!! I can't tell you how to code, but you are a: using public
fields, and b: using mutable structs. Neither of these is particularly
good practice in .Net. Please note that CLR structs are very different
to C[++] structs; they are not the same, and perhaps it was
unfortunate to call them structs in C#. What you have feels more like
a class. The public fields are marginally less critical, but prevent
you from doing any find of validation / notification, or changing the
model. I strongly advise using .Net the way it is intended, i.e.
(short version):

class Employee {
private string name, location;
private int age;
public string Name { get { return name; } set { name =
value; } }
public string Location { get { return location; } set {
location = value; } }
public int Age { get { return age; } set { age =
value; } }
};

This obeys a lot more CLR conventions, and will work more as expected
in collections etc.

Marc
 
M

Marc Gravell

Don't worry... we get it ;-p See other post.

However; this precisely illustrates my point re properties. When you
add the "birthdate", you might want to cease having "age" as a field,
and start using a calculated "get", i.e. using the offset from
DateTime.Now when the "get" is called.

Marc
 
T

Tom Porterfield

titan said:
The point is, if I modify the struct, I want the code to automatically
handle the new structure. For example, if I add a "birthdate" field,
the code should (without modification) display the birthdate.

The following should do it:

Employee employee;

employee.name = "Jim Smith";
employee.age = 35;
employee.location = "California";

Type t = employee.GetType();
System.Reflection.FieldInfo [] fields =
t.GetFields(System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.Public);

foreach (System.Reflection.FieldInfo field in fields)
{
Console.WriteLine(field.Name + " = " + field.GetValue(employee));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top