Member Avatar for stow19

Hi all,

I've been stuck on this problem for a couple days and not really sure if there's a way to do it but... I'm trying to dynamically name arrays for a game. So if someone types in the console 2 people are playing I want two arrays to be made - one that is named P1 and the second P2. Is this possible and if so, how so? I've tried a couple of ways but I get an error saying that the variable cannot be reused for array types.

for (int i = 0; i < playerNames.Length; i++)
{
   String name = "p" + i;
   string[] name = new string[];
}

playerNames is just an array of playerNames(i.e. P1,P2,P3,P4,etc.)


Thanks

Recommended Answers

All 2 Replies

Hi Stow19, welcome here at Daniweb.

Use something like this:

string[] names = new string[playerNames.Length];

for (int i = 0; i < playerNames.Length; i++)
{
   names[i] = "p" + i.ToString();
}
Member Avatar for stow19

Hi ddanbe,

Thanks for your help that was the centre piece of my code :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.