Hello,
I'm attempting to pass a property to a Sub using ByRef.
eg.
VB Code:
MySub Text1.Text Public Sub MySub(ByRef sText As String) sText = "Hello" End Sub
But the Property doesn't change. Can VB not do this, or is it just me?
Printable View
Hello,
I'm attempting to pass a property to a Sub using ByRef.
eg.
VB Code:
MySub Text1.Text Public Sub MySub(ByRef sText As String) sText = "Hello" End Sub
But the Property doesn't change. Can VB not do this, or is it just me?
You would have to do a function in order to do that
i want to do it for more than one property. i should have said:
VB Code:
MySub Text1.Text, Text2.Text, Text3.Text, Text4.Text Public Sub MySub(ByRef sText1 As String, ByRef sText2 As String, ByRef sText3 As String, ByRef sText4 As String) sText1 = "This" sText2 = "Is" sText3 = "A" sText4 = "Test" End Sub
The code won't work because you cannot change the property in text1 neither pass it as a reference...
Pluss you have no linkage between sText and Text1.text...
So try this...
VB Code:
Dim sReturn as string MySub sReturn Text1.Text = sReturn
thanks, i figured i would, was trying to avoid extra variables.
Out of curiosity, why can't the property be passed ByRef? Does it not have a memory allocation that ByRef can point to, or something?
Technically yes...
The Text property of a control is basically a reference...
When you try to use Byref on it, you are basically trying to reference a reference...
BUT... you can pass the TEXTBOX in byref and change the property in the sub.
VB Code:
MySub Text1, Text2, Text3, Text4 Public Sub MySub(ByRef sText1 As TextBox, ByRef sText2 As TextBox, ByRef sText3 As TextBox, ByRef sText4 As TextBox) sText1.Text = "This" sText2.Text = "Is" sText3.Text = "A" sText4.Text = "Test" End Sub
-tg
Think about how a class works. The actual variable is wrapped by functions that allow you to access it (ie. the Let and Get). So when you set something equal to Text1.Text in your code you are calling function that returns a value. Likewise, when you set Text1.Text to something, you are passing the assignment to a function as an argument. Allowing an external routine to change the value of the wrapped variable would defeat the purpose of having objects, as the object would no longer have control over it's interface.Quote:
Originally Posted by bushmobile