Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 19, 2009 How to set non-static variable in static method within class | ||||
---|---|---|---|---|
| ||||
Greetings! How to set value to non-static variable in a static method within a class? Given below code: import std.stdio; class InputDialog { string name; static string s_name; static this() { s_name=""; } static string getString(string prompt="Please enter a line of string below", string defaultValue="your string here") { defaultValue=dataFromConsole();//console input s_name=defaultValue; return s_name; } } int main(string[] args) { string str=InputDialog.getString; writefln("You entered %s",str); return 0; } This works.But what if actually I want to set non-static variable name other than static variable s_name in static method getString(...)? How can I do that? Thanks for your help in advance. Regards, Sam |
November 19, 2009 Re: How to set non-static variable in static method within class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Sam Hu wrote:
> Greetings!
>
> How to set value to non-static variable in a static method within a class?
>
> Given below code:
>
> import std.stdio;
>
> class InputDialog
> {
> string name;
>
>
> static string s_name;
>
>
> static this()
> {
> s_name="";
>
>
>
> }
>
> static string getString(string prompt="Please enter a line of string below",
> string defaultValue="your string here")
> {
> defaultValue=dataFromConsole();//console input
> s_name=defaultValue;
> return s_name;
> }
>
> }
>
> int main(string[] args)
> {
> string str=InputDialog.getString;
> writefln("You entered %s",str);
>
> return 0;
> }
>
> This works.But what if actually I want to set non-static variable name other than static variable s_name in static method getString(...)?
> How can I do that?
>
> Thanks for your help in advance.
>
> Regards,
> Sam
You can't access non-static data from a static method. Non-static data is related to an instance of a class, and a static method is not bound to any instance.
Why do you want to do that?
|
November 19, 2009 Re: How to set non-static variable in static method within class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | Ary Borenszweig Wrote:
>
> You can't access non-static data from a static method. Non-static data is related to an instance of a class, and a static method is not bound to any instance.
>
> Why do you want to do that?
Say I want to implement an utility dialog, InputDialog in DFL which is a subclass of Form,using its static method I can call InputDialog.getString to retrieve a string from the textbox of this dialog other than create an instance of InputDialog,at the mean time I want to customize the caption text and the prompt message of the InputDialog in the static method InputDialog.getString.But I can't modify Form.text, (form.)TextBox.text in the static method.Currently I have to use its non-static version of getString method and create an instance of the dialog each time when I call getString.
|
November 19, 2009 Re: How to set non-static variable in static method within class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | > You can't access non-static data from a static method. Non-static data is related to an instance of a class, and a static method is not bound to any instance.
Exactly. The only way would be to have some array of instances somewhere to access.
|
November 20, 2009 Re: How to set non-static variable in static method within class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Sam Hu <samhu.samhu@nospam.com> wrote: > Ary Borenszweig Wrote: > >> >> You can't access non-static data from a static method. Non-static data is related to an instance of a class, and a static method is not bound to any instance. >> >> Why do you want to do that? > > Say I want to implement an utility dialog, InputDialog in DFL which is a subclass of Form,using its static method I can call InputDialog.getString to retrieve a string from the textbox of this dialog other than create an instance of InputDialog,at the mean time I want to customize the caption text and the prompt message of the InputDialog in the static method InputDialog.getString.But I can't modify Form.text, (form.)TextBox.text in the static method.Currently I have to use its non-static version of getString method and create an instance of the dialog each time when I call getString. > since non-static data is only created with an instance you would have to use some sort of Singleton or something, I suppose. |
Copyright © 1999-2021 by the D Language Foundation