Class objects, creating/destroying?

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
So I'm learning a little about class objects, and I defined my own class and wrote some code to set values and all that.

From what I understand, the way I have written my code below, the system doesnt actually create my class object BPFeature until the line
Code:
Set BPFeature = New ClaBPFeature
I might be wrong on that, but from what I've read thats the way I understand it to work.

So if thats when the object is created, I'm wondering, when does it get destroyed?

Do I need a line of code to destroy the object and release the memory it was using, or does it get destroyed automatically at the end of the subroutine in which it was created?

for example, in the subroutine below, does the object BPFeature still exist and is it still using memory when the subroutine ends? Because if thats the case, if I run this subroutine enough then I would just be wasting system memory...
Code:
Sub testClass()
Dim rngItem                 As Range
Dim BPFeature               As ClaBPFeature
 
Set rngItem = Intersect(Range("A:A"), Selection.Rows.EntireRow)
Set BPFeature = New ClaBPFeature
    With BPFeature
        .Item = rngItem.Value
        .InputDescription = rngItem.Offset(0, 1).Value
        .Nominal = rngItem.Offset(0, 2).Value
        .HighTolerance = rngItem.Offset(0, 3).Value
        .LowTolerance = rngItem.Offset(0, 4).Value
        .Method = rngItem.Offset(0, 5).Value
        .Zone = rngItem.Offset(0, 6).Value
    End With
    If ThisWorkbook.Sheets("INPUT").Range("H2").Value = "Metric" Then
        BPFeature.Metric = True
    Else
        BPFeature.Metric = False
    End If
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
It's is destroyed when the variable goes out of scope, which occurs at the end of the sub.
 
Upvote 0
Thank you shg, I thought that was how it worked because it dissapears out of my locals window when the sub ends, but i wasnt 100% sure.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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