473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
5 1888
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Shibu | last post by:
Hi, I have a situation where I need to convert business objects to a flat table. The reverse is also required. I am using c# and Oracle ODP. I am looking for an easier method to do the below...
3
by: Faisal | last post by:
Hi. I'm in the process of moving an application from ASP to ASP.NET, & I'm writing in VB, using VS.NET. I'm new to the .NET framework & have a basic question regarding static objects defined in...
5
by: pitdog | last post by:
Hello, I am unable to find information on about this issue so I thought I would post and ask. I have a class that is using the sigleton pattern as it has an internal static instance of...
4
by: Michael | last post by:
Hi, I have a class with static fields. The values of those fields appear to stick between Web requests. That behaviour is a bit of a surprise to me, as I would expect the object to be completely...
6
by: TonyMast | last post by:
VB 2005 - XP Pro - Windows forms I'm trying to write a simple multiplication tables program for my little girl. How can I create 200 labels, 100 textboxes. Here is what I've tried so far...
7
by: puzzlecracker | last post by:
Dynamically allocated objects reside on a heap, local objects on a stack. What about static objects? Thanks
1
by: san1014 | last post by:
Hi I have a table SQL> select * from nodes; NODE_ID NODE_NAME -------------------- ------------------------------ N1 Kothhapet N2 Nagole...
1
by: rxyz | last post by:
Hi, I am facing problem in c# using vs 2.0. I had created windows form named as MainForm.cs.In code-behind class MainForm.Designer.cs some of objects were declared as static. Surprisingly when...
2
by: =?Utf-8?B?SHVzYW0=?= | last post by:
Hi EveryBody: I am using vb.net 2005 and the following code trying to create varible at runtime: For n=1 to 4 dim v+n.tostring(n) as double Next n the resone behaind using the previous...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.