Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello, I have a little problem of understanding with ByRef and ByVal. Look at this code :

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 !

Posted

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!

Posted

Try this:

 


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

Posted

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 ;)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...