www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Subclass method -distorted now put again

reply June <somewhere so.com> writes:
dwt.widget.Text has method
 
void setText  (char[] string); 
Sets the contents of the receiver to the given string. 

In my application I use many of these text boxes each with a name so I need a
function 

void setText(char[] name, char[] text){
   name.setText(text);}

How can  I bring this into my program ?
May 06 2008
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
June escribió:
 dwt.widget.Text has method
  
 void setText  (char[] string); 
 Sets the contents of the receiver to the given string.
 
 In my application I use many of these text boxes each with a name so I need a
function 
 
 void setText(char[] name, char[] text){
    name.setText(text);}

name.setText(text) ? But name is of type char[]
 
 How can  I bring this into my program ?

Maybe you could show what you do now, and how you would like to have it if you were able to do what you want. I don't understand what you are trying to do. :(
May 07 2008
parent reply June <somewhere so.com> writes:
Ary Borenszweig Wrote:

 June escribió:
 dwt.widget.Text has method
  
 void setText  (char[] string); 
 Sets the contents of the receiver to the given string.
 
 In my application I use many of these text boxes each with a name so I need a
function 
 
 void setText(char[] name, char[] text){
    name.setText(text);}

name.setText(text) ? But name is of type char[]
 
 How can  I bring this into my program ?

Maybe you could show what you do now, and how you would like to have it if you were able to do what you want. I don't understand what you are trying to do. :(

In a class 'Room' I make a text box called "speaker', and another called 'radio' etc Text speaker = new Text; all blank atm In another module 'town' I create instance of 'room' and now I want to set the text in each speaker.setText("Wallplate"); radio.setText("Valves") So Text class has method 'setText(char[]) In 'town' I want to make another method like setText(name[],text[]); different parameters to setText () in Text- I need to find out how to do it please?
May 07 2008
next sibling parent BLS <nanali nospam-wanadoo.fr> writes:
June schrieb:
 Ary Borenszweig Wrote:
 
 June escribió:
 dwt.widget.Text has method
  
 void setText  (char[] string); 
 Sets the contents of the receiver to the given string.

 In my application I use many of these text boxes each with a name so I need a
function 

 void setText(char[] name, char[] text){
    name.setText(text);}

But name is of type char[]
 How can  I bring this into my program ?

if you were able to do what you want. I don't understand what you are trying to do. :(

In a class 'Room' I make a text box called "speaker', and another called 'radio' etc Text speaker = new Text; all blank atm In another module 'town' I create instance of 'room' and now I want to set the text in each speaker.setText("Wallplate"); radio.setText("Valves") So Text class has method 'setText(char[]) In 'town' I want to make another method like setText(name[],text[]); different parameters to setText () in Text- I need to find out how to do it please?

Hi June, class room { void setText(char[] roomtext) { // print or whatever you wanna do with roomtext } } class town : room { void setText(char[] roomtext, char[] towntext) { super.setText(roomtext); // calls room.setText() // do something with towntext } } hth bjoern
May 07 2008
prev sibling parent reply BCS <BCS pathlink.com> writes:
June wrote:
 Ary Borenszweig Wrote:
 
 
June escribió:

dwt.widget.Text has method
 
void setText  (char[] string); 
Sets the contents of the receiver to the given string.

In my application I use many of these text boxes each with a name so I need a
function 

void setText(char[] name, char[] text){
   name.setText(text);}

name.setText(text) ? But name is of type char[]
How can  I bring this into my program ?

Maybe you could show what you do now, and how you would like to have it if you were able to do what you want. I don't understand what you are trying to do. :(

In a class 'Room' I make a text box called "speaker', and another called 'radio' etc Text speaker = new Text; all blank atm In another module 'town' I create instance of 'room' and now I want to set the text in each speaker.setText("Wallplate"); radio.setText("Valves") So Text class has method 'setText(char[]) In 'town' I want to make another method like setText(name[],text[]); different parameters to setText () in Text- I need to find out how to do it please?

so "setText("speaker", "foo") would call speaker.setText("foo")? D doesn't support this directly, however you could do it manually void setText(char[] name, char[] text) { switch(name) { case "speaker": speaker.setText(text); break; case "radio": radio.setText(text); break; default: assert(false); } }
May 07 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <BCS pathlink.com> wrote in message 
news:fvsdh5$2hii$11 digitalmars.com...

 so "setText("speaker", "foo") would call speaker.setText("foo")?

 D doesn't support this directly, however you could do it manually

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

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); } }
May 07 2008
parent reply June <somewhere so.com> writes:
Jarrett Billingsley Wrote:

 "BCS" <BCS pathlink.com> wrote in message 
 news:fvsdh5$2hii$11 digitalmars.com...
 
 so "setText("speaker", "foo") would call speaker.setText("foo")?


YES
 D doesn't support this directly, however you could do it manually

 void setText(char[] name, char[] text)
 {
 switch(name)
 {
 case "speaker": speaker.setText(text); break;  What is diff here? -none
 case "radio":     radio.setText(text); break;
 default: assert(false);
 }
 }

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); } }

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' dwt.widgets.Text' only has a function that takes one char array ? setText(char[] text) Sorry if confusing a womans perogative no?
May 07 2008
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"June" <somewhere so.com> wrote in message 
news:fvtbau$p5q$1 digitalmars.com...

 Dont see how this applies .
 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'

 dwt.widgets.Text'  only has a function  that takes one char array ? 
 setText(char[] text)

