Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am hoping to save lines of code by concatenating a counter ( 0 to 9 ) with the first part of a variable name (num) to identify num0, num1, num2, etc in a loop using XNA. Can I do this ?

share|improve this question

closed as off topic by Maik Semder, doppelgreener, Josh Petrie, Jari Komppa, bummzack Nov 8 '12 at 7:38

Questions on Game Development Stack Exchange are expected to relate to game development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Why Would you want to save code lines? Code is for humans, not for computers, it must be readable. –  Maik Semder Nov 7 '12 at 21:41
1  
However, it is not gamedev related, please read the FAQ. –  Maik Semder Nov 7 '12 at 21:43
2  
One usually uses an Array instead. –  Markus von Broady Nov 7 '12 at 21:50
1  
You should likely be using an array (or better variable names), but Maik is right. You have no reason to fret about how many lines of code there are. You're not designing for microchips, this isn't 1980, and you don't have to handle punch cards. This is also a programming question for which a game developer won't provide a better answer than a regular programmer, so this belongs on Stack Overflow rather than here. –  doppelgreener Nov 7 '12 at 22:55

1 Answer 1

It's a bit hard to be sure without more context, but it sounds like you should be using an array rather than many individual variables. For example:

int[] numbers = { 2, 4, 6, 8, 10 };       // Create an array of 5 integers
for (int i = 0; i < numbers.Length; i++)
{
    // The square brackets allow you to access individual elements by index
    System.Console.WriteLine(numbers[i]); 
}

However, if your loop is simply iterating over all the elements in your array, it would be cleaner to use the foreach statement:

foreach (int num in numbers)
{
    System.Console.WriteLine(num); 
}

If this is new to you, I'd suggest reading up on C# arrays. Here are a couple of links to get you started: http://msdn.microsoft.com/en-us/library/9b9dty7d.aspx and http://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.