Passing arguments byref to a class 
Author Message
 Passing arguments byref to a class

I've included some class code here and
some event code to call a method in the
class.  This behaves differently than I would
expect, though, because the variable in the
event code is not changed.  How do you
call a method in a class and return modified
variables?  I think a function would work, but
what if I want to return more than one variable?
Can I accomplish something like this with a
Sub?

Thanks,

'MyClass Class

Option Explicit

Public Sub MySub(ByRef strMyvar As String)
    strMyvar = "Testing"
End Sub

Option Explicit

Private Sub Command1_Click()
    Dim strmylocal As String
    Dim myclassinstance As MyClass
    Set myclassinstance = New MyClass
    myclassinstance.MySub (strmylocal)
    Set myclassinstance = Nothing
    MsgBox strmylocal
End Sub



Mon, 10 Apr 2000 03:00:00 GMT  
 Passing arguments byref to a class

Kevin,
If you pass your arguments in parenthesis, they
will always pass by value

Example:

by reference

myclassinstance.MySub strmylocal
call myclassinstance.MySub (strmylocal)

by value

myclassinstance.MySub (strmylocal)
call myclassinstance.MySub ((strmylocal))

Quote:

>I've included some class code here and
>some event code to call a method in the
>class.  This behaves differently than I would
>expect, though, because the variable in the
>event code is not changed.  How do you
>call a method in a class and return modified
>variables?  I think a function would work, but
>what if I want to return more than one variable?
>Can I accomplish something like this with a
>Sub?

>Thanks,

>'MyClass Class

>Option Explicit

>Public Sub MySub(ByRef strMyvar As String)
>    strMyvar = "Testing"
>End Sub

>Option Explicit

>Private Sub Command1_Click()
>    Dim strmylocal As String
>    Dim myclassinstance As MyClass
>    Set myclassinstance = New MyClass
>    myclassinstance.MySub (strmylocal)
>    Set myclassinstance = Nothing
>    MsgBox strmylocal
>End Sub



Tue, 11 Apr 2000 03:00:00 GMT  
 Passing arguments byref to a class

On Thu, 23 Oct 1997 06:09:00 -0700, "Kevin Kuhlwein"

Quote:

>I've included some class code here and
>some event code to call a method in the
>class.  This behaves differently than I would
>expect, though, because the variable in the
>event code is not changed.  How do you
>call a method in a class and return modified
>variables?  I think a function would work, but
>what if I want to return more than one variable?
>Can I accomplish something like this with a
>Sub?

>Thanks,

>'MyClass Class

>Option Explicit

>Public Sub MySub(ByRef strMyvar As String)
>    strMyvar = "Testing"
>End Sub

>Option Explicit

>Private Sub Command1_Click()
>    Dim strmylocal As String
>    Dim myclassinstance As MyClass
>    Set myclassinstance = New MyClass
>    myclassinstance.MySub (strmylocal)
>    Set myclassinstance = Nothing
>    MsgBox strmylocal
>End Sub

personally I don't like sending variables byref to accomplish this.
Why not return an array or collection of values?

Jack



Wed, 12 Apr 2000 03:00:00 GMT  
 Passing arguments byref to a class

On Thu, 23 Oct 1997 06:09:00 -0700, "Kevin Kuhlwein"

Quote:

>I've included some class code here and
>some event code to call a method in the
>class.  This behaves differently than I would
>expect, though, because the variable in the
>event code is not changed.  How do you
>call a method in a class and return modified
>variables?  I think a function would work, but
>what if I want to return more than one variable?
>Can I accomplish something like this with a
>Sub?

>Thanks,

>'MyClass Class

>Option Explicit

>Public Sub MySub(ByRef strMyvar As String)
>    strMyvar = "Testing"
>End Sub

>Option Explicit

>Private Sub Command1_Click()
>    Dim strmylocal As String
>    Dim myclassinstance As MyClass
>    Set myclassinstance = New MyClass
>    myclassinstance.MySub (strmylocal)
>    Set myclassinstance = Nothing
>    MsgBox strmylocal
>End Sub

Personally I don't like passing vars byref to accomplish this.  Why
not return an array/collection of values?

Jack



Wed, 12 Apr 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Pass arguments ByVal or ByRef

2. Passing arguments ByRef from VBScript to COMAdmin component

3. Passing arguments ByRef to Script Control 1.0

4. Property Let argument ByRef is NOT ByRef ?

5. Passing integer value between two classes as ByRef

6. Is it possible to pass class types as arguments to a function

7. Passing Arguments to a Class when a instace is created

8. Passing arguments when instantiating VB class

9. Public Method in a class calling a private method in same class yields ByRef error

10. Arguments : Passing Arguments containing <">

11. ByRef Argument Type Mismatch

12. ByRef Argument Type mismatch

 

 
Powered by phpBB® Forum Software