none
How to refer to a variable name within code

    Question

  • Hi. Here's a simplified version of my problem:

        Sub test() 
            Dim myvar1 As Int16 = 1001 
            MsgBox(String.Concat("myvar", "1")) 
        End Sub 

    What I'm trying to do is refer to the variable contents of myvar1, not a new string called "myvar1". How do I tell msgbox to print "1001" instead of "myvar1"? I know that strint.concat isn't right, but I want to know how to refer to a variable by concatenating a prefix (myvar) with a suffix (1). I guess this is a variable naming problem, I don't know the conventions for ammending variable names.

    Thank you.


    Monday, July 14, 2008 11:28 AM

Answers

  • It wouldn't use more memory...unless you copied them. And using reflection is never efficient.

    What if you load them as needed...instead of an array of images, you could use an array of file paths. Then randomly load an image using the file path array.

    Or, you could embed the images as a resource and load from there.




    Monday, July 14, 2008 8:31 PM
  • Another option is to use an ImageList.

    Chris
    Monday, July 14, 2008 8:55 PM

All replies

  • Try it like this:
        Sub test() 
            Dim myvar1 As Int16 = 1001 
            MsgBox(myvar1.ToString & "1")) 
        End Sub 


    hope it helps

    Rick
    Monday, July 14, 2008 1:12 PM
  • You can't dynamically create a variable name. How would the compiler know what to do with that?

    What are you trying to accomplish?
    Monday, July 14, 2008 1:47 PM
  • [_Joe_] said:

    You can't dynamically create a variable name.



    What?


    Rick
    Monday, July 14, 2008 2:31 PM
  • I am not sure I am totally clear on what you are trying to accomplish, but with what I understand, it seems like a simple solution would be to use an array as your variable. Then you would be using myvar(1) (or whatever)...instead of trying to create a variable.

    Thx...

    Monday, July 14, 2008 2:40 PM
  • Are you trying to do something like what's being asked on this Thread:
    http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/ced0c344-33ef-4bc1-a9ad-bfd272f5dfdb


    Rick
    Monday, July 14, 2008 2:43 PM
  • Rick1 said:

    [_Joe_] said:

    You can't dynamically create a variable name.



    What?


    Rick


    A variable name is declared before it is compiled.  He wants to dynamically build the variable name with a string.








    Monday, July 14, 2008 4:27 PM
  • Yep, sorry Joe, you're right. After realizing that I sent the previous post.
    Rick
    Monday, July 14, 2008 5:18 PM
  • Thanks guys. Let me explain the problem:

    I've got 50 image objects - pic1, pic2.....pic50

    I want to choose one of them at random by concatenating the prefix 'pic' with a random integer (1 to 50). That way I could call up something like picbox.image = pic + randint

    I realise I could put all the images into an array and call an element of that array with a random integer. But I don't want an array of image objects, I want to know how to dynamically reference a variable name. There must a way. In python I use the eval() function to parse a string into a var name:

        pic.show( eval("pic" + str(randint) ) )

    Thanks. J
    • Edited by JohnTul Monday, July 14, 2008 7:33 PM ammend
    Monday, July 14, 2008 7:28 PM
  • You could use reflection (System.Reflection).  But, this is not good design IMO.

    What's wrong with using an array? Or, you could use a generic collection.

    Dim images as new Generic.List(of Image)




    Monday, July 14, 2008 7:44 PM
  • Since I've already created 50 image objects, won't loading them all into an array use even more memory? Wouldn't it be more efficient to call each one dynamically? Thanks for your suggestion though, I'll look into it, though off the top of my head I don't know how it would be implemented.
    Monday, July 14, 2008 7:53 PM
  • It wouldn't use more memory...unless you copied them. And using reflection is never efficient.

    What if you load them as needed...instead of an array of images, you could use an array of file paths. Then randomly load an image using the file path array.

    Or, you could embed the images as a resource and load from there.




    Monday, July 14, 2008 8:31 PM
  • Another option is to use an ImageList.

    Chris
    Monday, July 14, 2008 8:55 PM