DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Martin Rydman Guest

    Passing properties ByRef


    Hi Gurus!

    Here I was, thinking I knew pretty much the ins and outs of VB, but nooo...Can
    somebody explain this:

    Private Sub Form_Load()
    Dim x As New Class1

    Dim p As Integer

    myset x.x
    myset p
    End Sub

    Sub myset(ByRef y)
    y = 5
    End Sub

    Class1 contains a simple Public x as Integer. When running this code, p is
    being changed by the call to myset, but x.x is not. Why?

    TIA
    /Martin

  2. #2
    Mattias Sjögren Guest

    Re: Passing properties ByRef

    >Class1 contains a simple Public x as Integer. When running this code, p is
    >being changed by the call to myset, but x.x is not. Why?


    Hej Martin,

    When you add a Public member variable of a class, what really happens
    is that VB creates Public Get and Let methods for you (check the
    generated interface on a public class in OLE View). So you could look
    at it like this:

    ' --- Class1 ---

    Private m_x As Integer

    Public Property Get x() As Integer
    x = m_x
    End Property

    Public Property Let x(new_x As Integer)
    m_x = new_x
    End property

    And this property will of course return a copy of the internal value.
    It is this copy that will be changed inside myset() and then thrown
    away.


    Mattias

    __________________________________________________
    Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    VB+ http://hem.spray.se/mattias.sjogren/
    Please send questions/replies to the newsgroups

  3. #3
    Martin Rydman Guest

    Re: Passing properties ByRef


    Hej själv, Mattias, och tack!

    Just to get this clear in my head. Passing ByRef is *not* an assignment,
    just passing a reference intead of a copy. Thus, Property Let is *not* called
    (which was obvious once I run it through the debugger). Got it now...

    /Martin


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links