It applies because it's exactly what you want to do ;) You have several Text objects, yes? And each one has a name? You can't just "add a method" to Text and have it "find" a text box of a given name, you have to store those text boxes and perform the name lookup yourself. Remember that a class method only operates on a single object; if you subclassed Text, you wouldn't be able to access other instances of Text besides 'this' unless you stored them somewhere. So instead of doing something like class MyWindow { Text foo; Text bar; this() { foo = new Text("hi!"); bar = new Text("bye!"); } } You can instead store them in an associative array which maps from names to text boxes: class MyWindow { Text[char[]] textBoxes; this() { textBoxes["foo"] = new Text("hi!"); textBoxes["bar"] = new Text("bye!"); } } Then, you can add a method to MyWindow that will take a name and a string, and will set the text box with the given name to the given string: // defined as a method of MyWindow void setText(char[] name, char[] s) { textBoxes[name].setText(s); } Keep in mind that D is a statically-compiled language, unlike languages like Python, and so dynamic (runtime) lookup of variables and members is, in general, not possible. Which is why you have to store the mapping from names to controls yourself.
May 07 2008
parent reply June <somewhere so.com> writes:
Jarrett Billingsley Wrote:

 "June" <somewhere so.com> wrote in message 
 news:fvtbau$p5q$1 digitalmars.com...
 
 Dont see how this applies .
 I want to add a function to 'dwt.widgets.Text' that takes two char


 and alters the text in  an instance of dwt.widgets.Text'

 dwt.widgets.Text'  only has a function  that takes one char


 setText(char[] text)

It applies because it's exactly what you want to do ;) You have several Text objects, yes? And each one has a name? You

 just "add a method" to Text and have it "find" a text box of a given

 you have to store those text boxes and perform the name lookup

 Remember that a class method only operates on a single object; if

 subclassed Text, you wouldn't be able to access other instances of

 besides 'this' unless you stored them somewhere.
 
 So instead of doing something like
 
 class MyWindow
 {
     Text foo;
     Text bar;
 
     this()
     {
         foo = new Text("hi!");
         bar = new Text("bye!");
     }
 }
 
 You can instead store them in an associative array which maps from

 text boxes:
 
 class MyWindow
 {
     Text[char[]] textBoxes;
 
     this()
     {
         textBoxes["foo"] = new Text("hi!");
         textBoxes["bar"] = new Text("bye!");
     }
 }
 
 Then, you can add a method to MyWindow that will take a name and a

 and will set the text box with the given name to the given string:
 
 // defined as a method of MyWindow
 void setText(char[] name, char[] s)
 {
     textBoxes[name].setText(s);
 }
 
 Keep in mind that D is a statically-compiled language, unlike

 Python, and so dynamic (runtime) lookup of variables and members is,

 general, not possible.  Which is why you have to store the mapping

 names to controls yourself. 
 
 

So much extraneous stuff
textBoxes["foo"] = new Text("hi!");  using 'dwt.widgets.Text' ,,this


I understand the need to store the names point you are making but surely I can override the 'dwt.widgets.Text's ' setText(text) function some way?
May 08 2008
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
June escribió:
 Jarrett Billingsley Wrote:
 
 "June" <somewhere so.com> wrote in message 
 news:fvtbau$p5q$1 digitalmars.com...

 Dont see how this applies .
 I want to add a function to 'dwt.widgets.Text' that takes two char


 and alters the text in  an instance of dwt.widgets.Text'

 dwt.widgets.Text'  only has a function  that takes one char


 setText(char[] text)

You have several Text objects, yes? And each one has a name? You

 just "add a method" to Text and have it "find" a text box of a given

 you have to store those text boxes and perform the name lookup

 Remember that a class method only operates on a single object; if

 subclassed Text, you wouldn't be able to access other instances of

 besides 'this' unless you stored them somewhere.

 So instead of doing something like

 class MyWindow
 {
     Text foo;
     Text bar;

     this()
     {
         foo = new Text("hi!");
         bar = new Text("bye!");
     }
 }

 You can instead store them in an associative array which maps from

 text boxes:

 class MyWindow
 {
     Text[char[]] textBoxes;

     this()
     {
         textBoxes["foo"] = new Text("hi!");
         textBoxes["bar"] = new Text("bye!");
     }
 }

 Then, you can add a method to MyWindow that will take a name and a

 and will set the text box with the given name to the given string:

 // defined as a method of MyWindow
 void setText(char[] name, char[] s)
 {
     textBoxes[name].setText(s);
 }

 Keep in mind that D is a statically-compiled language, unlike

 Python, and so dynamic (runtime) lookup of variables and members is,

 general, not possible.  Which is why you have to store the mapping

 names to controls yourself. 

So much extraneous stuff
 textBoxes["foo"] = new Text("hi!");  using 'dwt.widgets.Text' ,,this


I understand the need to store the names point you are making but surely I can override the 'dwt.widgets.Text's ' setText(text) function some way?

You can override it by defining a method with the same name and signature: class YourClass : Text { override void setText(char[] text) { // your code... } } However, if you define a method "void setText(char[] name, char[] text)" in YourClass, that's not an override: that's an overload. And I'm not sure, but in that case, if you still want to be able to use the old setText, you need to make an alias for it: alias setText setText;
May 07 2008
parent June <somewhere so.com> writes:
Ary Borenszweig wrote:

 Completely lost now .
 So much extraneous stuff
 
 textBoxes["foo"] = new Text("hi!");  using 'dwt.widgets.Text'
 ,,this


work I understand the need to store the names point you are making but surely I can override the 'dwt.widgets.Text's ' setText(text) function some way?

You can override it by defining a method with the same name and signature: class YourClass : Text { override void setText(char[] text) { // your code... } } However, if you define a method "void setText(char[] name, char[] text)" in YourClass, that's not an override: that's an overload. And I'm not sure, but in that case, if you still want to be able to use the old setText, you need to make an alias for it: alias setText setText;

Thanks thats what I am seeking to do .I will try it out
May 08 2008
prev sibling parent BCS <BCS pathlink.com> writes:
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 }
May 07 2008