Class Module Method


M

Matthew Pfluger

I have a class module called CInertiaData that contains 6 double variable
declarations:

Public dmMass As Double
Public dmDensity As Double
Public dmVolume As Double
Public dmCOG_x As Double
Public dmCOG_y As Double
Public dmCOG_z As Double

I declare new instances of this module as follows:
Dim muInertiaData As CInertiaData
Set muInertiaData = New CInertiaData

At several points in my code, I want to reset all values in muInertiaData to
0. I'd like to write a method instead of a subroutine, but I'm not sure how
to proceed. I believe it would be something like:

Public Sub Clear(ByRef oInertiaData As CInertiaData)
On Error Resume Next
With oInertiaData
.dmMass = 0
.dmDensity = 0
.dmVolume = 0
.dmCOG_x = 0
.dmCOG_y = 0
.dmCOG_z = 0
End With
End Sub

However, this still requires passing a CInertiaData object to the method.
How do I write the method so it clears the CInertiaData object that calls it?
I would guess that this involves changing the way I define my Class Module
variables.

Thanks,
Matthew Pfluger
 
Ad

Advertisements

A

Andy Pope

Hi,

You would make the Clear routine part of the class module.

Public Sub Clear()

dmMass = 0
dmDensity = 0
dmVolume = 0
dmCOG_x = 0
dmCOG_y = 0
dmCOG_z = 0

End Sub

And then in code you would use it something like this.

Option Explicit

Dim muInertiaData As CInertiaData

Sub Test()

Set muInertiaData = New CInertiaData

With muInertiaData
.dmMass = 10.3
.dmDensity = 2
.dmVolume = 12

Debug.Print "Before Clear", .dmMass, .dmDensity, .dmVolume

.Clear

Debug.Print "After clear", .dmMass, .dmDensity, .dmVolume

End With

End Sub

Cheers
Andy
 
Ad

Advertisements

M

Matthew Pfluger

Wow, I thought I had to send a reference to an object in order to run the
method. Thanks for setting me straight, Andy!

Matthew Pfluger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top