Help! initialize event

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Help! initialize event

Sunit Joshi
Hello All
I'm trying to work thru. the Dolphin Companion book. I have created
the Animal Class and set the getter-setter methods for name,
foundationYear instance variables.
In the class, I add the initialise method as
initialize
        (self new) foundationYear:0; name:String new.

But when I inspect an := Animal new, I see that the vars are not
initialized. This may be stupid, but any ideas what I'm doing wrong
here.

thanks
Sunit
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Help! initialize event

Jonathon Spaeth
Sunit

In my experience, when deriving a class from Object directly, one has to
override the class method new if one wants to automatically call the
initialize method.  For example:

Animal>>new
    ^super new initialize.

The initialize instance method would then look like:

Animal>>initialize
    self foundationYear: 0;
        name: String new.

The instance method initialize should not call new, rather the other way
around.  The class method new calls the instance method initialize, which
should then create the required instance variables.

Hope that helps,
Jon


"Sunit Joshi" <[hidden email]> wrote in message
news:[hidden email]...

> Hello All
> I'm trying to work thru. the Dolphin Companion book. I have created
> the Animal Class and set the getter-setter methods for name,
> foundationYear instance variables.
> In the class, I add the initialise method as
> initialize
> (self new) foundationYear:0; name:String new.
>
> But when I inspect an := Animal new, I see that the vars are not
> initialized. This may be stupid, but any ideas what I'm doing wrong
> here.
>
> thanks
> Sunit
> [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Help! initialize event

Stefan Schmiedl
On Tue, 10 Sep 2002 15:44:28 GMT,
Jonathon Spaeth <[hidden email]> wrote:
> Sunit
>

It just took me about five minutes to figure out
that you are not talking about SUnit ... time for
a nap, I guess.

s.


Reply | Threaded
Open this post in threaded view
|

Re: Help! initialize event

Sunit Joshi
In reply to this post by Jonathon Spaeth
Thanks for the info. I still get the same thing.
My new is now ^super new initialize
and initialize class method now
^(self new) foundationYear:0; name:String new

Since the vars are instance variables, I thought I should have the new
message sent first to get the instance object.

Sunit
[hidden email]

"Jonathon Spaeth" <[hidden email]> wrote in message news:<wrof9.612$[hidden email]>...

> Sunit
>
> In my experience, when deriving a class from Object directly, one has to
> override the class method new if one wants to automatically call the
> initialize method.  For example:
>
> Animal>>new
>     ^super new initialize.
>
> The initialize instance method would then look like:
>
> Animal>>initialize
>     self foundationYear: 0;
>         name: String new.
>
> The instance method initialize should not call new, rather the other way
> around.  The class method new calls the instance method initialize, which
> should then create the required instance variables.
>
> Hope that helps,
> Jon
>
>
> "Sunit Joshi" <[hidden email]> wrote in message
> news:[hidden email]...
> > Hello All
> > I'm trying to work thru. the Dolphin Companion book. I have created
> > the Animal Class and set the getter-setter methods for name,
> > foundationYear instance variables.
> > In the class, I add the initialise method as
> > initialize
> > (self new) foundationYear:0; name:String new.
> >
> > But when I inspect an := Animal new, I see that the vars are not
> > initialized. This may be stupid, but any ideas what I'm doing wrong
> > here.
> >
> > thanks
> > Sunit
> > [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Help! initialize event

Don Rylander-3
It looks as if you're have the #initialize method on the class side.  If you
do, that's the problem.  Assuming I understand what you're trying to do, the
class method #new should look like:

new
     ^super new initialize

The words "super new" create the new instance.  The word "initialize" is a
message sent to that instance, so the #initialize method needs to be on the
instance side.  Because it's for an instance that already exists, it should
simply be:

initialize
    self
        foundationYear: 0;
        name: String new

Hope this helps,

Don

"Sunit Joshi" <[hidden email]> wrote in message
news:[hidden email]...

> Thanks for the info. I still get the same thing.
> My new is now ^super new initialize
> and initialize class method now
> ^(self new) foundationYear:0; name:String new
>
> Since the vars are instance variables, I thought I should have the new
> message sent first to get the instance object.
>
> Sunit
> [hidden email]
>
> "Jonathon Spaeth" <[hidden email]> wrote in message
news:<wrof9.612$[hidden email]>...

> > Sunit
> >
> > In my experience, when deriving a class from Object directly, one has to
> > override the class method new if one wants to automatically call the
> > initialize method.  For example:
> >
> > Animal>>new
> >     ^super new initialize.
> >
> > The initialize instance method would then look like:
> >
> > Animal>>initialize
> >     self foundationYear: 0;
> >         name: String new.
> >
> > The instance method initialize should not call new, rather the other way
> > around.  The class method new calls the instance method initialize,
which

> > should then create the required instance variables.
> >
> > Hope that helps,
> > Jon
> >
> >
> > "Sunit Joshi" <[hidden email]> wrote in message
> > news:[hidden email]...
> > > Hello All
> > > I'm trying to work thru. the Dolphin Companion book. I have created
> > > the Animal Class and set the getter-setter methods for name,
> > > foundationYear instance variables.
> > > In the class, I add the initialise method as
> > > initialize
> > > (self new) foundationYear:0; name:String new.
> > >
> > > But when I inspect an := Animal new, I see that the vars are not
> > > initialized. This may be stupid, but any ideas what I'm doing wrong
> > > here.
> > >
> > > thanks
> > > Sunit
> > > [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Help! initialize event

Jonathon Spaeth
In reply to this post by Sunit Joshi
Sunit

The vars being instance variables, the initialize method should be an
instance method, as well.  Ocassionally, I see a class method named
initialize which serves the same purpose for class variables.  However in
the statement:

super new initialize.

The initialize message is being sent to the newly-created instance, as an
instance method.  You're right that new should be sent before initialize,
but the initialize method you should be writing is in the instance, not the
class.

Jon

"Sunit Joshi" <[hidden email]> wrote in message
news:[hidden email]...

> Thanks for the info. I still get the same thing.
> My new is now ^super new initialize
> and initialize class method now
> ^(self new) foundationYear:0; name:String new
>
> Since the vars are instance variables, I thought I should have the new
> message sent first to get the instance object.
>
> Sunit
> [hidden email]
>
> "Jonathon Spaeth" <[hidden email]> wrote in message
news:<wrof9.612$[hidden email]>...

> > Sunit
> >
> > In my experience, when deriving a class from Object directly, one has to
> > override the class method new if one wants to automatically call the
> > initialize method.  For example:
> >
> > Animal>>new
> >     ^super new initialize.
> >
> > The initialize instance method would then look like:
> >
> > Animal>>initialize
> >     self foundationYear: 0;
> >         name: String new.
> >
> > The instance method initialize should not call new, rather the other way
> > around.  The class method new calls the instance method initialize,
which

> > should then create the required instance variables.
> >
> > Hope that helps,
> > Jon
> >
> >
> > "Sunit Joshi" <[hidden email]> wrote in message
> > news:[hidden email]...
> > > Hello All
> > > I'm trying to work thru. the Dolphin Companion book. I have created
> > > the Animal Class and set the getter-setter methods for name,
> > > foundationYear instance variables.
> > > In the class, I add the initialise method as
> > > initialize
> > > (self new) foundationYear:0; name:String new.
> > >
> > > But when I inspect an := Animal new, I see that the vars are not
> > > initialized. This may be stupid, but any ideas what I'm doing wrong
> > > here.
> > >
> > > thanks
> > > Sunit
> > > [hidden email]