From: steven on
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:

On Friday, November 30, 2007 12:47 PM
sloan wrote:

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?

On Friday, November 30, 2007 1:32 PM
Morten Wennevik [C# MVP] wrote:

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));


On Fri, 30 Nov 2007 18:47:23 +0100, sloan <sloan(a)ipass.net> wrote:

ly



-- =

Happy coding!
Morten Wennevik [C# MVP]

On Friday, November 30, 2007 2:47 PM
sloan wrote:

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


"Morten Wennevik [C# MVP]" <MortenWennevik(a)hotmail.com> wrote in message
news:op.t2ma45e8dj93y5(a)ubuan...
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));


On Fri, 30 Nov 2007 18:47:23 +0100, sloan <sloan(a)ipass.net> wrote:




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

On Friday, November 30, 2007 6:51 PM
Alun Harford wrote:

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/tutorials/aspnet/7597ebc9-868a-420b-96d0-119d3a501d60/wcf-data-services--wcf-behaviors-and-server-side-processing.aspx
From: Arne Vajhøj on
On 21-06-2010 10:34, steven richardson 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?
>
> 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