Hello
question. Must the objects be static fields, or you can have another
alternative, like a static array, arraylist, or hashtable instead of
hardcoding 400 static fields in the code?
If they must be static fields, then you can use reflection.
for (int i = 0; i < dt.Rows.Count; ++i)
{
object value = ............; //whatever code you use to get the value
from the dataset
System.Reflection.FieldInfo fi = typeof(UtilityClass).GetField("Object"
+ i, System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.Public);
fi.SetValue(null, value);
}
otherwise, if you don't have to stick with static fields you can use a
hashtable
public sealed class UtilityClass
{
public static Hashtable AllObjects;
static UtilityClass()
{
AllObjects = new Hashtable();
}
public static CreateObjects()
{
for (int i = 0; i < dt.Rows.Count; ++i)
{
object value = ............; //whatever code you use to get the
value from the dataset
AllObjects["Object" + i] = value;
}
}
}
or an array
public sealed class UtilityClass
{
public static MyObject[] AllObjects;
public static CreateObjects()
{
MyObject[] AllObjects = new MyObject[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; ++i)
{
object value = ............; //whatever code you use to get the
value from the dataset
AllObjects[i] = value;
}
}
}
Best regards,
Sherif
"Mountain Bikn' Guy" <vc@attbi.com> wrote in message
news:lxSzb.431022$Fm2.432002@attbi_s04...
How would I do this?
public sealed class UtilityClass
{
public static MyObject Object1;//see note below about importance of
static object names in this class
public static MyObject Object2;
// ...
public static MyObject Object400;
public static CreateObjects()
{
for (int i = 0; i < dt.Rows.Count; ++i)
{
//create each static ObjectN field using values stored in
table. How?
}
}
}
I have an DataSet with DataTable dt. The DataSet is created by reading an
XML file. Then I need to create a bunch of static objects. The objects
fields are all hard-coded into this utility class and the names are
extremely important. I can't actually name them generically as shown. It
is very important for other code that we be able to call
UtilityClass.SpecificObjectName. I need an efficient way to use all the
data in the DataTable to create these specifically named static objects. Any
suggestions?