|
-
Dec 20th, 2005, 08:45 AM
#1
Passing Property as ByRef
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?
-
Dec 20th, 2005, 08:51 AM
#2
Fanatic Member
Re: Passing Property as ByRef
You would have to do a function in order to do that
-
Dec 20th, 2005, 08:55 AM
#3
Re: Passing Property as ByRef
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
-
Dec 20th, 2005, 09:07 AM
#4
Hyperactive Member
Re: Passing Property as ByRef
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
If my post has been helpful, then please rate it accordingly...
If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.
-
Dec 20th, 2005, 09:15 AM
#5
Re: Passing Property as ByRef
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?
-
Dec 20th, 2005, 09:21 AM
#6
Hyperactive Member
Re: Passing Property as ByRef
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...
If my post has been helpful, then please rate it accordingly...
If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.
-
Dec 20th, 2005, 09:23 AM
#7
Re: Passing Property as ByRef
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
-
Dec 20th, 2005, 09:28 AM
#8
Re: Passing Property as ByRef
 Originally Posted by bushmobile
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?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|