3

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?

2 Answers 2

12

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.

1
  • Thanks a lot! Both for the answer and for correcting my mistake of instance variables
    – Bouet
    Commented Dec 2, 2015 at 21:11
1

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 for StartEndTime 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.
6
  • 1
    there is no point in returning self in your year: method. That is the default return value if no other is provided. Dtto for initialize. Furthermore the question is tagged Pharo, which calls initialize automatically from new, so you are calling it twice. The comments do not provide any additional value and in year:'s case it's actually wrong/misleading. And last, but not least, StartEndTime shouldn't have year: method at all. Commented Dec 4, 2015 at 10:20
  • Yes - pressed 'save' too early. Still working on it.
    – Euan M
    Commented Dec 4, 2015 at 10:51
  • 1
    While it's true that self will be returned by default, there is no harm in making that return explicit. And in tutorial code, there is some benefit to be gained in terms of clarity and explicitness.
    – Euan M
    Commented Dec 4, 2015 at 10:55
  • Yes, the year: method is properly a method of Class Time
    – Euan M
    Commented Dec 4, 2015 at 10:58
  • 1
    @EuanM, you should leave tradition behind, because there is no added value in saying (on a comment) that #initialize returns an initialized instance of the class. In Smalltalk, that is so obvious (because is a pattern very old and completely integrated in ST culture) that you only would need to comment ONLY if it does anything else. Usually, comments are for things that are not obvious, hidden or surprising. Some tools enforce comments even for obvious methods, but there is no reason to do that for human readers Commented Dec 4, 2015 at 13:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.