RKh
RKh

Reputation: 14159

Creating class instances at run-time and initializing simultaneously

I am attempting following code to create multiple instances of a class at run-time and want to initialize also, but it is giving error:

A local variable named 'inum' cannot be declared in this scope because it would give a different meaning to 'inum', which is already used in a 'parent or current' scope to denote something else.

public class MyClass
{
    static int i=0;

    class A
    {
        public A()
        { 

        }
    }

    public static void Run()
    {
        string inum = "i";
        for (int j=1;j<=5;j++)
        {
            inum = inum + j.ToString();
            //Initialize Instance
            A inum = new A();
        }
    }
}

Upvotes: 0

Views: 1520

Answers (5)

DaveRandom
DaveRandom

Reputation: 88707

I'm not a C# programmer by any stretch of the imagination, but by the rules of Java and any other similarly syntaxed language I know anything about, what you are doing is attempting to redeclare 'inum' with a new type, after it has been declared as a string in the same scope.

The other point is that even if this were not the case, you are not creating multiple instances but filling the same variable with a new instance 5 times, which would only result in one instance (the last one).

From quickly reading a C# tutorial I think this is something like what you want. I'm not sure what you were trying to do with the 'inum' variable so it is gone, as is static variable 'i':

public class MyClass 
{

    class A
    {         
        public A()
        {

        }
    }

    public static void Run()
    {
        // Declare array to hold instances
        A[] instances;
        // instances is now five elements long
        instances = new A[5];
        for (int j=0;j<5;j++)
        {
            //Initialize Instance
            instances[j] = new A();
        }
    }
}

That should result in an array of 5 objects called 'instances' in the scope of the Run method - you may want this in the scope of the class itself, possibly as a static property.

As a side note, it's good practice to start at 0, not 1, for operations like this (with the var 'j') and the above code reflects this.

Upvotes: 1

hungryMind
hungryMind

Reputation: 7009

You cannot have dynamic variable in c#. The append you are trying is appending the value not the variable pointer.

rather use this way

   Dictionary<int, A> inum = new        Dictionary<int, A>();
    for (int j=1;j<=5;j++)
    {

        //Initialize Instance and add to dictionary
        inum.Add(j, new A());
    }

You can get them by key name. There are several other way to store instances as collection

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502716

You appear to be trying to use variable names "dynamically". That doesn't work in C#, and you should change how you think about variables. If you want to create several instances, declare an array:

public class MyClass
{
    static A[] instances;

    class A
    {
        public A()
        { 

        }
    }

    public static void Run()
    {
        instances = new A[5];
        for (int j=0;j<5;j++)
        {
            instances[j] = new A();
        }
    }
}

Upvotes: 2

Rami Alshareef
Rami Alshareef

Reputation: 7160

try to name the variable A with different name

A objA = new A();

Upvotes: 0

fixagon
fixagon

Reputation: 5566

you cannot call the variable of type A "inum" (there exists already one called like that)

you have to give it another name like:

A anyOtherName = new A();

Upvotes: 0

Related Questions