digitalmars.D.learn - Subclass method -distorted now put again
- June (8/8) May 06 2008 dwt.widget.Text has method
- Ary Borenszweig (6/17) May 07 2008 name.setText(text) ?
- June (13/34) May 07 2008 In a class 'Room'
- BLS (18/57) May 07 2008 Hi June,
- BCS (12/57) May 07 2008 so "setText("speaker", "foo") would call speaker.setText("foo")?
- Jarrett Billingsley (17/28) May 07 2008 In this case, it seems like keeping the controls in an AA would make a l...
- June (6/43) May 07 2008 Dont see how this applies .
- Jarrett Billingsley (42/47) May 07 2008 It applies because it's exactly what you want to do ;)
- June (19/82) May 08 2008 array ?
- Ary Borenszweig (12/96) May 07 2008 You can override it by defining a method with the same name and signatur...
- June (2/31) May 08 2008 Thanks thats what I am seeking to do .I will try it out
- BCS (33/68) May 07 2008 I think Jarrett is solving a more general problem that you are looking
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
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
Ary Borenszweig Wrote:June escribió: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?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
June schrieb:Ary Borenszweig Wrote: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 bjoernJune escribió: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?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
June wrote:Ary Borenszweig Wrote: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); } }June escribió: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?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
"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
Jarrett Billingsley Wrote:"BCS" <BCS pathlink.com> wrote in message news:fvsdh5$2hii$11 digitalmars.com...YESso "setText("speaker", "foo") would call speaker.setText("foo")?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) Sorry if confusing a womans perogative no?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); } }
May 07 2008
"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
Jarrett Billingsley Wrote:"June" <somewhere so.com> wrote in message news:fvtbau$p5q$1 digitalmars.com...arraysDont see how this applies . I want to add a function to 'dwt.widgets.Text' that takes two chararray ?and alters the text in an instance of dwt.widgets.Text' dwt.widgets.Text' only has a function that takes one charcan'tsetText(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? Youjust "add a method" to Text and have it "find" a text box of a givenname,you have to store those text boxes and perform the name lookupyourself.Remember that a class method only operates on a single object; ifyousubclassed Text, you wouldn't be able to access other instances ofTextbesides '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 fromnames totext 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 astring,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, unlikelanguages likePython, and so dynamic (runtime) lookup of variables and members is,ingeneral, not possible. Which is why you have to store the mappingfromnames to controls yourself.Completely lost now . So much extraneous stuffexpects a composite parent and an integer style so this does not 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?textBoxes["foo"] = new Text("hi!"); using 'dwt.widgets.Text' ,,this
May 08 2008
June escribió:Jarrett Billingsley Wrote: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;"June" <somewhere so.com> wrote in message news:fvtbau$p5q$1 digitalmars.com...arraysDont see how this applies . I want to add a function to 'dwt.widgets.Text' that takes two chararray ?and alters the text in an instance of dwt.widgets.Text' dwt.widgets.Text' only has a function that takes one charcan'tsetText(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? Youjust "add a method" to Text and have it "find" a text box of a givenname,you have to store those text boxes and perform the name lookupyourself.Remember that a class method only operates on a single object; ifyousubclassed Text, you wouldn't be able to access other instances ofTextbesides '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 fromnames totext 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 astring,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, unlikelanguages likePython, and so dynamic (runtime) lookup of variables and members is,ingeneral, not possible. Which is why you have to store the mappingfromnames to controls yourself.Completely lost now . So much extraneous stuffexpects a composite parent and an integer style so this does not 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?textBoxes["foo"] = new Text("hi!"); using 'dwt.widgets.Text' ,,this
May 07 2008
Ary Borenszweig wrote:Thanks thats what I am seeking to do .I will try it outCompletely lost now . So much extraneous stuffYou 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;expects a composite parent and an integer style so this does not 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?textBoxes["foo"] = new Text("hi!"); using 'dwt.widgets.Text' ,,this
May 08 2008
void setText(char[] name, char[] text) { switch(name) { case "speaker": speaker.setText(text); break;What is diff here? -noneI don't understand your question.duplicating much of Jarrett's comments:case "radio": radio.setText(text); break; default: assert(false); } }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: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 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