Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class Modules: Property Set 2

Status
Not open for further replies.

JTBorton

Technical User
Jun 9, 2008
345
DE
I am trying to create a class module to handle thermodynamic table data and calculations for user selected chemicals. The class properties and procedures that I have already created worked fine, until I added the property that allows to user to select which chemical to use. I am recieving the following error:

[red]Complie Error:
Definitions of property procedures for the same property are inconsistent or contain optional parameters or a ParamArray
[/red]

Code:
[navy]Private[/navy] pCompound [navy]as String[/navy] [i](Previously defined)[/i]

[navy]Public Property Get[/navy] Compound() [navy]As String[/navy]
    Compound = pCompound
[navy]End Property[/navy]

[highlight][navy]Public Property Set[/navy] Compound(Value [navy]As String[/navy])[/highlight]
[navy]Dim[/navy] blnAcceptableCompound [navy]As Boolean[/navy]
    Trim (LCase(Value))
    blnAcceptableCompound = [navy]False[/navy]
    [navy]Do Until[/navy] blnAcceptableCompound = [navy]True[/navy]
        [navy]If Not[/navy] Value = "" [navy]And Not[/navy] Value = "ethylene" [navy]And Not[/navy] Value = "water" [navy]And Not[/navy] Value = "propylene" [navy]Then[/navy]
            Value = InputBox(Value & " was not recognized.  Only Water, Ethylene or Propylene have available " & _
            "thermodynamic table data.  Which compound would you like to specify?")
        [navy]Else[/navy]: blnAcceptableCompound = [navy]True
        End If
    Loop
    Call[/navy] PopulateThermoTable(Value)
    pCompound = StrConv(Value, vbProperCase)

[navy]End Property[/navy]

I'm not sure what I'm doing wrong, or even what this error means, and I did not understand what the Microsft Visual Basic Help was telling me either. Can someone explain this?

-JTBorton
Another Day, Another Disaster
 
Since a String type is not an object type variable, you should be using "Public Property Let" instead of "Public Property Set".
 
Oh hey, good eye sniper! That fixed it. okay so question: When I declare the variables for these user defined objects, such as the

Private pCompound as String

that I used above, are these variables only created when I create this class module in my code, such as

<code...>
dim ThermoTable as ThermoDynamicTable
set ThermoTable = New ThermoDynamicTable
<...code>

or are they defined and created when the excel sheet loads, similar to a public variable defined in a module. This is important to know because I need to manage my variables and memory usage with this program.

Thanks for all the help.

-JTBorton
Another Day, Another Disaster
 
You need to read up and fully understand Scope.
Private pCompound as String

that I used above, are these variables only created when I create this class module in my code, such as

If Private pCompound as String is declared in the class module, then yes, it is created only when you initialize the instance of the class module. Not only that, but as it is Private, it is in Scope only with procedures within the class module. In this case, that seems OK...unless you try and use it elsewhere.
or are they defined and created when the excel sheet loads, similar to a public variable defined in a module. This is important to know because I need to manage my variables and memory usage with this program.

Indeed! This is VERY important, and good on you for working towards managing your variables and memory usage.

ASIDE: the TGML code tags for code uses square brackets [ ] not angle brackets < >.


Gerry
 
A little bit further to what Gerry says: a Class is a model, a template, if you will. Nothing exists in a Class; the variables, methods, etc. are brought into existence when an Object, based on the Class, is instantiated. Each of the elements declared in the Class exists in each such Object.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
So at what point does it create and chew up memory, when I declare it

Code:
Dim ThermoTable as ThermoDynamicTable

Or when I set it?

Code:
Set ThermoTable = New ThermoDynamicTable

If it only begins chewing up memory when I set the variable and frees the memory when I set the variable to nothing,

Code:
Set ThermoTable = New ThermoDynamicTable
.
.
.
Set ThermoTable = Nothing

then I could declare a public ThermoTable once in a module and simply set it when needed. Then I could free the memory after each use. These thermo tables can become extremely large, so I need to restrict them to the specific times I need to use them. Would the method I stated above work?

Thanks so much for all the help.

-JTBorton
Another Day, Another Disaster
 
When you set it. The declaration simply reserves four bytes (or maybe a couple more as I suspect it will be aligned on a doubleword boundary) to hold a pointer to the object when it is instantiated by the Set. Objects are destroyed (and the memory freed) when all references to them are deleted so, if you only maintain one reference then the memory will be freed when you Set it to Nothing (or to another instance of the class).

In brief, yes, the method you propose is fine, with one proviso. Ordinary modules have a lifetime the same as their container (workbook) so the actual global pointer always exists and is not tidied up by normal housekeeping, so you must remember to explicitly set it to nothing to force the object to be destroyed. Typically, many coders forget this when they exit early due to some exceptional condition.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top