Question

I have a problem with class inheritage - I have two classes defined like this:

Smalltalk defineClass: #Field
superclass: #{Core.Object}
indexedType: #none
private: false
instanceVariableNames: 'name type '
classInstanceVariableNames: ''
imports: ''
category: ''

and

Smalltalk defineClass: #CheckBox
superclass: #{Smalltalk.Field}
indexedType: #none
private: false
instanceVariableNames: 'checked '
classInstanceVariableNames: ''
imports: ''
category: ''

in class 'Field' I have a method setName:

setName: n
name := n.
^n

It works perfectly fine for something like this:

|tmp|
tmp := Field new.
tmp setName: 'fancy name'.

, but when I change 'Field' to 'CheckBox' in the above example I always get "Unhandled Exception: MessageNotUnderstood: #setName: ". Do you guys know how am I supposed to make my subclass inherit a setName: method from it's parent? I'm trying to find an answer everywhere, but no luck so far.

EDIT: Full error message is: Error message #1 Error message #2

Was it helpful?

Solution 3

Ok here's anwser to my question: I'm stupid. I was messing around with my CheckBox initialize method and accidently deleted ^self line at the end. After putting it back there everything works fine. Thanks for your help, David, and sorry for taking your time.

OTHER TIPS

Somehow your CheckBox class didn't get compiled. Check the spelling. Make sure that there's no existing class called CheckBox. Make sure you can see the CheckBox class in your browser.

Just on a lark - in your workspace window, go to the Variables tab and make sure you don't have a variable called CheckBox. If so, delete it and try again.

I cannot see the #initialize method on your Checkbox class,
but it was, for some reason, answering nil.

When you changed the last line, so it would answer self,
you also changed whatever had been causing the method to answer nil.

Removing the line that was answering nil would also have worked.

You can avoid this problem If you write your #new methods like this -

  >>new
      ^self basicNew
          initialize
          ; yourself

so you'll always get the new instance,
instead of like this --

  >>new
      ^super new initialize

whereby you always get whatever it is that
the #initialize method happens to answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top