[RESOLVED] Variable Automation by Reference?
Hey! Long time no posting here :) So here i go.
Im just want to pass a variables reference to a class, then im want to change the referenced variables value later, outside the subroutine there i passed the variable to.
Here is my idea, im seeking for any workaround.
Code:
'In a module:
Global myVarAutomated as long
'In a form:
Private clsObj as myClassObj
Private Sub Form_Load()
Set clsObj = new myClassObj
clsObj.Init myVarAutomated 'passing the reference
clsObj.IncValue 10 'Increase the value by 10
msgBox myVarAutomated 'Now at this point im want to display the value of the variable that increased before. :)
End Sub
The problem is in the class.
Code:
Private AutomatedValue 'as any, or what?
Sub Init(ByRef aVal)
Set AutomatedValue = aVal
End Sub
Sub IncValue(ByVal lVar as long)
AutomatedValue = AutomatedValue + lVar
End Sub
No it isnt works.. but how can i get it to work? :afrog:
Thanks
J
Re: Variable Automation by Reference?
Not sure I completely understand however it sounds like you want to pass class id (or prog id) to your class and from that class create object.
If so then look at the following sample:
Code:
'this goes to your form
Option Explicit
Private Sub Command1_Click()
'============================
Dim myObj As Object
Dim myProgID As String
Dim myClass As clsAutomation
myProgID = "Word.Application"
Set myClass = New clsAutomation
Set myObj = myClass.CreateMyObject(myProgID)
myObj.Visible = True
End Sub
'class module:
Option Explicit
Public Function CreateMyObject(strProgID As String) As Object
Dim obj As Object
On Error GoTo ErrHandler
Set obj = CreateObject(strProgID)
Select Case strProgID
Case "Word.Application"
'perhaps you need to open some template...
Case "Excel.Application"
'do whatever...
End Select
Set CreateMyObject = obj
Set obj = Nothing
Exit Function
ErrHandler:
MsgBox Err.Description
Err.Clear
Exit Function
End Function
You may expand/change sample code as you wish.
Re: Variable Automation by Reference?
Lol no its completely different :)
So basically im want to pass a reference of a variable to the class, then im want to modify the value of the variable in an another sub.
I think the only way this could work, if i store the reference id of the variable, or just bind an another variable to the referenced variable. So this way when i change the value of the variable in the class, that will set the value of the original variable also...
but how can i do it?
Re: Variable Automation by Reference?
If variable is passed ByRef (which is default in VB6) then you can change its original value:
Code:
'in nthe form:
Private Sub Command1_Click()
'============================
Dim myValue As String
Dim myClass As Class1
myValue = "abc"
Set myClass = New Class1
Debug.Print "myValue used to be " & Chr(34) & myValue & Chr(34)
myClass.Test myValue
Debug.Print "and now it is " & Chr(34) & myValue & Chr(34)
End Sub
'in the class
Public Function Test(ByRef a As String) As String
Test = a & "123"
a = "hello from class"
End Function
If you want to preserve original value then you need to pass variable ByVal.
Re: Variable Automation by Reference?
Yeah i know, but what if im want to change the value in a different sub (in class ) that couldnt get the reference of the variable? So is there any way to STORE the reference id, then modify the referenced variable later?..
eg:
'in module
Global myVar as Long
'in a form sub
myClass.Init myVar 'pass the reference
myClass.Set 10 'it modifies myVar.
Please recheck my first post, that might be helpful to understand my problem..
Re: Variable Automation by Reference?
.. or just how can i get the memory pointer of the variable, then how can i write there to modify its value..
Re: Variable Automation by Reference?
Visual Basic was not designed to support pointers.
However, to fill this hole MS decided to do something in VB5/6 just to bring down the noise I guess.
Have a look at this KB article: How To Get the Address of Variables in Visual Basic
1 Attachment(s)
Re: Variable Automation by Reference?
Aww sweet, varptr and strptr are exactly im looking for.
ive just played a bit with this copymemory API and it wors fine. Ive just did two little examples, that shows how is it works. So this way i can store the reference (memory pointer) of a string variable then i can modify its value from wherever and whenever i want.
However ive tried to build it into my app, then ive got some strange results. Sometimes it crashes the app, but sometimes give me wrong results... and it works only on a few times :)
Im not sure why. Do you have any ideas? Is this the answer? -> http://vbforums.com/showpost.php?p=2745456&postcount=2
Anyway.. Thanks for pointing me to the right direction.
J