By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,193 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,193 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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?

Nov 15 '05 #2

P: n/a
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" <el*************@wayout.net.NOSPAM> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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?


Nov 15 '05 #3

P: n/a
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" <vc@attbi.com> wrote in message
news:JsTzb.231264$Dw6.816102@attbi_s02...
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" <el*************@wayout.net.NOSPAM> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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?



Nov 15 '05 #4

P: n/a
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" <vc@attbi.com> wrote in message
news:ZMTzb.431788$Fm2.432014@attbi_s04...
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" <vc@attbi.com> wrote in message
news:JsTzb.231264$Dw6.816102@attbi_s02...
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" <el*************@wayout.net.NOSPAM> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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?
>
>



Nov 15 '05 #5

P: n/a
Thanks

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:eO*************@TK2MSFTNGP11.phx.gbl...
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" <vc@attbi.com> wrote in message
news:ZMTzb.431788$Fm2.432014@attbi_s04...
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" <vc@attbi.com> wrote in message
news:JsTzb.231264$Dw6.816102@attbi_s02...
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" <el*************@wayout.net.NOSPAM> wrote in
message news:%2******************@TK2MSFTNGP10.phx.gbl...
> 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?
> >
> >
>
>



Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.