CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Nov 2005
    Posts
    95

    passing variable name rather than the variable

    There is a simple way to refer to the variable name rather than then variable value, but I can't remember or locate how to do it. I forget the term, somthing like convert the name to a string?

    such as:

    apple=20
    lemon=33
    banana=45

    send one of these names (call it "xfername")

    xfername=xfername +4
    msgbox (" increased to " & xfername)

    Note that what I desire is that if apple were the "sent one", it would have an updated final value of 24. If banana were sent, it would have a value of 49.

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: passing variable name rather than the variable

    Create a function like this.

    Code:
    Public Function AddTo(ByVal InitValue as Integer, ByVal Increment as Integer) As Integer
        AddTo = InitValue + Increment
    End Function
    Call it like this.

    Code:
        Apple = 20
        Apple = AddTo(Apple, 4)
    Last edited by sotoasty; January 4th, 2010 at 01:38 PM.

  3. #3
    Join Date
    Nov 2005
    Posts
    95

    Re: passing variable name rather than the variable

    I really need to have a variable or some function hold the name of the variable (apple, banana, etc) so that I can do some processing on it much later in the program. There is some decision logic early on to determine which variable is being updated later on.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: passing variable name rather than the variable

    Create a TYPE of your own, for FRUIT

    Code:
    Type SystemInfo
       CPU As Variant
       Memory As Long
       DiskDrives(25) As String   ' Fixed-size array. if you need one for each
       VideoColors As Integer
       Cost As Currency
       PurchaseDate As Variant
    End Type
    or create an array of them, and assign values

    Code:
    Dim AllSystems(1) As SystemInfo
    AllSystems(0).CPU = "386SX"
    AllSystems(0).DiskDrives(2) = "100M SCSI"

    You can pass procedure arguments using a user-defined type.

    Code:
    Sub FillSystem (SomeSystem As SystemInfo)
       SomeSystem.CPU = lstCPU.Text
       SomeSystem.Memory = txtMemory.Text
       SomeSystem.Cost = txtCost.Text
       SomeSystem.PurchaseDate = Now
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: passing variable name rather than the variable

    I think I understand what vbcandies wants. In fact everything was easy if we had pointers in VB. So we could store a pointer to a variable somewhere and know later which variable that was. However, you cannot pass the name of a variable to a function or sub, or store the name of a variable in a string.

    Unfortunately we haven't pointers and we can't really simulate them.

    What you can do is: you can implement an array (of fruit), wher fruit(0) is apples, fruit(1) is lemons, fruit(2) is bananas. Then you can store the index of the array element or pass it to a sub to make changes. If you assign constants you keep the overview in your program.
    The actual value is stored in an array, but you use the index to access it.
    Code:
    Const FRT_apple = 0
    Const FRT_lemon = 1
    Const FRT_banana = 2
    
    Private Fruit(2) As Integer
    
    Private LastFruit as Integer
    
    Private Sub ChangeFruit(FruitIndex as Integer, Increment as Integer)
      Fruit(FruitIndex) = Fruit(FruitIndex) + Increment
    End Sub
    
    'now you can do:
    
    ChangeFruit(FRT_apple, 10)
    LastFruit = FRT_apple
    ...later then
    ChangeFruit(LastFruit, -10)
    Another idea is, to create a simple class Fruit which contains one property 'Count'.
    The variables will be objects references which can be seen like pointers.
    You'd need a class module named 'Fruit'
    In the simplest way it contains only one line
    Code:
    Public Count As Integer 'or Long if you like
    In your main program you have to declare and use your objects like that:
    Code:
    Private apple As New Fruit
    Private lemon as New Fruit
    Private banana as New Fruit
    
    Private LastFruit as Fruit
    
    Private Sub ChangeFruit(FruitVar as Fruit, Increment as Integer)
      FruitVar.Count = FruitVar.count + Increment
    End Sub
    
    ChangeFruit(apple, -10)
    Set LastFruit = apple
    ... later then
    ChangeFruit(LastFruit, 20)

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

    Re: passing variable name rather than the variable

    If the names are known at design-time, you could use a Select Case statement:
    Code:
    Function GetVar(Name As String) As Long
    Select Case Name
      Case "Apple"
        GetVar = Apple
      Case "Lemon"
        GetVar = Lemon
      Case "Banana"
        GetVar = Banana
    End Select
    End Function
    So you could use it like:
    Code:
    Result = GetVar("Banana") + 4
    However, you can get a pointer to VB variables using VarPtr(), or StrPtr() for strings. Though this doesn't sound quite like what you're looking for.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: passing variable name rather than the variable

    I also considered VarPtr() in the first place, but then I found no means to pass it as a reference to a function or sub.
    You could store a VarPtr() in a Long but I didn't find a means to pass it to a sub, so as the sub would accept it as a reference to a variable.
    You would have to declare the parameter ByRef, but pass the VarPtr() variable ByVal
    Code:
    Dim apple as integer
    
    Sub ChangeFruit(ByRef FruitVar as Integer, Increment as Integer)
      FruitVar = FruitVar + Increment
    End Sub
    
    Dim LastFruit as Long
    LastFruit = VarPtr(apple)
    
    ChangeFruit ByVal LastFruit, -10
    Would be nice, but does not work.
    Sub

  8. #8
    Join Date
    Nov 2005
    Posts
    95

    Re: passing variable name rather than the variable

    Wiz bang--thanks, but your suggestion is sort of the reverse of what I want===I need to later on UPDATE the variable value (being banana, apple, etc) based on previously slecting which of the variables I want to update.

    I suppose DGLIENNA/WOF's idea is the only way to go..."problem" is I've already using a bunch of different variable names here & there in the code, so now I have to gather them up into a multidimensional array or TYPE!!
    Last edited by vbcandies; January 7th, 2010 at 12:51 AM.

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: passing variable name rather than the variable

    After considering all the possibilities I like the Object solution best.
    It allows closest what you want: store a pointer to a variable for later use, no matter which one it was.
    Arrays seem apropriate too, but all the changes seem more complicated than to write one simple one-line-class as I described.
    Change the variable declarations, and where the variables are used, you add the Value property (or Count).
    Code:
    Dim apple as New Fruit
    apple.Value = 10
    Dim FruitPointer as Fruit
    Set FruitPointer = apple
    Looks rather straight forward.

  10. #10
    Join Date
    Dec 2001
    Posts
    6,332

    Re: passing variable name rather than the variable

    Quote Originally Posted by WoF View Post
    I also considered VarPtr() in the first place, but then I found no means to pass it as a reference to a function or sub.
    You could store a VarPtr() in a Long but I didn't find a means to pass it to a sub, so as the sub would accept it as a reference to a variable.
    You would have to declare the parameter ByRef, but pass the VarPtr() variable ByVal
    Code:
    Dim apple as integer
    
    Sub ChangeFruit(ByRef FruitVar as Integer, Increment as Integer)
      FruitVar = FruitVar + Increment
    End Sub
    
    Dim LastFruit as Long
    LastFruit = VarPtr(apple)
    
    ChangeFruit ByVal LastFruit, -10
    Would be nice, but does not work.
    Sub
    Actually, CopyMemory can do the trick.
    Quote Originally Posted by vbcandies View Post
    Wiz bang--thanks, but your suggestion is sort of the reverse of what I want===I need to later on UPDATE the variable value (being banana, apple, etc) based on previously slecting which of the variables I want to update.

    I suppose DGLIENNA/WOF's idea is the only way to go..."problem" is I've already using a bunch of different variable names here & there in the code, so now I have to gather them up into a multidimensional array or TYPE!!
    Well, I'm partial to UDT arrays. Incredibly useful, and they also make it sooo easy to store and retrieve data from a file. If I understand what it is you want to do, attached is an example of three ways to do it.
    Attached Files Attached Files
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  11. #11
    Join Date
    Nov 2005
    Posts
    95

    Re: passing variable name rather than the variable

    WizBang:

    WOW---that is quite an impressive demo...thanks for the great ideas...there seems to be so many obscure and hidden functions to work with...not sure how you run across all this...I guess. you call it: Experience..

    I alway get great thoughts from everyone on this forum.

  12. #12
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: passing variable name rather than the variable

    Yes, good one, Wizbang. CopyMemory is always a way to workaround things VB6 does not have. But it requires you to know exactly what you are doing, because any wrong memory address would crash the program badly.

  13. #13
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: passing variable name rather than the variable

    or the OS, or the security system on the LAN...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  14. #14
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: passing variable name rather than the variable

    Yeah. That's why I'd rather use the object oriented approach. It is perfectly safe.
    What else are Class modules good for if we can achieve everything with CopyMemory.

  15. #15
    Join Date
    Dec 2001
    Posts
    6,332

    Re: passing variable name rather than the variable

    Fortunately, VarPtr() gives CopyMemory valid values every time. What's scary is when someone hard-codes values for such things.

    Class modules are great for various things too, and can do even more when used in conjunction with CopyMemory
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

Page 1 of 2 12 LastLast

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