Subclass method -distorted now put again

BCS BCS at pathlink.com
Wed May 7 16:39:22 PDT 2008


>>>void setText(char[] name, char[] text)
>>>{
>>>switch(name)
>>>{
>>>case "speaker": speaker.setText(text); break;

>  What is diff here? -none

I don't understand your question.

>>>case "radio":     radio.setText(text); break;
>>>default: assert(false);
>>>}
>>>}

duplicating much of Jarrett's comments:

>>
>>In this case, it seems like keeping the controls in an AA would make a lot 
>>more sense.
>>
>>class MyWindow
>>{
>>    Control[char[]] controls;
>>
>>    this()
>>    {
>>        controls["speaker"] = new Slider();
>>        controls["radio"] = new Button();
>>    }
>>
>>    void setText(char[] name, char[] text)
>>    {
>>        controls[name].setText(text);
>>    }
>>} 
>>
>>
> 
> Dont see how this applies .

I think Jarrett is solving a more general problem that you are looking 
at. Replace 'Control', 'Slider' and 'Button' with 'Text' and it might be 
closer to what you want.

Independently:

> I want to add a function to 'dwt.widgets.Text' that takes two char
 > arrays and alters the text in  an instance of dwt.widgets.Text'

Unless you are willing to alter the source code for 'dwt.widgets.Text' 
(and end up with a non-standard version) you can't add a function to 
'dwt.widgets.Text'. The best you can do is derive a new class from it 
and add your function to that.

Also, I don't think adding the function to Text will work anyway 
because, If I understand you correctly, you want to call setText on 
different instances of Text based on 'name'. For this to work the 
setText(name,text) function needs to be attached to whatever holds the 
reference to the Text instance.

Jumping back to the case you described befor this requiters that the new 
function be attached to the Room class because it has the 'radio' and 
'speaker' variables:

class Room
{
   Text speaker;
   Text radio;

   void setText(char[] name, char[] text)
   {
     switch(name)
     {
       case "speaker": speaker.setText(text); break;
       case "radio":     radio.setText(text); break;
       default: assert(false);
     }
   }

   ... // everything else
}


More information about the Digitalmars-d-learn mailing list