Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Passing args in a class

This is a discussion on Passing args in a class within the Excel Questions forums, part of the Question Forums category; Omitting it would be the same as adding ByRef. ByVal would have been clearer....

  1. #11
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    29,767

    Default Re: Passing args in a class

    Omitting it would be the same as adding ByRef. ByVal would have been clearer.

  2. #12
    Board Regular
    Join Date
    Apr 2013
    Posts
    435

    Default Re: Passing args in a class

    Thanks

  3. #13
    GTO
    GTO is offline
    MrExcel MVP
    Join Date
    Dec 2008
    Location
    Phoenix, Arizona
    Posts
    6,066

    Default Re: Passing args in a class

    @tiredofit:

    Sorry I seemed to misunderstand your question.

    @Rory:

    Quote Originally Posted by RoryA View Post
    But note that Property procedures in a class always pass ByVal regardless of what you specify.
    Probably logging out shortly (near coma-tired and looking forward to the joy of replacing a P/S gear box...) and not back until Sunday night, but wanted to ask before it falls out of my head...

    It makes sense to me that Property Set/Let would be a one-way (in only) deal, as we're trying to save it for later use. I am not understanding the difference if the property is in a class or standard module however?

    I only tested this...

    Standard Module (Module1)
    Code:
    Option ExplicitSub example()
    Dim lCnt As Long
    Dim cls1 As Class1
      Set cls1 = New Class1
      
      lCnt = 3
      
      cls1.Count = lCnt
      
      MsgBox "Class property returns " & cls1.Count & vbLf & "lCnt (passed byRef) returns " & lCnt
      
      Module2.MyProperty = lCnt
      
      MsgBox "Std Mod Property returns " & Module2.MyProperty & vbLf & "lCnt (passed byVal) returns " & lCnt
      
      Module2.MyOtherProperty = lCnt
      
      MsgBox "Std Mod Property returns " & Module2.MyOtherProperty & vbLf & "lCnt (passed byRef) STILL returns " & lCnt
      
    End Sub
    Class Module (Class1)
    Code:
    Option Explicit
    Private lCount As Long
    Public Property Get Count() As Long
      Count = lCount
    End Property
    Public Property Let Count(ByRef lNewValue As Long)
      lNewValue = lNewValue + 1
      lCount = lNewValue
    End Property
    Standard Module w/Properties (Module2)
    Code:
    Option Explicit
    Private lMyProperty As Long
    Private lOtherProperty As Long
    Public Property Get MyProperty() As Long
      MyProperty = lMyProperty
    End Property
    Public Property Let MyProperty(ByVal lNewValue As Long)
      lNewValue = lNewValue + 1
      lMyProperty = lNewValue
    End Property
    Public Property Get MyOtherProperty() As Long
      MyOtherProperty = lOtherProperty
    End Property
    Public Property Let MyOtherProperty(ByRef lSomeInt As Long)
      lSomeInt = lSomeInt + 1
      lOtherProperty = lSomeInt
    End Property
    ...what am I missing?

    Mark

  4. #14
    Board Regular Kyle123's Avatar
    Join Date
    Jan 2012
    Location
    Leeds, UK
    Posts
    2,188

    Default Re: Passing args in a class

    Properties behave the same in a module as a class module, or is that your point?
    Last edited by Kyle123; Apr 21st, 2017 at 12:24 PM.

  5. #15
    GTO
    GTO is offline
    MrExcel MVP
    Join Date
    Dec 2008
    Location
    Phoenix, Arizona
    Posts
    6,066

    Default Re: Passing args in a class

    Hi Kyle,

    Yes or specifically, if there is any difference in how the argument is passed to a property Let/Set in a Standard vs Class module.

    Mark

Page 2 of 2 FirstFirst 12

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com