Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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
However, it is not gamedev related, please read the FAQ. – Maik Semder Nov 7 '12 at 21:43
One usually uses an Array instead. – Markus von Broady Nov 7 '12 at 21:50
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

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

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)

If this is new to you, I'd suggest reading up on C# arrays. Here are a couple of links to get you started: and

share|improve this answer

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