www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Write variable from other function

reply "Suliman" <evermind live.ru> writes:
I continue attempted to become a programmer, but I have small 
problem. I can't understand how to write/change variable from one 
function by code in another.
http://www.everfall.com/paste/id.php?nqq61th5loq3

writeln(configpath);  // I need write this one! <-- this string

I remember (if I now mistake) that there is way to get access to 
vars from other class in way like MyClass2.var = 2 but probably I 
am wrong
Feb 28 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Suliman:

 I continue attempted to become a programmer,
While D is good enough language, but D is large, so I think it's not the best as first language :-)
 I can't understand how to write/change variable from one 
 function by code in another.
In general you can't, and even if you find ways to do it, it's not a good idea. A simple solution is to define a struct (or a class), with a member variable that you can change as you wish.
 http://www.everfall.com/paste/id.php?nqq61th5loq3
D language is too much lax to accept 0/1 as bools, but in general it's quite better to use true/false literals. And functions in D start with a lower case letter. Bye, bearophile
Feb 28 2014
parent reply "Suliman" <evermind live.ru> writes:
Oh thanks! I had forgot that I should declarate it's in class.
But what about return type? Look like that DMD want that I 
declarate some bool value in constructor. It's get me next error: 
"Error: need 'this' for 'isConfigExist' of type 'bool()'"

http://www.everfall.com/paste/id.php?qs2lsozrd3k7
Feb 28 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Friday, 28 February 2014 at 15:59:41 UTC, Suliman wrote:
 Oh thanks! I had forgot that I should declarate it's in class.
 But what about return type? Look like that DMD want that I 
 declarate some bool value in constructor. It's get me next 
 error: "Error: need 'this' for 'isConfigExist' of type 'bool()'"

 http://www.everfall.com/paste/id.php?qs2lsozrd3k7
Methods that can be called without any specific instance ("this") must be declared static too. 'Config` defines a class, which is a type. Normally you want to create an instance of the type (object of the class) before using it and each object has new set of variables. When you declare variable static it makes it shared between all instances of that class, allowing to use it without any specific instance. When you declare method as static you say to compiler that it is not going to use any non-static fields of the class (normally available via "this" pointer) and make it possible to call it as `Config.method`.
Feb 28 2014
parent reply "Suliman" <evermind live.ru> writes:
 When you declare variable static it makes it shared between all 
 instances of that class, allowing to use it without any 
 specific instance. When you declare method as static you say to 
 compiler that it is not going to use any non-static fields of 
 the class (normally available via "this" pointer) and make it 
 possible to call it as `Config.method`.
Thanks! I know about static, but now I need experience to work with class constructor. So now I can't understand what I can do with error: "Error: need 'this' for 'isConfigExist' of type 'bool()'"
Feb 28 2014
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Friday, 28 February 2014 at 16:16:36 UTC, Suliman wrote:
 Thanks! I know about static, but now I need experience to work 
 with class constructor. So now I can't understand what I can do 
 with error: "Error: need 'this' for 'isConfigExist' of type 
 'bool()'"
Most likely you don't create an instance. This works: http://dpaste.dzfl.pl/7c6ada9056ea
Feb 28 2014
parent reply "Suliman" <evermind live.ru> writes:
On Friday, 28 February 2014 at 16:26:17 UTC, Dicebot wrote:
 On Friday, 28 February 2014 at 16:16:36 UTC, Suliman wrote:
 Thanks! I know about static, but now I need experience to work 
 with class constructor. So now I can't understand what I can 
 do with error: "Error: need 'this' for 'isConfigExist' of type 
 'bool()'"
Most likely you don't create an instance. This works: http://dpaste.dzfl.pl/7c6ada9056ea
Big thanks! That's work! What is the way to get from function 2 return value? For example I need get true or false and string value (to use it in another function). bool isConfigExist() { configpath = exePath() ~ "config.txt"; if (exists(configpath)) { return true; } else return false; } Now function return only bool and I need to make it's universal to get text also
Feb 28 2014
parent reply "Suliman" <evermind live.ru> writes:
 	bool isConfigExist()

 		{
 			configpath = exePath() ~ "config.txt";
 			if (exists(configpath))
 				{
 				return true;	
 				}
 				
 			else
 			return false;

 		}

 Now function return only bool and I need to make it's universal
  to get text also
Probably not very good idea from me, I understand that I was wrong thinking!
Feb 28 2014
parent "Dicebot" <public dicebot.lv> writes:
On Friday, 28 February 2014 at 18:18:15 UTC, Suliman wrote:
 Probably not very good idea from me, I understand that I was 
 wrong thinking!
In case you really-really-really want it, such behavior can be achieved via std.typecons.Tuple : import std.typecons; auto foo() { return tuple(true, "text"); } void main() { auto ret_val = foo(); assert(ret_val[1] == "text"); }
Feb 28 2014
prev sibling parent Mike Parker <aldacron gmail.com> writes:
On 3/1/2014 1:16 AM, Suliman wrote:
 When you declare variable static it makes it shared between all
 instances of that class, allowing to use it without any specific
 instance. When you declare method as static you say to compiler that
 it is not going to use any non-static fields of the class (normally
 available via "this" pointer) and make it possible to call it as
 `Config.method`.
Thanks! I know about static, but now I need experience to work with class constructor. So now I can't understand what I can do with error: "Error: need 'this' for 'isConfigExist' of type 'bool()'"
Here, 'this' is not referring to a constructor, but to an instance of the class. It means you are trying to call isConfigExist but haven't created an instance of Config yet. auto config = new Config( "exepath", "configPath" ); writeln( config.isConfigExist() );
Feb 28 2014
prev sibling next sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Friday, 28 February 2014 at 14:15:17 UTC, Suliman wrote:
 I remember (if I now mistake) that there is way to get access 
 to vars from other class in way like MyClass2.var = 2 but 
 probably I am wrong
You want static variables, either in form of global variables (discouraged) or static class fields: class MyClass { static int var; // note static! } You can't access local variables of other functions because they are, well, local.
Feb 28 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/28/2014 06:42 AM, Dicebot wrote:

 You want static variables, either in form of global variables
 (discouraged) or static class fields:
Luckily, there is no global namespace in D. So, when it makes sense module-level global variables are fine as well. Ali
Feb 28 2014
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Friday, 28 February 2014 at 14:15:17 UTC, Suliman wrote:
 I continue attempted to become a programmer, but I have small 
 problem. I can't understand how to write/change variable from 
 one function by code in another.
 http://www.everfall.com/paste/id.php?nqq61th5loq3

 writeln(configpath);  // I need write this one! <-- this string

 I remember (if I now mistake) that there is way to get access 
 to vars from other class in way like MyClass2.var = 2 but 
 probably I am wrong
You can not access/read/write a variable of another function. import std.path : buildPath; auto configpath = buildPath(getcwd(), "config.txt"); if (!isConfigExist(configpath)) { writeln("config do not exists"); writeln(configpath); // I need write this one!
Feb 28 2014