Results 1 to 11 of 11

Thread: Dynamically accessing Variables

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    3

    Dynamically accessing Variables

    Hi,

    I have a script where I make separate routines with identical code

    Example:
    Code:
    sub AddToA {
    ATotal += 1
    }
    
    sub AddToB {
    BTotal += 1
    }
    What I would like to do is have 1 routine and dynamically set the variable I'm adding to
    Similar to:
    Code:
    sub AddOneToTotal {
    (A/B)Total += 1
    }
    Essentialy the (A/B) in the 2nd block is a variable that I set in another function.

    Any pointers would be appreciated.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Dynamically accessing Variables

    You could use "ByRef" in a sub so it acts on the variable you pass to it, is that what you want?

    Code:
    Public Class Form1
        Dim i As Integer
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            AddOneToIntVar(i) ' add one to this integer variable
            Debug.Print(i.ToString)
        End Sub
    
        Sub AddOneToIntVar(ByRef TheVar As Integer)
            TheVar += 1
        End Sub
    
    End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    3

    Re: Dynamically accessing Variables

    I was hoping to avoid subscripting since I'm already doing subscripting with those values. I really didn't want to float the multi-dimensional array boat if I didn't have to.

    I would really prefer to build a variable name from a couple strings instead of subscripting.

    ATotal($sub) instead of Total($sub1, $sub2)

    If I could index an Array off of a letter instead of a number, that would probably work.

    Doing the multi-dimensional arrays is an option, it just means being more state aware regarding where I'm at in the code.

    I had even considered doing some kludge with an Excel Spreadsheet for data management, but that would even have its drawbacks.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Dynamically accessing Variables

    Who said any thing about arrays? Or subscripting?

    Perhaps if you explain what you are trying to do in better detail, some one may be able to offer a better solution.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Dynamically accessing Variables

    What do you mean be subscripting? You have some kind of basic misconception going on there. What Edgemeal did was create a sub that takes an argument rather than one that takes no argument. There isn't an array involved at all, so there isn't any subscripting.

    Indexing an array off of a number doesn't make any sense, under the hood, and probably isn't what you want to do anyways, buy you could certainly make something that looked like an array with letters as a subscript, it would be a Dictionary. The letters would be the key in the key-value pair.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    3

    Re: Dynamically accessing Variables

    Essentially what I have is 2 buttons that execute the same logic (Example code above), but on different variables. What I'm trying to do is consolidate them and only have 1 reference of the same logic (2nd code block above), but be able to figure out what variables to access. I'm trying to avoid using if/then/else or select/case because I'm essentially rearranging the deck chairs by doing that. What I'm referring to as rearranging the deck chairs is that I'm doing the same code, just in a different format.

    Maybe I'm trying to be too clever with the code and trying to do something that is either completely insane or just not possible with VB.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Dynamically accessing Variables

    The two buttons can have the same click event handler, but at some point you have to have a condition (an If statement) because at some point the code will not be quite the same. You end up with two choices. The first choice is to have different code for the two different buttons, which is what you showed in version 1. The second choice is to have a conditional switch, which is what Edgemeal suggested. This has nothing to do with the language, as it will be true in any language. At some point, the code has to know which variable it is to use unless one set of code uses one variable and the other set of code uses the other variable.

    There is a very rare case where you can avoid this, but it will only work for some simple addition or subtraction and booleans. The case is so rare that it is hardly worth mentioning, but I figured I might as well be complete. In that rare case, you are actually performing both operations, but one has no effect on the result, and which one has no effect depends on the variables themselves. It's also not worth doing in .NET.
    My usual boring signature: Nothing

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Dynamically accessing Variables

    Code:
        Class someTotal
            Private _Value As Integer
            Public Property Value() As Integer
                Get
                    Return _Value
                End Get
                Set(ByVal value As Integer)
                    _Value = value
                End Set
            End Property
        End Class
    
        Dim ATotal, BTotal As New someTotal
    
        Private Sub someButton_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                              Handles Button1.Click, Button2.Click
    
            Dim b As Button = CType(sender, Button)
            CType(b.Tag, someTotal).Value += 1
    
            'proof
            Debug.WriteLine("A " & ATotal.Value.ToString)
            Debug.WriteLine("B " & BTotal.Value.ToString)
    
        End Sub
    
        Private Sub Form1_Shown(ByVal sender As Object, _
                                ByVal e As System.EventArgs) Handles Me.Shown
            Button1.Tag = ATotal
            Button2.Tag = BTotal
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Dynamically accessing Variables

    I think Edgemeal has your solution. I will just try to clarify post #2. It sounds like it is exactly what you need.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            AddTo(ATotal)
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            AddTo(BTotal)
        End Sub
    
        Private Sub AddTo (ByRef Variable As Integer)
            Variable += 1
        End Sub
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  10. #10
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Dynamically accessing Variables

    Maybe I didn't understand what exactly qsecofr wanted, but wouldn't a Dictionary collection fit the purpose of accessing a certain value by its name (or key)?

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Dynamically accessing Variables

    Quote Originally Posted by cicatrix View Post
    Maybe I didn't understand what exactly qsecofr wanted, but wouldn't a Dictionary collection fit the purpose of accessing a certain value by its name (or key)?
    Sure would

    Code:
        Dim totals As New Dictionary(Of String, Integer)
    
        Private Sub someButton_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                              Handles Button1.Click, Button2.Click
    
            Dim b As Button = CType(sender, Button)
            Dim key As String = CStr(b.Tag)
            totals(key) += 1
    
    
            'proof
            Debug.WriteLine("A " & totals("ATotal"))
            Debug.WriteLine("B " & totals("BTotal"))
    
        End Sub
    
        Private Sub Form1_Shown(ByVal sender As Object, _
                                ByVal e As System.EventArgs) Handles Me.Shown
            Button1.Tag = "ATotal"
            Button2.Tag = "BTotal"
            totals.Add(CStr(Button1.Tag), 0)
            totals.Add(CStr(Button2.Tag), 0)
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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