accessing a instance method from a class method
Author |
Message |
John L Aldridg #1 / 9
|
 accessing a instance method from a class method
I want to access an instance method from a class method, can anybody help? Also I want to create a class instance from a class method, but having problems. I thank anybody in advance for any help. A frustrated OU student. Best regards John
|
Thu, 13 Oct 2005 05:15:56 GMT |
|
 |
Peter van Rooije #2 / 9
|
 accessing a instance method from a class method
Hello John, First of all, if these issues are frustrating for you as an OU student, something is not right with the way you are doing this course. But I'll let you deal with that for now and simply put you on the right track with your questions, as best I can. Quote: > I want to access an instance method from a class method, can anybody help?
Q: Access an instance method? What do you mean by that? Where did you get that terminology? You can do many things in Smalltalk, but accessing methods is not one of them. The basic things you can do are. (0. Denote an object) 1. Send a message 2. Assign a value to a variable 3. Return a value from a method If what you are seeking to do is invoke an instance method, the way to go about it is... 1. Send a message. When you send a message, a method will be invoked. Which one is decided by the system based on the code. IOW, you don't get to invoke methods, you just get to send messages, and the system will decide which method it invokes. If you want an instance method to be invoked, what you will need to do is send a message to an instance. If you need more help with that question, come back here. Quote: > Also I want to create a class instance from a class method, but having > problems.
Q: What do you mean when you say 'a class instance'? Let's assume you mean 'I want to create an instance of a class C from a class method of C'. In that case, your best strategy is to send the message #new to the class, in that class method. Do you know how to do that or do you need more help? Quote: > I thank anybody in advance for any help.
You are welcome. Quote: > A frustrated OU student.
What exactly are you frustrated about? You can probably get help here if you let us know. Quote: > Best regards > John
Regards, Peter van Rooijen
|
Thu, 13 Oct 2005 14:36:49 GMT |
|
 |
John L Aldridg #3 / 9
|
 accessing a instance method from a class method
Dear Peter, Thank you for your help. You are right my terminology is bad. Terminology has never been my strong point. Lets start again. I have created a class method. Within that class method I want to do 2 things: 1. Send a message to a instance variable. 2. Create a new object of the class In general I am finding the OU course interesting, but when you need clarification on a point, it is terrible. There are a lot of unhappy student!. Thanks once again. Best regards John L. Aldridge
Quote:
> Hello John, > First of all, if these issues are frustrating for you as an OU student, > something is not right with the way you are doing this course. But I'll let > you deal with that for now and simply put you on the right track with your > questions, as best I can. > > I want to access an instance method from a class method, can anybody help? > Q: Access an instance method? What do you mean by that? Where did you get > that terminology? > You can do many things in Smalltalk, but accessing methods is not one of > them. The basic things you can do are. > (0. Denote an object) > 1. Send a message > 2. Assign a value to a variable > 3. Return a value from a method > If what you are seeking to do is invoke an instance method, the way to go > about it is... > 1. Send a message. > When you send a message, a method will be invoked. Which one is decided by > the system based on the code. IOW, you don't get to invoke methods, you just > get to send messages, and the system will decide which method it invokes. > If you want an instance method to be invoked, what you will need to do is > send a message to an instance. > If you need more help with that question, come back here. > > Also I want to create a class instance from a class method, but having > > problems. > Q: What do you mean when you say 'a class instance'? > Let's assume you mean 'I want to create an instance of a class C from a > class method of C'. > In that case, your best strategy is to send the message #new to the class, > in that class method. Do you know how to do that or do you need more help? > > I thank anybody in advance for any help. > You are welcome. > > A frustrated OU student. > What exactly are you frustrated about? You can probably get help here if you > let us know. > > Best regards > > John > Regards, > Peter van Rooijen
|
Thu, 13 Oct 2005 16:45:45 GMT |
|
 |
Reinout Heec #4 / 9
|
 accessing a instance method from a class method
Quote:
> I have created a class method. Within that class method I want to do 2 > things: > 1. Send a message to a instance variable. > 2. Create a new object of the class
Number 2 is easy: send the message #new to the class; the result is a new instance. Number one sounds confusing to me, there are two essential problems with it: 1) instance variables are private to an instance, so you cannot reach them (send messages to them) directly from the outside (unlike other languages like Java where this is possible). You _can_ reach them directly from instance methods of the object. To make an instance variable accessable from the outside create a getter and setter method for it. 2) a class can have many instances, the question is: the instance variable of _which_ instance. Since I don't understand what you want to do (yet) let me go on a tangent and assume you want the following: in a class method you want to create an instance and then set an instance variable of that instance to some value. I further assume that 'some value' is a literal (constant) defined in that method. Here is a straitforward implementation of such a class method: myClassMethod "return a new instance with 'myInstVar' initialized" | instance | instance := self new. instance myInstVar: 'some value'. ^instance note that on the instance side you will need to create the setter method #myInstVar: before this will work. here is a variation you'll see very often in smalltalk code: If all instances of your class need to have the instance variable set to a same value when they are instantiated we can move that knowledge to an instance method. We can also make it hard to create instances that are not initialized. We do that by redefining (overriding) the #new method on the class: new "return a new instance with 'myInstVar' initialized" | instance | instance := super new. "'self new' will not work. why?" instance initialize. ^instance and by defining the instance method #initialize like so: initialize myInstVar := 'some value' note that here you can access the instance variable directly, you don't need a setter method. Quote: > In general I am finding the OU course interesting, but when you need > clarification on a point, it is terrible. There are a lot of unhappy > student!.
Spread the word: they (you) are welcome here with their questions. For those that don't like newsgroups try the vwnc-list or the IRC channel, details at: http://wiki.cs.uiuc.edu/VisualWorks/VisualWorks+Online+Communities HTH, Reinout -------
|
Thu, 13 Oct 2005 18:04:59 GMT |
|
 |
