Passing arguments to class initialise

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
We know it's not possible to pass arguments in a class in VBA and here is an attempt to overcome it:

Code:
https://stackoverflow.com/questions/15224113/pass-arguments-to-constructor-in-vba

However, instead of the above, can someone tell me what is wrong with this approach?

Declare a global variable and assign anything in the Class_Initialize to it. For example:

This is in Class1:

Code:
Option Explicit
 
Private Sub Class_Initialize()

    Dim abc As String

    abc = MySomeVariable.SomeVariable
    
End Sub

and this is in Class2:

Code:
Option Explicit

    Private pSomeVariable As String
    
Public Property Get SomeVariable() As String

    SomeVariable = pSomeVariable
    
End Property

Public Property Let SomeVariable(ByVal S As String)

    pSomeVariable = S
    
End Property

This is in a standard module:

Code:
Option Explicit

    Global MySomeVariable As Class2

Public Sub Test()
    
    Set MySomeVariable = New Class2
    
    MySomeVariable.SomeVariable = "Some string"
    
    Dim NewClass As Class1
    Set NewClass = New Class1
    
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
First, since you've declared your variable abc within the procedure Class_Initialize(), it has local scope. So when the procedure ends, the variable is not longer available. So you'll need to declared it at the module level, and then you'll need to create a Property Get procedure to retrieve the value.

So Class1 could be something like this...

VBA Code:
Option Explicit

Dim m_abc As String

Public Property Get abc() As String
    abc = m_abc
End Property
 
Private Sub Class_Initialize()

    m_abc = MySomeVariable.SomeVariable
   
End Sub

And the code for your regular module like this...

VBA Code:
Option Explicit

    Global MySomeVariable As Class2

Public Sub Test()
   
    Set MySomeVariable = New Class2
   
    MySomeVariable.SomeVariable = "Some string"
   
    Dim NewClass As Class1
    Set NewClass = New Class1
   
    MsgBox NewClass.abc, vbInformation
   
End Sub

However, with this approach, you're declaring a global variable, which in itself is not considered good programming practice. In addition, this approach seems somewhat convoluted, at least to me.

Personally, my preference would be the approach described in your link. ;)

Hope this helps!
 
Upvote 0
First, since you've declared your variable abc within the procedure Class_Initialize(), it has local scope. So when the procedure ends, the variable is not longer available. So you'll need to declared it at the module level, and then you'll need to create a Property Get procedure to retrieve the value.

So Class1 could be something like this...

VBA Code:
Option Explicit

Dim m_abc As String

Public Property Get abc() As String
    abc = m_abc
End Property
 
Private Sub Class_Initialize()

    m_abc = MySomeVariable.SomeVariable
  
End Sub

And the code for your regular module like this...

VBA Code:
Option Explicit

    Global MySomeVariable As Class2

Public Sub Test()
  
    Set MySomeVariable = New Class2
  
    MySomeVariable.SomeVariable = "Some string"
  
    Dim NewClass As Class1
    Set NewClass = New Class1
  
    MsgBox NewClass.abc, vbInformation
  
End Sub

However, with this approach, you're declaring a global variable, which in itself is not considered good programming practice. In addition, this approach seems somewhat convoluted, at least to me.

Personally, my preference would be the approach described in your link. ;)

Hope this helps!
Thanks for correcting my code.

Re the point of declaring a public variable, I consider it only "half public" because it's declaring it via a class and not directly.

Would you agree this is less bad?
 
Upvote 0
Actually, the public or global variable is not being declared via a class. It's in fact being declared in a regular module. It just so happens that it's being declared as Class2.

It seems to me that the approach outlined in your link is more straight forward, and a lot easier to follow. :)
 
Upvote 0
Actually, the public or global variable is not being declared via a class. It's in fact being declared in a regular module. It just so happens that it's being declared as Class2.

It seems to me that the approach outlined in your link is more straight forward, and a lot easier to follow.
Thanks for sharing your thoughts.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

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