Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 :
 
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 27-Nov-12 6:01am
Edited 27-Nov-12 6:54am
v4
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

So use an array:
 
int[] ids = new int[5];
 
for(int i = 0; i < 5; i++)
{
    Console.Write(" ID {0} : ",i+1);
    ids[i] = Convert.ToInt32(Console.ReadLine());
}
  Permalink  
Comments
OriginalGriff at 27-Nov-12 12:11pm
   
And if getting two almost completely identical answers at the same time doesn't convince him...:laugh:
:thumbsup:
ridoy at 27-Nov-12 12:20pm
   
+5
AlirezaDehqani at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

What you need is called an Array. It is the simplest collection of variables you can create:
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());
   } 
  Permalink  
Comments
lewax00 at 27-Nov-12 12:12pm
   
Great minds think alike? :P (A little cleaner than mine though.)
ridoy at 27-Nov-12 12:21pm
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
 
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
  Permalink  
v2
Comments
ridoy at 27-Nov-12 12:21pm
   
+5
Sergey Alexandrovich Kryukov at 27-Nov-12 12:26pm
   
Thank you,
--SA
lewax00 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Instead of using an array, you can also use a List[^]:
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], ...
  Permalink  
Comments
ali_reza_zareian at 27-Nov-12 15:11pm
   
What do u mean by generate variables?
AlirezaDehqani at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 645
1 OriginalGriff 335
2 CPallini 250
3 PIEBALDconsult 150
4 Fredrik Bornander 130
0 OriginalGriff 5,835
1 Sergey Alexandrovich Kryukov 5,198
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,470


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100