Delta_Cmdr
Delta_Cmdr

Reputation: 180

c# Unique variable Name

Hi there I have a bit of a noob question about creating unique variable names in C#. I would like to make an ArrayList of Enemy objects and I want to call each enemy "enemy[1]" I would like to make a for loop and I would like to use the value of "i" to add on to the end of the word enemy. so for instance:

forloop
{
Enemy enemy1 = new Enemy(actual parameters of the enemy class)
Enemy enemy2 = new Enemy();
Enemy enemy3 = new Enemy();
}

Any help would be appreciated.

Upvotes: 0

Views: 2095

Answers (7)

tolga güler
tolga güler

Reputation: 347

 List<Enemy> listOfYourEnemy = new List<Enemy>();
 for (int i = 0; i < n; i++)
 {
     listOfYourEnemy.Add(new Enemy() {unqName= string.Format("enemy{0}", i)}); 
 }

Upvotes: 1

Andrew Cooper
Andrew Cooper

Reputation: 32576

You can't really do what you're asking. If you really want to reference the objects by those names you could put them in a dictionary.

var enemies = new Dictionary<string, Enemy>();
for (int i = 0; i < ??? ; i++)
{
    enemies["enemy"+i.ToString()] = new Enemy(...);
}

Then you can reference them like this:

enemies["enemy1"].FireWeapon();

Upvotes: 0

Anthony Pegram
Anthony Pegram

Reputation: 126804

Variables cannot be dynamically named as you desire, but by using arrays or lists (but please don't use ArrayLists, more below!), you can access via indexes.

Create a List<Enemy> (List<T> is found in System.Collections.Generic)

var enemies = new List<Enemy>();
enemies.Add(new Enemy()); // add as many as you need

Or if the collection does not need to be dynamically sized, you could use a simple array, of which there are several legal syntaxes for creation.

Enemy[] enemies = { new Enemy(), new Enemy(), new Enemy() };

Then you can write your loop and access via the index

// enemies.Count if using List<Enemy>
// enemies.Length if using Enemy[] array
for (int index = 0; index < enemies.Count; index++) 
{
    enemies[index].FireWeapon();
}

Or write a foreach loop

foreach (Enemy enemy in enemies)
{
    enemy.FireWeapon();
}

Your notion about using an ArrayList is outdated as of C# 2.0+ (the current release on the market is C# 4.0), or Visual Studio 2005 (2010 is the current market version). It is preferrable to use the strongly typed generic collections found in the aforementioned System.Collections.Generic namespace.

Upvotes: 4

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112299

Add a constructor to your enemy class

public class Enemy
{
    public Enemy(string name, int score)
    {
        this.Name = name;
        this.Score = score;
    }

    public string Name { get; set; }
    public int Score { get; set; }
}

Fill enemy with

Enemy[] enemy = new Enemy[10];
for (int i = 0; i < enemy.Length; i++) {
    enemy[i] = new Enemy("Foe #" + i, 100);
}

or with

List<Enemy> enemy = new List<Enemy>();
for (int i = 0; i < 10; i++) {
    enemy.Add(new Enemy("Foe #" + i, 100));
}

You can also use an object initializer instead of consturctor parameters

var e = new Enemy{ Name = "X", Score = 100 };

Upvotes: 1

Chris
Chris

Reputation: 2895

Are you looking to name them or just create n enemy variables?

To create n variables use an array w/in your for loop:

EnemyArray[*n*]
for(int i =0; i< *n*; i++)
{
  EnemyArray[i] = new Enemy()
}

Now you can get the 1st Enemy like this

Enemy myEnemy = EnemyArray[0]

The second like this

 Enemy myEnemiesEnemy = EnemyArray[1]

And so on...

Upvotes: 0

ken2k
ken2k

Reputation: 48975

        List<Enemy> enemies = new List<Enemy>();
        for (int i = 0; i < N; i++)
        {
            enemies.Add(new Enemy() { Parameters... });
        }

        // Access to objects by index
        Enemy enemy2 = enemies[1];

Upvotes: 0

John Gardner
John Gardner

Reputation: 25116

you could do it like

List<Enemy> enemies = new List<Enemy> { 
    new Enemy(), 
    new Enemy(), 
    new Enemy() };

or even

List<Enemy> enemies = new List<Enemy>();
for (int i = 0; i < someNumberOfEnemiesDefinedElsewhere; i++)
{
    enemies.Add( new Enemy( "Enemy" + i ) );
}

otherwise, we need more details.

Upvotes: 0

Related Questions