May 08, 2008
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
> 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?

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 08, 2008
Ary Borenszweig wrote:

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

1 2
Next ›   Last »