John Leste #5 / 9
|
 accessing a instance method from a class method
Hi John. I'm doing M206 as well, so perhaps I can help. Quote: > Dear Peter, > Thank you for your help. > You are right my terminology is bad. Terminology has never been my strong > point. > Lets start again. > I have created a class method. Within that class method I want to do 2 > things: > 1. Send a message to a instance variable.
I don't get you here. Do you mean, you want to send a message to an object referenced by an instance variable? I don't see any requirement in the assignment to do that. Quote: > 2. Create a new object of the class
Send the message new to the class. Then, to send messages to the new instance, either use a temporary variable to refer to the instance or send messages to the message answer from the new message: (self new) message; message: arg Quote: > In general I am finding the OU course interesting, but when you need > clarification on a point, it is terrible. There are a lot of unhappy > student!.
It bugs me that the tutors (at least in my region) never participate in the OU conferences, despite specific pleas from students to resolve some issue. Have you tried mailing your tutor? John
|
Thu, 13 Oct 2005 18:07:45 GMT |
|
 |
John L Aldridg #6 / 9
|
 accessing a instance method from a class method
I am in R09. Believe me its bad. I thought there would be quite a free exchange of information, but unfortunately its not so. The tutors seem to be always on holiday! Best regards John
Quote:
> >It bugs me that the tutors (at least in my region) never participate in the > >OU conferences, despite specific pleas from students to resolve some issue. > OU tutors are part-time, but a conference moderator should be able to > reply to a query within a few days. What region are you in? > The FirstClass conferences should be your first choice of forum, rather > than this group, and you should note it is monitored by OU staff. > -- > Nigel Mercier > M206 tutor - Region 13 > http://www.mercier.org.uk/
|
Thu, 13 Oct 2005 23:54:24 GMT |
|
 |
Peter van Rooije #7 / 9
|
 accessing a instance method from a class method
Quote:
> >It bugs me that the tutors (at least in my region) never participate in the > >OU conferences, despite specific pleas from students to resolve some issue. > OU tutors are part-time, but a conference moderator should be able to > reply to a query within a few days.
OU students have quite a good chance of getting help here a lot quicker, especially if they phrase their questions clearly. Quote: > What region are you in? > The FirstClass conferences should be your first choice of forum, rather > than this group, and you should note it is monitored by OU staff.
Let me just add to that that this newsgroup is an open forum which anyone can use. I would encourage OU students to identify themselves as such and place their queries here. There are many people like myself who have a lot of experience teaching and don't mind helping out someone trying to learn. Regards, Peter van Rooijen Quote: > -- > Nigel Mercier > M206 tutor - Region 13 > http://www.mercier.org.uk/
|
Fri, 14 Oct 2005 05:56:05 GMT |
|
 |
John Leste #8 / 9
|
 accessing a instance method from a class method
Nigel Mercier ? wrote Quote:
> >It bugs me that the tutors (at least in my region) never participate in the > >OU conferences, despite specific pleas from students to resolve some issue. > OU tutors are part-time, but a conference moderator should be able to > reply to a query within a few days. What region are you in?
I'm in R04. Quote: > The FirstClass conferences should be your first choice of forum, rather > than this group, and you should note it is monitored by OU staff.
There's nothing I'd say in here that I wouldn't also say in FC, although I'm likely to be more humble here ;-) John
|
Fri, 14 Oct 2005 06:52:31 GMT |
|
 |
Steven T Abel #9 / 9
|
 accessing a instance method from a class method
Quote: > I want to access an instance method from a class method, can anybody help? > Also I want to create a class instance from a class method, but having > problems.
I'll have to make several assumptions to try to help based on the litte bit that you've said, and I might sound a little harsh in the process. No offense intended. First, you might say that you access a variable, but you either "call" or, more properly, "invoke" a method. Methods are invoked by sending a message. Some folks think this is splitting hairs, but I used to teach this stuff and the words matter. You can only invoke an instance method on an instance. You can do this in a class method if you have an instance. You can probably get an instance by saying "MyClass new" or "self new" if the class method is a method of the class you want. It sounds to me like you haven't yet understood that this OO stuff is not just a fancy way of organizing functions. Please flush this if I'm off base. I'll be happy to discuss it more with you if I'm not. Best regards, Steve -- Steven T Abell Software Designer http://www.brising.com In software, nothing is more concrete than a good abstraction.
|
Sat, 15 Oct 2005 11:55:12 GMT |
|
|
|