Passing parameters in a class

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Is it better to use properties to pass parameters in a class, (as in Method 1 below) or not?


Method 1:

Sheet1:

Rich (BB code):
Private Sub UpdateTable()
    
    Dim MyUpdateTable As ClsUpdateTable
    
    Set MyUpdateTable = New ClsUpdateTable
    
    Set MyUpdateTable.Period = Me.Range("Now")
    
    Call MyUpdateTable.UpdateTable
    
End Sub


ClsUpdateTable

Rich (BB code):
Option Explicit

    Private pPeriod As Range
    
Public Property Get Period() As Range
    
    Set Period = pPeriod
    
End Property

Public Property Set Period(ByVal P As Range)
    
    Set pPeriod = P
    
End Property

Sub UpdateTables()

    Dim MyArray() As Variant
            
    MyArray = Array("1", "2", "3")
    
    Dim MyArrayElement As Variant
    
    For Each MyArrayElement In MyArray()
    
        Range(Period.Address).Value = MyArrayElement
                    
    Next MyArrayElement         

End Sub


Method 2:

Sheet1:

Rich (BB code):
Private Sub UpdateTable()
    
    Dim MyUpdateTable As ClsUpdateTable
    
    Set MyUpdateTable = New ClsUpdateTable
    
    Call MyUpdateTable.UpdateTable(Period:=Me.Range("Now"))
    
End Sub


ClsUpdateTable

Rich (BB code):
Option Explicit

Sub UpdateTables(ByRef Period As Range)

    Dim MyArray() As Variant
            
    MyArray = Array("1", "2", "3")
    
    Dim MyArrayElement As Variant
    
    For Each MyArrayElement In MyArray()
    
        Range(Period.Address).Value = MyArrayElement
                    
    Next MyArrayElement         

End Sub

Thanks
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I am not sure if there is much difference but I personally prefer to pass all the parameters in a single Method or Property like in Method 2 specially when the number of parameters is relatively small or/and when there are optional arguments ... It involves less code and more readable.
 
Upvote 0
Define "better".

I can't!

I am not sure if there is much difference but I personally prefer to pass all the parameters in a single Method or Property like in Method 2 specially when the number of parameters is relatively small or/and when there are optional arguments ... It involves less code and more readable.

When I was creating this, it occurred to me that IF I used method 1, there's never a need to write code such as (in a class):

Rich (BB code):
Sub UpdateTables(ByRef Period As Range)
Rich (BB code):

as in Method 2, ie to have to take in arguments because that's the purpose of using properties Get, Let and Set.
 
Last edited:
Upvote 0
VBA lacks constructors which make it neater, but I do it by state/scope. If the property alters the state or is used outside the scope of a function/sub then I use a property, otherwise it's an argument.
 
Last edited:
Upvote 0
Unless you use those variables for anything else in the class, I don't really see any benefit of making them separate properties, unless perhaps you need to perform some validation on them.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top