Dear all, I wonder if there is some way to print a Character into the Transcript as I came across the situation below: The Name class I defined has a message like firstName: string1 middleName: aCharacter lastName: string2. Then I create an instance of the Name class like aName := Name firstName: 'John' middleName: $C lastName: 'LeCarre'. After that I try to use Transcript show: aName middleName. to print the middle name into the Transcript but failed. So how to print a Character into the Transcript? I am looking foward to your kind help. ----- Best Regards, Ian -- View this message in context: http://www.nabble.com/How-to-print-a-Character-into-the-Transcript--tp15496158p15496158.html Sent from the VisualWorks mailing list archive at Nabble.com. |
Transcript expects Strings for the show: method. So either you have to
convert the single character $C to a String (via 'String with: aName middleName') or use the nextPut: method on Transcript that prints single characters. HTH Thomas Ian Jiang schrieb: > Dear all, > > I wonder if there is some way to print a Character into the Transcript as I > came across the situation below: > > The Name class I defined has a message like firstName: string1 middleName: > aCharacter lastName: string2. > > Then I create an instance of the Name class like > aName := Name firstName: 'John' middleName: $C lastName: 'LeCarre'. > > After that I try to use Transcript show: aName middleName. to print the > middle name into the Transcript but failed. So how to print a Character into > the Transcript? > > I am looking foward to your kind help. > > > ----- > Best Regards, > > Ian > -- Mit freundlichen GrĂ¼ssen Thomas Brodt PORaBo Consulting Tel. +41 71 666 76 52 Fax. +41 71 666 76 57 http://www.porabo.ch |
In reply to this post by Ian Jiang
Ian Jiang escreveu:
> Dear all, > > I wonder if there is some way to print a Character into the Transcript as I > came across the situation below: > > The Name class I defined has a message like firstName: string1 middleName: > aCharacter lastName: string2. > Why do you want to store the middle name as a character? Nip in the bud this problem and store it as String! Name>>firstName: string1 middleName: aCharacter lastName: string2 "Assuming the atributes for storing are as follows." firstname := string1. middlename := String with: aCharacter. lastname := string2 You can even have some polymorphism: Name>>firstName: string1 middleName: anotherString lastName: string2 "Same assumptions" firstname := string1. middlename := anotherString. lastname := string2 Regards, -- Cesar Rabak GNU/Linux User 52247. Get counted: http://counter.li.org/ |
In reply to this post by Thomas Brodt
On Feb 15, 2008, at 9:42 AM, Thomas Brodt wrote: > Transcript expects Strings for the show: method. So either you have > to convert the single character $C to a String (via 'String with: > aName middleName') or use the nextPut: method on Transcript that > prints single characters. > I tend to use #print: since that will work for any object. So if the middle name is nil it will be printed as nil instead of raising an exception. An alternative approach is that you implement #printOn: on your Name, then you can #print: a Name in one go instead of #print:-ing the separate parts of a Name. R - > HTH > > Thomas > > Ian Jiang schrieb: >> Dear all, >> >> I wonder if there is some way to print a Character into the >> Transcript as I >> came across the situation below: >> >> The Name class I defined has a message like firstName: string1 >> middleName: >> aCharacter lastName: string2. >> >> Then I create an instance of the Name class like aName := Name >> firstName: 'John' middleName: $C lastName: 'LeCarre'. >> >> After that I try to use Transcript show: aName middleName. to >> print the >> middle name into the Transcript but failed. So how to print a >> Character into >> the Transcript? >> >> I am looking foward to your kind help. >> >> >> ----- >> Best Regards, >> >> Ian >> > > -- > Mit freundlichen GrĂ¼ssen > > Thomas Brodt > PORaBo Consulting > Tel. +41 71 666 76 52 > Fax. +41 71 666 76 57 > http://www.porabo.ch > > |
In reply to this post by Ian Jiang
The Transcript is a character stream in addition to being an output console. Streams put elements via nextPut:. So you can say
Transcript nextPutAll: firstName; space; nextPut: middleInitial; nextPut: $.; space; nextPutAll: lastName; flush. The flush is needed because Transcript doesn't actually display the output until told to. show: aString is equivalent to nextPutAll: aString; flush. Flush is to be preferred over show: (or endEntry) since its compatible with stream. But you shouldn't be implementing a print method that prints direct to the Transcript. Instead implement printOn: to print to any stream, e.g. printOn: aStream aStream nextPutAll: firstName; space; nextPut: middleInitial; nextPut: $.; space; nextPutAll: lastName or better still displayString String new writeStream nextPutAll: firstName; space; nextPut: middleInitial; nextPut: $.; space; nextPutAll: lastName; contents printOn: aStream super printOn: aStream. aStream nextPutAll: firstName; space; nextPut: middleInitial; nextPut: $.; space; nextPutAll: lastName Now you can say Transcript print: aName; flush, get the class name printed (useful if you add subclasses) and have Name instances displayed nicely in the UI. On Fri, Feb 15, 2008 at 12:32 AM, Ian Jiang <[hidden email]> wrote:
|
Free forum by Nabble | Edit this page |