Results 1 to 8 of 8

Thread: Passing Property as ByRef

  1. #1

    Thread Starter
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Passing Property as ByRef

    Hello,

    I'm attempting to pass a property to a Sub using ByRef.

    eg.

    VB Code:
    1. MySub Text1.Text
    2.  
    3. Public Sub MySub(ByRef sText As String)
    4.   sText = "Hello"
    5. End Sub

    But the Property doesn't change. Can VB not do this, or is it just me?

  2. #2
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Passing Property as ByRef

    You would have to do a function in order to do that

  3. #3

    Thread Starter
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Passing Property as ByRef

    i want to do it for more than one property. i should have said:

    VB Code:
    1. MySub Text1.Text, Text2.Text, Text3.Text, Text4.Text
    2.  
    3. Public Sub MySub(ByRef sText1 As String, ByRef sText2 As String, ByRef sText3 As String, ByRef sText4 As String)
    4.   sText1 = "This"
    5.   sText2 = "Is"
    6.   sText3 = "A"
    7.   sText4 = "Test"
    8. End Sub

  4. #4
    Hyperactive Member
    Join Date
    Feb 2003
    Location
    Grenada
    Posts
    346

    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:
    1. Dim sReturn as string
    2. MySub sReturn
    3. 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.

  5. #5

    Thread Starter
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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?

  6. #6
    Hyperactive Member
    Join Date
    Feb 2003
    Location
    Grenada
    Posts
    346

    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.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Passing Property as ByRef

    BUT... you can pass the TEXTBOX in byref and change the property in the sub.
    VB Code:
    1. MySub Text1, Text2, Text3, Text4
    2.  
    3. Public Sub MySub(ByRef sText1 As TextBox, ByRef sText2 As TextBox, ByRef sText3 As TextBox, ByRef sText4 As TextBox)
    4.   sText1.Text = "This"
    5.   sText2.Text = "Is"
    6.   sText3.Text = "A"
    7.   sText4.Text = "Test"
    8. End Sub

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Passing Property as ByRef

    Quote 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
  •  



Click Here to Expand Forum to Full Width