Results 1 to 8 of 8

Thread: [RESOLVED] Variable Automation by Reference?

  1. #1

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Resolved [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?
    Thanks
    J

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

  3. #3

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    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?

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

  5. #5

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    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..

  6. #6

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    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..

  7. #7

  8. #8

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    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
    Attached Files Attached Files

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