|
-
Feb 26th, 2011, 11:00 PM
#1
Thread Starter
New Member
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.
-
Feb 27th, 2011, 12:06 AM
#2
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
-
Feb 27th, 2011, 06:17 PM
#3
Thread Starter
New Member
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.
-
Feb 27th, 2011, 08:20 PM
#4
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
-
Feb 27th, 2011, 08:21 PM
#5
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
 
-
Feb 27th, 2011, 10:05 PM
#6
Thread Starter
New Member
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.
-
Feb 27th, 2011, 11:14 PM
#7
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
 
-
Feb 28th, 2011, 08:00 AM
#8
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
-
Feb 28th, 2011, 08:11 AM
#9
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
-
Feb 28th, 2011, 08:19 AM
#10
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)?
-
Feb 28th, 2011, 08:32 AM
#11
Re: Dynamically accessing Variables
 Originally Posted by cicatrix
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|