Subclass method -distorted now put again

Jarrett Billingsley kb3ctd2 at
Wed May 7 16:21:23 PDT 2008

"June" <somewhere at> wrote in message 
news:fvtbau$p5q$1 at

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

        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;

        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)

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. 

More information about the Digitalmars-d-learn mailing list