CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2013
    Posts
    6

    Smile Creating a variable using a variable?

    Hi guys.
    Okay, so I am quite new to programming, and i was wondering if there is any way to create a variable using a variable in the name?
    So f.x. if you wanted to create an int named nr(x), and x was 1, you would get an int variable named nr1. How would you do this?

  2. #2
    Join Date
    Jan 2012
    Posts
    27

    Re: Creating a variable using a variable?

    Its possible, I dont know the EXACT code off by heart, but one way that I do it, is I make 2 variables, like bob, and nr, and then x, so id have like
    int bob = 0;
    int nr = x;
    int x = 1;
    so now, nr will always be 1, and now I can use bob to call x any time in my code
    Then you could even use a Setinel Loop for use of it for example... Hope that coulda helped, again sorry I cant provide the legit code to copy n paste

  3. #3
    Join Date
    Jun 2013
    Posts
    6

    Re: Creating a variable using a variable?

    Quote Originally Posted by Dibbie View Post
    Its possible, I dont know the EXACT code off by heart, but one way that I do it, is I make 2 variables, like bob, and nr, and then x, so id have like
    int bob = 0;
    int nr = x;
    int x = 1;
    so now, nr will always be 1, and now I can use bob to call x any time in my code
    Then you could even use a Setinel Loop for use of it for example... Hope that coulda helped, again sorry I cant provide the legit code to copy n paste
    I'm not sure if it's what i looked for, and i dont know what a Setinel Loop is (ill google it) , but thx for the try anyway

  4. #4
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: Creating a variable using a variable?

    Quote Originally Posted by akiel123 View Post
    Hi guys.
    Okay, so I am quite new to programming, and i was wondering if there is any way to create a variable using a variable in the name?
    So f.x. if you wanted to create an int named nr(x), and x was 1, you would get an int variable named nr1. How would you do this?
    I believe what you are trying to ask is if you can create an int called x as 1, and then create a new int called nr1 but using x in place of the 1. Well, if this is what you are asking then the answer is no. You can use a similar technique to call upon ints already created (ex: int nr1, nr2, nr3; ) but you cannot create them in this sense. Let me know if this is what you were asking for or if not, you have any other ways to clarify it.

  5. #5
    Join Date
    Jun 2013
    Posts
    6

    Re: Creating a variable using a variable?

    Quote Originally Posted by Mercifies View Post
    I believe what you are trying to ask is if you can create an int called x as 1, and then create a new int called nr1 but using x in place of the 1. Well, if this is what you are asking then the answer is no. You can use a similar technique to call upon ints already created (ex: int nr1, nr2, nr3; ) but you cannot create them in this sense. Let me know if this is what you were asking for or if not, you have any other ways to clarify it.
    Yea i think thats it - so nr[x] will be nr[whatever-x-is] upon creation :/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured