rekam Posted November 24, 2003 Posted November 24, 2003 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 ! Quote
Moderators Robby Posted November 24, 2003 Moderators Posted November 24, 2003 Controls are passed by reference Quote Visit...Bassic Software
Raven76 Posted November 24, 2003 Posted November 24, 2003 try this instead... Private Sub rere(ByVal t As String) t ="after" End Sub Quote
Administrators PlausiblyDamp Posted November 24, 2003 Administrators Posted November 24, 2003 That wouldn't compile because he's passing a textbox in and rere has now been set to accept a string. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
rekam Posted November 24, 2003 Author Posted November 24, 2003 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! Quote
Darc Posted November 25, 2003 Posted November 25, 2003 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... Quote
rekam Posted November 25, 2003 Author Posted November 25, 2003 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 ;) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.