Click here to Skip to main content
15,885,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to have a new variable named after 2 other variables and I'm not sure how to do it in C#. The code below would create 5 variables named number1, number2, number3, number4, and number5 if it worked. The underlined int is where my visual studio says I'm receiving the error "Invalid expression term 'int'".

What I have tried:

int a = 1;
string number = "number";

for(int counter = 1; counter <=5; counter++)
{
    Console.Write("Please input a number: ");
    int (string.Concat(number,a)) = int.Parse(Console.ReadLine());
    a++;
}
Posted
Updated 17-Mar-18 9:55am

No, you cannot dynamically create variables with a dynamic name.

It looks like you want to take 5 integers from input and store them. Have you considered using an array[^] (or a List<int>[^])?
 
Share this answer
 
v2
Comments
Member 13689794 16-Mar-18 12:32pm    
I wanted the possibility for infinite variables to be created. The example I provided only allows 5. But, say for instance, what if you want the user to tell you how many numbers they want to input?
Thomas Daniels 16-Mar-18 12:34pm    
You can't dynamically create infinite variables but then again there isn't really a need for infinite variables as you can just store many elements in an array or List (I won't say "infinite" because practically, there are memory constraints).

If you use an array, you have to know the number of elements at the time of creation. Then indeed, you can ask the user how many elements they want to input.

If you cannot know the number of elements in advance, then go for a List (that's the second link I provided).
Maciej Los 17-Mar-18 15:35pm    
5ed!
Use an array or things like this if you know how many you want, or a List if you don't. Google "c# arrays" for examples\tutorials on how to use arrays.
 
Share this answer
 
Quote:
Is there anyway to name a variable using 2 variables?

Not with C#.
What you want to do is the reason why arrays exist.
 
Share this answer
 
Alternativelly to solution #1 by Thomas D [ProgramFOX][^] you can use a Dictionary[^], which enables you store the name of "variable" and its value. See:

C#
Dictionary<string, int> d = new Dictionary<string, int>();
for(int counter = 1; counter <=5; counter++)
{
    Console.WriteLine("Please input {0}. number: ", counter);
    int a = int.Parse(Console.ReadLine());
    d.Add("number" + counter, a);
}

Console.WriteLine();
foreach(var k in d.Keys)
{
	Console.WriteLine("Variable name: '{0}' | Value: {1}", k, d[k]);
}


Result:
Please input 1. number: 
Please input 2. number: 
Please input 3. number: 
Please input 4. number: 
Please input 5. number: 

Variable name: 'number1' | Value: 543
Variable name: 'number2' | Value: 34534
Variable name: 'number3' | Value: 67654
Variable name: 'number4' | Value: 22
Variable name: 'number5' | Value: 1
 
Share this answer
 
A quick google search on "C# adding number to string" reveals that all you have to do is use the '+' operator. string str = Console.ReadLine() + a;. It should be something like that, as I'm not yet proficient in C#.
 
Share this answer
 
Comments
Thomas Daniels 16-Mar-18 12:35pm    
The question isn't about string/int concatenation though, it's about creating variables with dynamic names :)
Richard Deeming 16-Mar-18 12:41pm    
The OP wants to create variables with dynamic names, not concatenate values to create a string.

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