Subclass method -distorted now put again

June somewhere at so.com
Thu May 8 03:43:53 PDT 2008


Jarrett Billingsley Wrote:

> "June" <somewhere at so.com> wrote in message 
> news:fvtbau$p5q$1 at 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. 
> 
> 
Completely lost now .
So much extraneous stuff

>>textBoxes["foo"] = new Text("hi!");  using 'dwt.widgets.Text' ,,this
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?


More information about the Digitalmars-d-learn mailing list