Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I just bumped to an issue and I hope it's an easy one :)

I have a structure that contains various data types:
C#
string, Nullable<datetime>, Nullable<double> and Nullable<int64>

Totally there are 102 variables.
What I need is to do a for or foreach look in that structure and check the values.

The thing I am trying to avoid here is to reference each var separatelly like this:

C#
MyStruct str = new MyStruct();
str.var0 = "xxxx";
str.var1 = "xxxx";
...
...
str.var101 = "xxxx";


I would appreciate if you guys could at least show me the way. Don't need the detailed example as I would prefer to learn myself :)

C#
UPDATE OF THE QUESTION


The reason I am doing this is the following:
I have a DB and I need to take the tada from the DB and store it in the application. Each structure represents a single line of data from the DB (I am using
C#
OleDBDataReader
for that). Then - I am putting everything into
C#
List<MyStruct>
which I need for LINQ queries.
The final result would be another List<mystruct_2>

Thanks!
Posted
Updated 9-Sep-13 22:41pm
v3

That's pretty nasty. You can do it, but it's not a good idea as a general rule unless the actual makeup of the structure is unavailable to you are design time, but the instance is at run time.
You need to play with Reflection to do this:
C#
FieldInfo[] fi = typeof(MyStruct).GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo info in fi)
{
    Console.WriteLine(info.Name);
}

That will give you the names of the fields within the structure, and you can use those (again with Reflection) to give you the values of the actual instance. But it really isn't a good idea unless you really, really have to - as a solution to save you typing it's a sledgehammer to crack a nut! :laugh:
 
Share this answer
 
Comments
Herbisaurus 10-Sep-13 4:40am    
*****
MK-Gii 10-Sep-13 4:43am    
That's loud and clear :)
Thanks!
You can use Reflection!

C#
using System.Reflection;
//...
//...
//...


public void Foo (ref object structObject)
{
  FieldInfo[] members = structObject.GetType().GetFields();

  object tempValueToAssign = "A test string";

  foreach (FieldInfo fi in members)
  {
     // perform update of FieldInfo fi
     fi.SetValue(structObject, tempValueToAssign); 
  }
}


Cheers,
Edo
 
Share this answer
 
v2
Comments
Herbisaurus 10-Sep-13 4:40am    
*****
MK-Gii 10-Sep-13 4:43am    
Thanks!
Joezer BH 10-Sep-13 4:45am    
Maloniai kviečiame!
MK-Gii 10-Sep-13 4:48am    
You should say: "Maloniai prasom" xDD
"Maloniai kvieciame" means "You are pleasantly invited" :D
Joezer BH 10-Sep-13 4:53am    
Tnx for the correction,
it's not easy when you are not native :)
You may use reflection. See, for instance, this Stack Overflow question: "Iterating through Struct members"[^].
 
Share this answer
 
Comments
MK-Gii 10-Sep-13 4:43am    
Thanks!
CPallini 10-Sep-13 4:48am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900