How to create static objects at runtime from values stored in a table

  • Thread starter Thread starter Mountain Bikn' Guy
  • Start date Start date
M

Mountain Bikn' Guy

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?
 
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 = value;
}
}
}

Best regards,
Sherif
 
Hi Sherif,
Thanks for your reply. I had planned on using reflection, and yes indeed the
fields must be static. Your first example code looks like it will do the
trick. I had just found something similar by searching Google Groups and a
quick test shows it will work for me.

Now my next question is how to quickly match the FieldInfo array items to
the DataTable row items. I'll probably start by iterating thru the FieldInfo
array. Then I will need to use the name of the field to get the correct row
from the DataTable.

If I sort the FieldInfo array, will it sort by field name? If so, then I can
sort a DataView by the same field and efficiently step thru both collections
in a synchronized manner. Do you have a better idea?

Thanks for your help.
Mountain

Sherif ElMetainy said:
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 = value;
}
}
}

Best regards,
Sherif

Mountain Bikn' Guy said:
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?
 
I guess I would have to derive a class from FieldInfo and implement
IComparable in order to do what I thought might work. Do you have a better
suggestion? I appreciate any tips.
Regards,
Mountain

Mountain Bikn' Guy said:
Hi Sherif,
Thanks for your reply. I had planned on using reflection, and yes indeed the
fields must be static. Your first example code looks like it will do the
trick. I had just found something similar by searching Google Groups and a
quick test shows it will work for me.

Now my next question is how to quickly match the FieldInfo array items to
the DataTable row items. I'll probably start by iterating thru the FieldInfo
array. Then I will need to use the name of the field to get the correct row
from the DataTable.

If I sort the FieldInfo array, will it sort by field name? If so, then I can
sort a DataView by the same field and efficiently step thru both collections
in a synchronized manner. Do you have a better idea?

Thanks for your help.
Mountain

Sherif ElMetainy said:
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 = value;
}
}
}

Best regards,
Sherif

Mountain Bikn' Guy said:
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?

 
Hello

You can make a class the implements System.Collections.IComparer interface
use the static System.Array.Sort(Array, IComparer); version of Sort, then
you don't have to derive a class from FieldInfo

Best regards
Sherif

Mountain Bikn' Guy said:
I guess I would have to derive a class from FieldInfo and implement
IComparable in order to do what I thought might work. Do you have a better
suggestion? I appreciate any tips.
Regards,
Mountain

Mountain Bikn' Guy said:
Hi Sherif,
Thanks for your reply. I had planned on using reflection, and yes indeed the
fields must be static. Your first example code looks like it will do the
trick. I had just found something similar by searching Google Groups and a
quick test shows it will work for me.

Now my next question is how to quickly match the FieldInfo array items to
the DataTable row items. I'll probably start by iterating thru the FieldInfo
array. Then I will need to use the name of the field to get the correct row
from the DataTable.

If I sort the FieldInfo array, will it sort by field name? If so, then I can
sort a DataView by the same field and efficiently step thru both collections
in a synchronized manner. Do you have a better idea?

Thanks for your help.
Mountain

Sherif ElMetainy said:
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 = value;
}
}
}

Best regards,
Sherif

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.
 
Thanks

Sherif ElMetainy said:
Hello

You can make a class the implements System.Collections.IComparer interface
use the static System.Array.Sort(Array, IComparer); version of Sort, then
you don't have to derive a class from FieldInfo

Best regards
Sherif

Mountain Bikn' Guy said:
I guess I would have to derive a class from FieldInfo and implement
IComparable in order to do what I thought might work. Do you have a better
suggestion? I appreciate any tips.
Regards,
Mountain

indeed
the
and
a
quick test shows it will work for me.

Now my next question is how to quickly match the FieldInfo array items to
the DataTable row items. I'll probably start by iterating thru the FieldInfo
array. Then I will need to use the name of the field to get the
correct
row
from the DataTable.

If I sort the FieldInfo array, will it sort by field name? If so, then
I
can
sort a DataView by the same field and efficiently step thru both collections
in a synchronized manner. Do you have a better idea?

Thanks for your help.
Mountain

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 = value;
}
}
}

Best regards,
Sherif

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?

 
Back
Top