• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by vozochris_1 · Dec 06, 2011 at 02:12 PM · c#variablestringint

string + int = variable

Is there a way I can declare a variable from a string and an int? I use C#.

Edit:

I need something like
string mystring = "Level";
int myint = 1;
int myintResult = 5;
int mystring+myint (must be -> "int Level1") = myintResult;

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jahroy · Dec 06, 2011 at 05:23 PM 0
Share

This is a great opportunity to learn about Arrays, Dictionaries, or Hashmaps.

Why are so many people asking this question lately?

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Bunny83 · Dec 06, 2011 at 02:55 PM

I'm not sure what you're after, but every variable need a type. It could be dynamic or static typed. Usually you should use static type variables so you have to decide what type you need / want. If the result should be a string you can just do this:

int myIntVar = 10;
string myNewString = "Some text " +  myIntVar;

this will automatically call myIntVar.ToString() which converts the integer into a string to be able to concat them.

If the result should be an int you have to convert / parse the string into an int. Something like that:

string myStringVar = "5";
int myIntVar = 10;
int myIntResult = int.Parse(myStringVar) + myIntVar;
// myIntResult wil contain 15

Be careful: the string must contain text that can be interpreted as number / integer.

If you think about declaring a variable name at runtime, that's something which isn't really supported by compiled languages. If you want to access a variable via a string identifier you might use a Hashtable / Dictionary.

edit

Here's an example how to use a dictionary to store a value / object / whatever under a unique string name.

If you want to store int values you would declare a generic dictionary like this:

Dictionary<string,int> myDict = new Dictionary<string,int>();

// To add a new "variable" use Add: myDict.Add("Level1",5);

// To read or write the stored int just do: int tmp = myDict["Level1"];

// or to set a new value: myDict["Level1"] = 20;

A Dictionary always have Key-value pairs. The key is unique for each dictionary, in our case a string. The value is stored along with the key. Dictionaries or HashTables are optimised for fast key-searching and therefore faster then doing it yourself with a List but always slower then using real variables.

Since the key is a string you can create it at runtime:

int currentLevel = 5;
myDict["Level" + currentLevel] = 123;

note: you can only access / use entries that have been added to the dictionary. You can test if a specific key exists with:

if (myDict.ContainsKey("Level25"))
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vozochris_1 · Dec 06, 2011 at 03:20 PM 0
Share

That's not what I need :( I need something like
string mystring = "Level";
int myint = 1;
int myintResult = 5;
int mystring+myint (must be -> "int Level1") = myintResult;

avatar image Bunny83 · Dec 06, 2011 at 04:12 PM 0
Share

Like i said in my last sentence you CAN'T declare variables at runtime. $$anonymous$$ono / .NET is a compiled language, not a dynamic scripting language. All identifiers have to be constant at compile-time.

Also as i already said in this case you might want to use a Dictionary.
I will add an example.

avatar image vozochris_1 · Dec 06, 2011 at 05:58 PM 0
Share

Thanks very much!!! I voted up your answer and marked it as correct.

avatar image
1

Answer by ptdnet · Dec 06, 2011 at 05:15 PM

a dictionary would be perfect for this

 Dictionary myDic = new Dictionary();
 
 for (int i = 0; i < 10; i++) {
     myDic.Add("Level" + i, 0);
 }
 
 // and later on ...
 myDic("Level4") = 392;
 // etc!

My C# above may have a few typos in it, but the general idea is there.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vozochris_1 · Dec 06, 2011 at 06:00 PM 0
Share

Thanks very much! I voted up your answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Converting a string to an int 2 Answers

Change multiple variables with function argument 2 Answers

Public variable hidden in the inspector 2 Answers

How to reference a variable in another script based on string in c# 0 Answers

Distribute terrain in zones 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges