How to print a Character into the Transcript?

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

How to print a Character into the Transcript?

Ian Jiang

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.

Reply | Threaded
Open this post in threaded view
|

Re: How to print a Character into the Transcript?

Thomas Brodt
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

Reply | Threaded
Open this post in threaded view
|

Re: How to print a Character into the Transcript?

Cesar Rabak
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/

Reply | Threaded
Open this post in threaded view
|

Re: How to print a Character into the Transcript?

Reinout Heeck
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
>
>

Reply | Threaded
Open this post in threaded view
|

Re: How to print a Character into the Transcript?

Eliot Miranda-2
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:

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.