Apologies for the newbie question, but I failed to figure this despite of long try.
I created a matrix class using NewClass feature in Cincom Visualworks.
Smalltalk.Core defineClass: #Matrix
superclass: #{Core.Object}
indexedType: #none
private: false
instanceVariableNames: 'rowCount columnCount cellValues '
classInstanceVariableNames: ''
imports: ''
category: ''
Added the following class method:
withRowCount: rowCount withColumnCount: columnCount withCellValues: cellValues
^self new rowCount: rowCount columnCount: columnCount cellValues: cellValues.
Added the following accessor methods:
cellValues
^cellValues
cellValues: anObject
cellValues := anObject
columnCount
^columnCount
columnCount: anObject
columnCount := anObject
rowCount
^rowCount
rowCount: anObject
rowCount := anObject
I have this code in workspace:
|myMatrix|
myMatrix := Matrix rowCount: 5 columnCount: 5 cellValues: 5.
Transcript show: (myMatrix rowCount).
But compiler says that message undefined. I guess my class method is not working as expected. Can someone please point out where I am going wrong?
First: Matrix
doesn't have a rowCount:columnCount:cellValues:
method. You probably meant Matrix withRowCount: 5 withColumnCount: 5 withCellValues: 5
.
Second, I'm thinking methods return the value of the last expression. So chaining methods doesn't work quite like that. (And even if it did, that still looks like one message.)
Your class method should probably read like
withRowCount: rowCount withColumnCount: columnCount withCellValues: cellValues
| newMatrix |
newMatrix := self new.
newMatrix rowCount: rowCount;
columnCount: columnCount;
cellValues: cellValues.
^newMatrix
The ;
breaks up the messages and tells Smalltalk to send all three to newMatrix
.
Then you can use it like
|myMatrix|
myMatrix := Matrix withRowCount: 5 withColumnCount: 5 withCellValues: 5.
Transcript show: (myMatrix rowCount).