Reputation: 69
I am trying to access a class variable in one of my classes in Smalltalk.
I have two classes: Class1 and Class2.
Class1 has the following variables: year month day hour minute. Class2 has the following variables: start-time end-time. In the initialize method for Class2 I have the following:
start-time := Class1 new.
end-time := Class1 new.
Now I want to assign the year 2012 to start-time, how do I access the year variable in the Class1 object start-time?
Upvotes: 3
Views: 5175
Reputation: 10217
Since you are sending new
message to the classes I will assume that you are interested in instance variables, and not class variables (shared variables) (see Pharo Object Model in Updated Pharo By Example to understand the differences).
In Pharo all class/instance variables are private, thus the way to access them is to create accessors.
Add to your Class1 methods
Class1>>year
^ year
Class1>>year: aYear
year := aYear
And then you can send the message to the class with the appropriate value:
Class2>>initialize
startTime := Class1 new.
startTime year: 2012.
"or by using a cascade"
startTime := Class1 new
year: 2012;
yourself.
If for whatever reason you needed to access a variable without accessors, you can use metaprogramming:
startTime instVarNamed: #year "meta-getter"
startTime instVarNamed: #year put: 2012 "meta-setter"
Finally, 'start-time'
is not a valid variable name.
Upvotes: 12
Reputation: 1136
I am trying to access a class variable in one of my classes in Smalltalk.
Are you sure that you want Class variables in this case? A Class variable (or attribute is held once and only once. It is accessible to all the instances of that Class, and all the instances of all it's sub-classes, as well as being accessible to the sub-classes themselves.
If what you want is to spawn many of the objects, each noting a different time, or startTime and endTime, then you need to use the more ordinary instance variables.
If however, you want to store one time, and only one-time, then yes, you can store the information in the Class itself.
I have two classes: Class1 and Class2.
I'll call Class1 "Time" and I'll call Class2 "StartEndTime"
Time has the following variables:
year month day hour minute
.StartEndTime
has the following variables:startTime endTime
. In the initialize method forStartEndTime
I have the following:
startTime := Time new. endTime := Time new.
Now I want to assign the year 2012 to startTime, how do I access the year variable in the object startTime?
The convention is to name getter accessor methods with the same name as the attribute. In which case the Time object instances will have a year
getter method, which returns the year of the Time object.
startTime year
would then return the variable named year
Similarly, setter accessor methods have the same name as their attribute, but are suffixed with a ':
'
startTime year: 2012
would then set the variable named year
to 2012
.
Putting these into an initialize
method would mean:
StartEndTime >> initialize
"Returns an initialized StartEndTime"
startTime := Time new.
endTime := Time new.
^self
Time >> year: anInt
"Returns receiver with its year set to the year argument."
year := anInt.
^self
In the Workspace (or Playground)
"create a new StartEndTime instanceobject"
aStartEndTime := StartEndTime new initialize.
aStartEndTime startTime: 2012.
Upvotes: 1