How to set non-static variable in static method within class

Ary Borenszweig ary at esperanto.org.ar
Thu Nov 19 02:42:36 PST 2009


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?


More information about the Digitalmars-d-learn mailing list