How do I call a method from another method within the same class 
Author Message
 How do I call a method from another method within the same class

I am new to Smalltalk.  How do I call a method from another method
within the same class.  Thanks!

--

Georgia Institute of Technology
"There will be a movement of storage onto the Net."
                                -George Gilder



Mon, 14 Jul 2003 10:52:27 GMT  
 How do I call a method from another method within the same class
Wen,

Quote:
> I am new to smalltalk.  How do I call a method from another method
> within the same class.

Good to have you in the community.  To answer your question -- you use the
"self" keyword followed by the name of the method.  For example, let's
assume you have a Person class and each instance of person has instance
variables to hold the person's name and age.  Now let's pretend that you
want to be able to assign the name individually and the age individually.
The method to assign the name might be:

======================
name: aName

    name := aName
======================

The method to assign the age might be:

=======================
age: anAge

    age := anAge
========================

So assume you want to assign the name and age together.  The following
method would do that:

==========================
name: aName age: anAge

    self name: aName.
    self age: anAge
=========================

Notice that this method reuses the previous methods to accomplish its work.
This example not only demonstrates how to call a method from another within
the same class, but it also demonstrates the principle of expressing atomic
code one time -- as opposed to rewritting the code in many different places.
For example, let's say you want to store the age in days, although it is
supplied in years.  So this could be accomplished by only changing the
"age:" method maybe like this:

===========================
age: anAgeInYears

    age := anAgeInYears * 365
===========================

Hope that helps!

Regards,

Randy



Mon, 14 Jul 2003 11:22:02 GMT  
 How do I call a method from another method within the same class
Like this:

self method2


Quote:

> I am new to smalltalk.  How do I call a method from another method
> within the same class.  Thanks!

> --

> Georgia Institute of Technology
> "There will be a movement of storage onto the Net."
>                                 -George Gilder

Sent via Deja.com
http://www.deja.com/


Tue, 22 Jul 2003 02:55:30 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Calling protected methods from class methods

2. calling a method of a class within call_user_func_array

3. Getting method from within method?

4. How to call a class method from a string representing a class name

5. accessing a instance method from a class method

6. Class methods vs Instance methods

7. Class method vs instance method???

8. Class method vs instance method???

9. Class methods in ab???.clw files and derived methods

10. same method as class and instance methods?

11. Singleton method == Class method ??

12. Static method and class method comments

 

 
Powered by phpBB® Forum Software