Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Indirect adressing of variables

Status
Not open for further replies.

Paulus

Programmer
Sep 25, 2000
16
0
0
A1
I´m looking for the possibility to address variables indirectly in VB6
I mean the following:

Dim Pos1 as integer
Dim Pos2 as integer
Dim Pos3 as integer
Dim a as integer

for a = 1 to 3
'Here I want to set the three Pos-variables
'The number of the variables depends on a
next a

In reality my program is a little bit more complicated.
The Pos-variables are set at an ASP file and the ASP variables are used in a VB-program

Hope you understand what I mean (my German is much better than my English)
Who can tell me how it works ?

 
About the only way you will be able to this is to pass an array of variables by reference.

The example below takes an array of Longs and adds 1 to each. By putting your variables into an array you can pass it into the public function. The array length is not fixed - LBound & UBound will find out how many there are. Passing them ByRef allows the original values to be updated - by default variables are passed ByVal which in effect passes a copy of the data. ByRef passes the address of the variable so it can be updated directly.

e.g.
Code:
Public Sub ProcessVars(ByRef Vars() As Long)
Dim A As Long
For A = LBound(Vars()) To UBound(Vars())
    Vars(A) = Vars(A) + 1
Next A
End Sub

Private Sub Test()
Dim Vars(3) As Long
Dim A As Long
Dim Result As String
Vars(0) = 1
Vars(1) = 2
Vars(2) = 3
Vars(3) = 4
ProcessVars Vars()
Result = ""
For A = 0 To 3
    If Result <> &quot;&quot; Then Result = Result & vbCr
    Result = Result & &quot;Vars(&quot; & A & &quot;) = &quot; & Vars(A)
Next A
MsgBox Result
End Sub
 
Here's an illustration of using the ScriptControl to achieve this sort of thing:
[tt]
Option Explicit
Private wsh As ScriptControl

Private Sub Command1_Click()
Setup
MsgBox wsh.Eval(&quot;opt1&quot;)
End Sub


Public Function Setup() As Long
Dim lp As Long
For lp = 1 To 3
wsh.ExecuteStatement &quot;opt&quot; & CStr(lp) & &quot; = &quot; & CStr(lp * 2)
Next
End Function

Private Sub Form_Initialize()
Set wsh = New ScriptControl
wsh.Language = &quot;vbscript&quot;
End Sub

Private Sub Form_Terminate()
Set wsh = Nothing
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top