loop over the static variables of a struct?

  • Thread starter Thread starter sloan
  • Start date Start date
S

sloan

Is there a way to loop over the static variables of a struct?




public struct FunFoods

{



public static readonly Guid GRANOLA = new
System.Guid("00000000-0000-0000-0000-000000000101");

public static readonly Guid TRAILMIX = new
System.Guid("00000000-0000-0000-0000-000000000102");

public static readonly Guid PEANUTS = new
System.Guid("00000000-0000-0000-0000-000000000103");



}



As in....

List<Guid> funFoodList = new List<Guid>();

foreach ( static_variable svar in fun_foods_stucture ) // obviously
this doesn't work, just trying to get the concept across
{

funFoodList.Add(svar);

}


Something like that?
 
Hi Sloan,

That would be something like

FunFoods ff = new FunFoods();
Type t = ff.GetType(); // or typeof(FunFoods)
FieldInfo[] fields = t.GetFields();

List<Guid> guids = new List<Guid>();
foreach (FieldInfo field in fields)
guids.Add((Guid)field.GetValue(ff));
 
thanks


Hi Sloan,

That would be something like

FunFoods ff = new FunFoods();
Type t = ff.GetType(); // or typeof(FunFoods)
FieldInfo[] fields = t.GetFields();

List<Guid> guids = new List<Guid>();
foreach (FieldInfo field in fields)
guids.Add((Guid)field.GetValue(ff));
 
sloan said:
Is there a way to loop over the static variables of a struct?

C# 3:

List<Guid> guids = typeof(FunFoods)
.GetFields(BindingFlags.Public | BindingFlags.Static)
.Select(f=>(Guid)f.GetValue(null, null))
.ToList();

Lovely!

Alun Harford
 
Here is the easiest and best way i have found to do what you want to do.

Public Struct Testing
{
public string t1;
public string t2;
public string t3;
}

public List<Testing> CreateTesting()
{
List<Testing> LT = new List<Testing>();
Testing T = new Testing();
T.t1 = "1";
T.t2 = "2";
T.t2 = "3";
LT.Add(T);
Return LT;
}


Private Void LoopStruct()
{
List<Testing> LT = CreatTesting();
foreach(Testing T in LT)
{
string v1 = T.t1;
string v2 = T.t2;
string v3 = T.t3;
}
}


This requires a bridge method in between that will return a List wich you can loop through. As you know you can add anything to a list so in this case you simply add a stucture to the list. Then you can loop through each structure element of the list. I use this method alot on both web and windows based applications and it works like a charm. I looked at some of the other solutions and honestly i belive this to be the easist and best way to access values inside a stucture.







sloan wrote:

loop over the static variables of a struct?
30-Nov-07

Is there a way to loop over the static variables of a struct?




public struct FunFoods

{



public static readonly Guid GRANOLA = new
System.Guid("00000000-0000-0000-0000-000000000101");

public static readonly Guid TRAILMIX = new
System.Guid("00000000-0000-0000-0000-000000000102");

public static readonly Guid PEANUTS = new
System.Guid("00000000-0000-0000-0000-000000000103");



}



As in....

List<Guid> funFoodList = new List<Guid>();

foreach ( static_variable svar in fun_foods_stucture ) // obviously
this doesn't work, just trying to get the concept across
{

funFoodList.Add(svar);

}


Something like that?

Previous Posts In This Thread:

loop over the static variables of a struct?
Is there a way to loop over the static variables of a struct?




public struct FunFoods

{



public static readonly Guid GRANOLA = new
System.Guid("00000000-0000-0000-0000-000000000101");

public static readonly Guid TRAILMIX = new
System.Guid("00000000-0000-0000-0000-000000000102");

public static readonly Guid PEANUTS = new
System.Guid("00000000-0000-0000-0000-000000000103");



}



As in....

List<Guid> funFoodList = new List<Guid>();

foreach ( static_variable svar in fun_foods_stucture ) // obviously
this doesn't work, just trying to get the concept across
{

funFoodList.Add(svar);

}


Something like that?

Hi Sloan,That would be something likeFunFoods ff =3D new FunFoods();Type t =3D
Hi Sloan,

That would be something like

FunFoods ff =3D new FunFoods();
Type t =3D ff.GetType(); // or typeof(FunFoods)
FieldInfo[] fields =3D t.GetFields();

List<Guid> guids =3D new List<Guid>();
foreach (FieldInfo field in fields)
guids.Add((Guid)field.GetValue(ff));



ly



-- =

Happy coding!
Morten Wennevik [C# MVP]

Re: loop over the static variables of a struct?
thanks


Hi Sloan,

That would be something like

FunFoods ff = new FunFoods();
Type t = ff.GetType(); // or typeof(FunFoods)
FieldInfo[] fields = t.GetFields();

List<Guid> guids = new List<Guid>();
foreach (FieldInfo field in fields)
guids.Add((Guid)field.GetValue(ff));






--
Happy coding!
Morten Wennevik [C# MVP]

Re: loop over the static variables of a struct?
sloan wrote:

C# 3:

List<Guid> guids = typeof(FunFoods)
..GetFields(BindingFlags.Public | BindingFlags.Static)
..Select(f=>(Guid)f.GetValue(null, null))
..ToList();

Lovely!

Alun Harford


Submitted via EggHeadCafe - Software Developer Portal of Choice
WCF Data Services / WCF Behaviors And Server Side Processing
http://www.eggheadcafe.com/tutorial...wcf-behaviors-and-server-side-processing.aspx
 
Here is the easiest and best way i have found to do what you want to do.

Public Struct Testing
{
public string t1;
public string t2;
public string t3;
}

The question is from 2007.

And your fields are not static.

And public and struct are not capitalized in C#.

Arne
 
Back
Top