Public Group

# simple c# assigning variable's help

This topic is 2527 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hey,
im teaching myself c# with the use of tutorials and making simple games. The latest question i need answering is this,
Is it possible to use a variable within a variable name? for example

 for (int i=1; i<5 ;i++) { int eval(myvar+i) = i; } Console.WriteLine("myvar1 should be 1 and it is{0} ",myvar1); Console.WriteLine("myvar2 should be 2 and it is{0} ",myvar2); //and so on

also is there any kinda chat room I can goto as most of my questions would be simple for real programmers to answer?

some of you may ask why im doing it this way and not in an array, that would be too hard for me to explain at the moment because mainly I cant get the array back to a different class but normal variables I can.(remember im new)!

thanks

##### Share on other sites
this eval is new to me.

##### Share on other sites
its all new to me haha! but if you have to remove eval part and is it possible?

##### Share on other sites
You mentioned
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

### some of you may ask why im doing it this way and not in an array, that would be too hard for me to explain at the moment because mainly I cant get the array back to a different class[/font][/quote] Assuming you are using an ArrayList you can cast it back to whatever you want. ArrayList v = new ArrayList(); v.Add(1); v.Add(2); v.Add(4); int v1 = (int)v[1];  Same way for any other types, including classes. If you want to avoid casting but have a static number of elements, a standard array can be helpful. string[] hello = {"Hello", "World"}; string world = hello[1];  so on The code you posted is, well, it makes no sense at all.

##### Share on other sites

some of you may ask why im doing it this way and not in an array, that would be too hard for me to explain at the moment because mainly I cant get the array back to a different class but normal variables I can.(remember im new)!

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

### [/quote][/font] I think this is your actual question, can you explain this is in more detail? What exactly is the problem you are having. As thisgamesux said, the code you posted seems non-nonsensical. You seem to be trying to store multiple variables in a single variable, which in fact is an array. There is no reason for you to not be able to return an array, hence why I believe that is the root of your real question. /EDIT: Forums been weird for me lately, changed my font size for no apparent reason, plus my cursor has been jumping for no apparent reason.

##### Share on other sites
ok ill go to my real question then rather than my work around. ive tried to simplify my code and please remember im new

 class ShowMap { public static string maptile4; public static string[] mapline1; public static void Displaymap() { Console.WriteLine("THIS IS MY MAP"); Console.ReadLine(); CreatMap(); Console.WriteLine("{0} {1} {2} and {3}", mapline1[0], mapline1[1], mapline1[2], maptile4); } static void CreatMap() { Console.WriteLine("or is it?"); string[] mapline1 = { "howdy", "DUDE", "x", "x", "x", "x", "x", "x", "x", "x", "x", }; maptile4 = "XXX"; }

Long story short
i can get CreatMap() to pass the single variable maptile4 back to Displaymap(). but i cant get it to pass the array back to Displaymap().

##### Share on other sites
remove the string[] from the mapline1 in CreateMap.

And you're not passing the arrays/variables anywhere. You're re-using the ones in the ShowMap class. I'll let others explain why this works (or I will once I get out of this flashpoint)

##### Share on other sites
im not passing them anywhere now because i couldn't figure it out
[size=2] (this bit isnt important any more but, in my main game class i was using public static void make(ShowMap allmap) and then fetching the variables with allmap.maptile4 this like i said worked fine but the arrays just wouldn't cross over, so i tried this way inside the ShowMap class and still couldn't get it to work)
anyway i had tried to remove the string part before and multiple other ways. but here is an example of what i can and cant do with the info.
(hope it makes sense)

 class ShowMap { public static string maptile4; public static string[] mapline1; public static string[] mapline2 = new string[1]; /* removed part */ static void CreatMap() { Console.WriteLine("or is it?"); string[] mapline1 = { "howdy", "DUDE", "x", "x", "x", "x", "x", "x", "x", "x", "x", }; //doesnt send mapline1 = { "howdy", "DUDE", "x", "x", "x", "x", "x", "x", "x", "x", "x", }; //invaild expression mapline2[0] = "hey"; //works but i didnt want to hardcode asi wanted the map to change size etc maptile4 = "XXX"; //works fine (where my work around idea came from) }

##### Share on other sites
That's because
 mapline1 = { "howdy", "DUDE", "x", "x", "x", "x", "x", "x", "x", "x", "x", }; 
isn't valid since the initializer only works when initializing things. You need to add new string[] (or whatever the new array syntax is... I don't remember; arrays are near useless) before the curly braces.

##### Share on other sites
You only need to declare variables once by the way, you seem to declare mapline1 at least twice.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633715
• Total Posts
3013494
×