Visualworks Cincom Smalltalk Class Variable Destruction

204 views Asked by At

How can we initialize a class variable in Visualworks Smalltalk and destroy it after its use? I want to know about ClassVariables. NOT ClassInstanceVariables.

I am implementing Singleton pattern and here is my code

MyClass class>> aClasMethod
         aClassVariable isNil ifTrue:[
               aClassVariable := 'I am a variable'.
         ]
         ^aClassVariable.

Once the variable is created, I am not able to destroy it. i.e Next time I run my code, I see that the class variable is retaining its previous value. How can I avoid this?

I tried this: MyClass allInstances do: [:inst | inst become: nil ]. But of no use.

2

There are 2 answers

0
David Buck On BEST ANSWER

The best way is simply to add a class method to set the class variable to nil and then call it whenever it's an appropriate time to clear it. I do this all the time with the Singleton pattern.

0
Harshil Shah On

i think we usually use singleton pattern so that we only have one object. I don't know why do you want to store a string value in the class variable. Try to store a object so you will have only one copy of that specific object and store values in the instance variables. so that specific object has only some specific values. Then in future if u need to update the values stored in instance variable then make one more method updateValues and do everything in that.