www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to set non-static variable in static method within class

reply Sam Hu <samhu.samhu nospam.com> writes:
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
Nov 19 2009
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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?
Nov 19 2009
next sibling parent Sam Hu <samhu.samhu nospam.com> writes:
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.
Nov 19 2009
prev sibling next sibling parent Trass3r <mrmocool gmx.de> writes:
 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.
Nov 19 2009
prev sibling parent Rory McGuire <rjmcguire gmail.com> writes:
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

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.
Nov 20 2009