Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Want To Create New Instance Every New iteration And I Am My Trying To concatenate Object Name Than It Throws Error

C#
static void Main(string[] args)
     {
         int i;
         char ch = 'n';
         while(ch=='n')
         {
             for(i=0;i<=10;i++)
             {   // I Want To Create New Instance Every New iteration And My Trying To concatenate  Object Name It Throws Error
                  lern "st"+i = new lern();
             }
         }

     }
 }
 class lern
 {
     public static int i = 0;
     public int m1,m2,m3;
     public string nm, sub;
    public lern ()
     {

     }
 }
Posted
Updated 4-Nov-15 20:40pm
v2
Comments
Gautham Prabhu K 5-Nov-15 4:53am    
You cannot create variables at run time.
BillWoodruff 5-Nov-15 11:00am    
Why do you have a loop that will never exit ?

No, Wrong way.
In C# do not allow to create variables names at runtime, this mean that your solution is impossible with C#.

You should have a look at documentation about arrays and resizeable lists.
 
Share this answer
 
v2
Comments
CPallini 5-Nov-15 3:11am    
5.
Patrice T 5-Nov-15 3:24am    
Thank You
You cannot create a variable like this
C#
lern "st"+i = new lern();

in c#. It will not even compile.

You have to do something like this:
C#
List<lern> lernList = new List<lern>();
for (i=0; i<=10; i++)
{
    lernList.Add(new lern());
}


That said, we have to take a look at your outer loop.
C#
while (ch=='n')
{
...
}

How will you get out of this loop?
Where do you change the value of ch?
 
Share this answer
 
v3
Comments
Hitesh Jain 5-Nov-15 3:06am    
while(ch=='n')
{
for(i=0;i<=10;i++)
{ // I Want To Create New Instance Every New iteration And My Trying To concatenate Object Name It Throws Error
lern "st"+i = new lern();


}
Console.WriteLine("Press s To Stop");
Console.ReadLine(ch);
}
Patrice T 5-Nov-15 3:25am    
Use Improve question instead.
CPallini 5-Nov-15 3:12am    
5.
George Jonsson 5-Nov-15 21:28pm    
Thanks
You can't do that. You can't create variable names at run time, because the compiler can't check them! Even if you could, you couldn;t use them as they would go "out of scope" at the end of the list.

What you want to do is use an array, or a collection:
C#
private List<lern> lerns = new List<lern>();
static void Main(string[] args)
    {
    int i;
    char ch = 'n';
    while(ch=='n')
        {
        for(i=0;i<=10;i++)
            {   
            lern l = new lern();
            lerns.Add(l);
            }
        }
    }

Then in subsequent code you can refer to each instance separately:
C#
lern firstLern = lerns[0];
lern lastLern = lerns[lerns.Count - 1];
 
Share this answer
 
Comments
CPallini 5-Nov-15 3:12am    
5.
Hitesh Jain 5-Nov-15 3:29am    
Hey What Do You Mean By 5. ?
phil.o 5-Nov-15 3:47am    
He means he gave the answer a note of 5, because he considers the answer to the question as perfectly valid.
You can not create a variable name at runtime, compile will not compile the code as

lern "st"+i = new lern();

The solution provided by pankaj is nic.
 
Share this answer
 
Try this
C#
static void Main(string[] args)
     {
List<lern> objList = new List<lern>();
       int i;
       char ch = 'a';
       while(ch<='n')
       {
           for(i=0;i<=10;i++)
           {
               objList.Add(new lern("st"+ch + i));
           }
           ch++;
       }
       string[] str = objList.Select(x => x.objID).ToArray();
       string result = string.Join("|", str);
}

class lern
    {
        public static int i = 0;
        public int m1, m2, m3;
        public string nm, sub;
        public string objID { get; set; }
        public lern(string strobjID)
        {
            this.objID = strobjID;
        }
    }</lern></lern>
 
Share this answer
 
v2
Comments
George Jonsson 5-Nov-15 21:30pm    
Maybe a little modification needed.
char ch = 'a';
while(ch=='n') // How will the while loop be executed?
[no name] 5-Nov-15 23:21pm    
yup .. a typo mistake. it should be while(ch<='n'). I has been updated the solution. Thanks for correcting me.

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