Click here to Skip to main content
15,889,656 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am creating a object as below.


zkemkeeper.CZKEMClass axCZKEM = new zkemkeeper.CZKEMClass();

but, I want to create this object in a loop upto device numbers as below,

axCZKEM1
axCZKEM2 ....

C#
for (int i = 0; i < int.Parse(System.Configuration.ConfigurationSettings.AppSettings["NoOfPunchStation"]); i++)
          {
              zkemkeeper.CZKEMClass "axCZKEM"+i = new zkemkeeper.CZKEMClass();

          }


no idea. :(

thanks
Posted

So create a collection of the objects, and store them in there.
For example, an array:
C#
zkemkeeper.CZKEMClass[] created = new zkemkeeper.CZKEMClass[10];
for (int i = 0; i < created.Length; i++)
   {
   created[i] = new zkemkeeper.CZKEMClass();
   }
 
Share this answer
 
Comments
hasbina 30-Oct-14 7:24am    
@OriginalGriff
Thank you sir...thanks a lotssss.....
OriginalGriff 30-Oct-14 7:38am    
You're welcome!
There are many ways to accomplish this. I'd go with an array of objects,something like this

C#
int noPunch= int.Parse(System.Configuration.ConfigurationSettings.AppSettings["NoOfPunchStation"];
zkemkeeper.CZKEMClass[] CZKEMarray= new zkemkeeper.CZKEMClass[noPunch];

for (int i=0;i<nopunch;i++)>
{
    CZKEMarray[i]=new zkemkeeper.CZKEMClass();
}


And then access each object with

C#
CZKEMarray[i];
 
Share this answer
 
Comments
hasbina 30-Oct-14 7:24am    
@Pikoh
thank you sir...thank you..:)
Pikoh 30-Oct-14 8:00am    
You're welcome :)
Assuming you have a real need to access the instances of your run-time created objects by names like: "axCZKEM1" , "axCZKEM2" :
C#
// less typing with Directives !
using czClass = YourNameSpaceGoesHere.zkemkeeper.CZKEMClass;

private Dictionary<string, czClass> dctCZ = new Dictionary<string, czClass>();

private const string czNameBase = "axCZEM_";

private void initializeCZClasses()
{
    int nCZ = int.Parse 
    (System.Configuration.ConfigurationSettings.AppSettings["NoOfPunchStation"]);

    for (int i = 1; i <= nCZ; i++)
    {
        string name = czNameBase + i.ToString();
        dctCZ.Add(name, new czClass(name, i));
    }
}

// test it somewhere
var cz = dctCZ["axCZKEM_4"];
bool is4 = cz.someInt == 4 && cz.Name == "axCZKEM_4";
Note that we assume the zkemkeeper.CZKEMClass class has two Properties, one string (Name), one int (someInt) , that are set in its constructor call.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900