ByRef Error !? 
Author Message
 ByRef Error !?

Hi!
In an VB-Project  i am using a sub with one byref argument and one class
with one public attribute.
My problem:
    Private Sub Test(ByRef s as string)
        s="Hello World"
    End Sub

    Private Sub Command1_Click
        Dim c as new Class1
            c.MyString="blabla"
            Test c.MyString
            MsgBox c.MyString
    End Sub

Result: MsgBox is showing "blabla" instead of "Hello World"   WHY??? Was
there something wrong in my Compiler-Lecture some years ago??

Holger Gubbels
stud softwareengineering



Tue, 07 Oct 2003 23:16:53 GMT  
 ByRef Error !?
Hi,

When you pass the C.MyString in the parameter you are just calling a
property GET, so that the Test routine correctly sets the value of s, but
there is no associated LET so the classes private member stays the same
internally (ie it's still the original string).  If you don't understand
this try single stepping through the code and you'll set that the classes
internal data is never set anywhere.

David Crump


Quote:
> Hi!
> In an VB-Project  i am using a sub with one byref argument and one class
> with one public attribute.
> My problem:
>     Private Sub Test(ByRef s as string)
>         s="Hello World"
>     End Sub

>     Private Sub Command1_Click
>         Dim c as new Class1
>             c.MyString="blabla"
>             Test c.MyString
>             MsgBox c.MyString
>     End Sub

> Result: MsgBox is showing "blabla" instead of "Hello World"   WHY??? Was
> there something wrong in my Compiler-Lecture some years ago??

> Holger Gubbels
> stud softwareengineering



Tue, 07 Oct 2003 23:34:15 GMT  
 ByRef Error !?


Quote:
> Hi!
> In an VB-Project  i am using a sub with one byref argument and one class
> with one public attribute.
> My problem:
>     Private Sub Test(ByRef s as string)
>         s="Hello World"
>     End Sub

>     Private Sub Command1_Click
>         Dim c as new Class1
>             c.MyString="blabla"
>             Test c.MyString

When VB executes that line it's the same as if you had written:
     tempstring=c.MyString
     Test tempstring
In other words, it gets the property value to pass to the procedure but does
not put it back on return.  If you want to update the property you have to
do it explicitly:
     Dim s As String
     s=c.MyString
     test s
     c.MyString=s

Note that VB.Net will change this so that it works the way you expected.

Quote:
>             MsgBox c.MyString
>     End Sub

> Result: MsgBox is showing "blabla" instead of "Hello World"   WHY??? Was
> there something wrong in my Compiler-Lecture some years ago??



Tue, 07 Oct 2003 23:35:05 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. ByRef Error

2. ByRef Error

3. VB5: ByRef Error when adding user-defined type to Collection

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

5. ByRef or not ByRef, that is the array?

6. Property Let argument ByRef is NOT ByRef ?

7. Getting a Compile Error: ByRef argument type mismatch

8. ByRef Type Mismatch Error

9. Cursor Error sending Report Object to ActiveX DLL ByRef

10. DLL call error : ByRef argument type mismatch

11. sub examplefunc(byref var1,byref var2,byref var2) doesnt work!

12. ByRef Argument Type Mismatch

 

 
Powered by phpBB® Forum Software