Scope of variables in class

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,798
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
In Module1, I have:

Code:
Global MyFlag As ClsFlag

In ClsVariables, I have:

Code:
Set MyFlag = New ClsFlag

MyFlag.Flag = False

The problem is in Module2, it does not regonise MyFlag has already been set, ie:

Code:
If MyFlag.Flag Then 'FAILS HERE

How can I overcome this?

Do I have to add:

Code:
Set MyFlag = New ClsFlag

in Module2?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
At what point did you create an instance of clsVariables?
 
Upvote 0
You'll have to post the full code. What you've got here should work.
 
Upvote 0
You'll have to post the full code. What you've got here should work.

Module1:

Code:
Option Explicit
Global RatesTable() As Variant
Sub Start()
    Set MyVar = New ClsVariables
    
    With MyVar
    
        .StartingDate = Range("StartDate")
        
        RatesTable = wksInput.Range("RatesTable").CurrentRegion.Value
        
        .DateToCalc = Range("DateToCalc")
        
        Range("EarlierDate") = .EarlierDate
        
        If MyFlag.Flag Then
        
            Range("LaterDate") = "Date not applicable"
        
            Range("EarlierRate") = "Date not applicable"
            
            Range("LaterRate") = "Date not applicable"
            
            Range("CalcRate") = "Date not applicable"
            
            Set MyVar = Nothing
            
            Exit Sub
            
        End If
        
        Range("LaterDate") = .LaterDate
        
        Range("EarlierRate") = .EarlierRate
        
        Range("LaterRate") = .LaterRate
        
        Range("CalcRate") = CalcRate
        
    End With
    Set MyVar = Nothing
            
End Sub

Module2:

Code:
Option Explicit
Global MyVar As ClsVariables
Global MyFlag As ClsFlag

ClsFlag:

Code:
Option Explicit
    
    Private mboolFlag As Boolean
    
Public Property Get Flag() As Boolean
    Flag = mboolFlag
End Property
Public Property Let Flag(ByVal Fla As Boolean)
    mboolFlag = Fla
End Property

ClsVariables:

Code:
Option Explicit
Private mdtStartingDate As Date
Private mdtDateToCalc As Date
Private msngRateCalc As Single
Private mdtEarlierDate As Date
Private msngEarlierRate As Single
Private mdtLaterDate As Date
Private msngLaterRate As Date
Dim Counter As Integer
Public Property Get StartingDate() As Date
    StartingDate = mdtStartingDate
End Property
Public Property Let StartingDate(ByVal SDate As Date)
    mdtStartingDate = SDate
End Property
Public Property Get DateToCalc() As Date
    DateToCalc = mdtDateToCalc
End Property
Public Property Let DateToCalc(ByVal DToCalc As Date)
    
    mdtDateToCalc = DToCalc
    
End Property
Public Property Get RateCalc() As Single
    
    RateCalc = msngRateCalc
    
End Property
Public Property Get EarlierDate() As Variant
    
    Set MyFlag = New ClsFlag
    
    MyFlag.Flag = False
    
    Counter = 2
    
    On Error GoTo ErrDate
    
        Do Until mdtDateToCalc <= RatesTable(Counter, 2)
        
            Counter = Counter + 1
        
        Loop
        
        Select Case Counter
            
            Case Is = 2
            
ErrDate:
        
                MyFlag.Flag = True
                
                EarlierDate = "Date not applicable"
                
    On Error GoTo 0
                
            Case Else
        
                EarlierDate = RatesTable(Counter - 1, 2)
        
        End Select
    
    Set MyFlag = Nothing
    
End Property
Public Property Get EarlierRate() As Single
    EarlierRate = RatesTable(Counter - 1, 3)
    
End Property
Public Property Get LaterDate() As Date
    LaterDate = RatesTable(Counter, 2)
End Property
Public Property Get LaterRate() As Single
    LaterRate = RatesTable(Counter, 3)
End Property
 
Last edited:
Upvote 0
In your clsVariables GetEarlierDate property you have this:
Code:
Set MyFlag = Nothing

hence your problem. I suspect you need to rethink your structure as the classes don't seem particularly self contained.
 
Upvote 0
In your clsVariables GetEarlierDate property you have this:
Code:
Set MyFlag = Nothing

hence your problem. I suspect you need to rethink your structure as the classes don't seem particularly self contained.

Thanks for spotting the Set = nothing.

What exactly do you mean the classes are not particularly self-contained (I'm still learning)?

I'm using them to avoid publically declaring variables, so that there's some control over what's set. Is that not a good use of classes?
 
Upvote 0
Well, you still have to declare the class instances as Public ('Global' is actually a throwback to VB4, IIRC) , so you're not really avoiding it. ;)

What I mean is that you have two distinct objects declared but your code will only work if you instantiate them in a particular order. I don't really know why you have a separate object for the Flag - it looks very much like using classes purely for the sake of using classes, not for a good design reason. Of course that's fine as a training exercise, as long as you know why you are doing this.
 
Last edited:
Upvote 0
Well, you still have to declare the class instances as Public ('Global' is actually a throwback to VB4, IIRC) , so you're not really avoiding it. ;)

What I mean is that you have two distinct objects declared but your code will only work if you instantiate them in a particular order. I don't really know why you have a separate object for the Flag - it looks very much like using classes purely for the sake of using classes, not for a good design reason. Of course that's fine as a training exercise, as long as you know why you are doing this.

You've hit the nail on the head! I am using it to get a feel for classes and of course for something small like this, it's a bit of an overkill.

I have noticed one minor difference between Global and Public - you can't declare a variable as Global in a class but you can declare it as Public. I think the same applies in a worksheet module.
 
Upvote 0
A worksheet is a class. ;)
 
Upvote 0

Forum statistics

Threads
1,203,601
Messages
6,056,211
Members
444,850
Latest member
dancasta7

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