ByVal and ByRef

rekam

Freshman
Joined
Oct 7, 2003
Messages
43
Location
Switzerland
Hello, I have a little problem of understanding with ByRef and ByVal. Look at this code :
Visual Basic:
Private Sub test( )

              Me.TextBox1.Text = "before"

              '(1) Show textBox1 content
              MsgBox(Me.TextBox1.Text)

              '(2) Call method
              Me.rere(Me.TextBox1)

              '(3) Show textBox1 content after the call
              MsgBox(Me.TextBox1.Text)

End Sub

Private Sub rere(ByVal t As System.Windows.Forms.TextBox)

              t.Text = "after"

End Sub


If passed with ByVal, after the call, the text should be "before" ?? Because it's not the reference which is passed....
Can someone explains me what's happening ?? Thanks !
 
So this is not possible to pass a control by val ? Well, my problem is when I change a value of a textbox in a class I created, it dosen't change anything. I saw I've passed the textbox ByVal instead of ByRef, but you guys tell me that a control is passed ByRef, no matter what.

So my problem is outhere. Thanks!
 
Try this:

Visual Basic:
Private Sub test( )

              Me.TextBox1.Text = "before"

              '(1) Show textBox1 content
              MsgBox(Me.TextBox1.Text)

              '(2) Call method
              Me.rere(Me.TextBox1.Text)

              '(3) Show textBox1 content after the call
              MsgBox(Me.TextBox1.Text)

End Sub

Private Sub rere(ByVal t As String)

              t = "after"

End Sub

I may be wrong on this, but I think when you pass controls, you pass a pointer to the controls memory, not the control itself (as you would a variable) so any changes affect the memory itself rather than the copy that byval would create. In my code, you pass one of the controls properties into the argument, which CAN be byval-ed as they follow the normal byval/byref rules. Hope this makes sense, it did to me at midnight...
 
Thank you all ! I resolve my problem...Well, it was not because ByVal or ByRef.
The fact is I have four TextBox. They only accpept value between 0 and 100. The fourth textBox is readonly, and its content depends on the three others.

So if I have 25, 10 and 25, the last textBox should have 40.

I do the "automatic content" when keypress on one of the three editable textBox...Hum, well, it's a bit complicated. I have a class which inherits of TextBox. Each of the four textBox is an instance of this class.

When an instance is created, I set 2 properties, which are :
- the "automatic textBox"
- the two other editable textBox

Then, when I change a value, the last is calculated automatically. My problem was that the value of the last textBox didn't change.

Now it's okay, but I don't really know why ;)
 
Back
Top