Click here to Skip to main content
15,885,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone
i want to Generate number Variable with for loop or another technique. for example
i want to Generate 5 variable to Save int Values without to use Array or list or any Storage locations
and i guess this solution and i know it's not working :

C#
for(int i = 0; i < 5; i++)
{
    Console.Write(" ID {0} : ",i+1);
    int ID = Convert.ToInt32(Console.ReadLine()); // ???? i don't want to override ID and i want to generate different ID to save different Values 
}
Posted
Updated 27-Nov-12 6:54am
v4
Comments
Sergey Alexandrovich Kryukov 27-Nov-12 12:06pm    
Not clear what could be called "generation of variables". Perhaps, your very notion is based on some misconception. You should understand the difference between variable and actual object. You can create an object, but a variable you can declare, assign, etc... Essentially, there are no such thing as "variable" during runtime. What you write in the code does not make any sense. You need to explain your ultimate purpose.
--SA

So use an array:

C#
int[] ids = new int[5];

for(int i = 0; i < 5; i++)
{
    Console.Write(" ID {0} : ",i+1);
    ids[i] = Convert.ToInt32(Console.ReadLine());
}
 
Share this answer
 
Comments
OriginalGriff 27-Nov-12 12:11pm    
And if getting two almost completely identical answers at the same time doesn't convince him...:laugh:
:thumbsup:
ridoy 27-Nov-12 12:20pm    
+5
AlirezaDehqani 27-Nov-12 12:32pm    
thank you guys. but i want to Generate Variables without use of Storage locations
like Array or Collection or File. i just Generate Variables
lewax00 27-Nov-12 12:37pm    
Then you'll have to be a bit more specific because "generate variables" doesn't make any sense to me.
What you need is called an Array. It is the simplest collection of variables you can create:
C#
int[] IDs = new int[5];
for (int i = 0; i < IDs.Length; i++)
   {
   Console.Write("ID {0} : ", i + 1);
   IDs[i] = Convert.ToInt32(Console.ReadLine());
   } 
 
Share this answer
 
Comments
lewax00 27-Nov-12 12:12pm    
Great minds think alike? :P (A little cleaner than mine though.)
ridoy 27-Nov-12 12:21pm    
+5
Please see my comment to the question, which is probably based on some misconception. Probably you will be able to understand it from this comment. Consider this:

C#
int length = //...
int[] ids = new int[length];

for (int index = 0; index < length; ++index) {
    Console.Write(" ID {0}: ", index + 1);
    ids[index] = int.Parse(Console.ReadLine());
}


Array is just one way of having the objects (of the integer type, in this case) which are created, can be accessed, manipulated, but have no individual names, such as variables. Objects do not have to have individual names, and, in many cases, cannot have them. Other cases include collections, arbitrary object graphs linked by managed pointers (references), with linked lists or trees being simple example of such graphs.

—SA
 
Share this answer
 
v2
Comments
ridoy 27-Nov-12 12:21pm    
+5
Sergey Alexandrovich Kryukov 27-Nov-12 12:26pm    
Thank you,
--SA
lewax00 27-Nov-12 12:21pm    
I love how all three answers capitalize the name of the array differently, but all have the same name. But the use of a variable for the length is a good idea.
Sergey Alexandrovich Kryukov 27-Nov-12 12:25pm    
Thank you. Actually, my capitalization is a mistake as soon as this is a stack variable. I'll change it to lower case; the alternative would be to introduce the public or internal property, which would not add anything to the idea.
--SA
Instead of using an array, you can also use a List[^]:
C#
List<int> IDs = new List<int>();
for(int i = 0; i < 5; i++)
{
    Console.Write(" ID {0} : ",i+1);
    IDs.Add(Convert.ToInt32(Console.ReadLine()));
}

Now, you can get the first value of the List with IDs[0], you can get the second value of the List with IDs[1], ...
 
Share this answer
 
Comments
ali_reza_zareian 27-Nov-12 15:11pm    
What do u mean by generate variables?
AlirezaDehqani 28-Nov-12 4:29am    
i mean that define number variables of int type without use Array or list or any storage location. just define variables for assignment int values

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