digitalmars.D.learn - Write variable from other function
- Suliman (8/8) Feb 28 2014 I continue attempted to become a programmer, but I have small
- bearophile (12/16) Feb 28 2014 While D is good enough language, but D is large, so I think it's
- Suliman (5/5) Feb 28 2014 Oh thanks! I had forgot that I should declarate it's in class.
- Dicebot (12/17) Feb 28 2014 Methods that can be called without any specific instance ("this")
- Suliman (4/10) Feb 28 2014 Thanks! I know about static, but now I need experience to work
- Dicebot (3/7) Feb 28 2014 Most likely you don't create an instance. This works:
- Suliman (18/25) Feb 28 2014 Big thanks! That's work!
- Mike Parker (6/15) Feb 28 2014 Here, 'this' is not referring to a constructor, but to an instance of
- Dicebot (9/12) Feb 28 2014 You want static variables, either in form of global variables
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/6) Feb 28 2014 Luckily, there is no global namespace in D. So, when it makes sense
- Jesse Phillips (8/16) Feb 28 2014 You can not access/read/write a variable of another function.
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
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?nqq61th5loq3D 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
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
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?qs2lsozrd3k7Methods 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
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
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
On Friday, 28 February 2014 at 16:26:17 UTC, Dicebot wrote:On Friday, 28 February 2014 at 16:16:36 UTC, Suliman wrote: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 alsoThanks! 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
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 alsoProbably not very good idea from me, I understand that I was wrong thinking!
Feb 28 2014
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
On 3/1/2014 1:16 AM, Suliman wrote: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() );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
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 wrongYou 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
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
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 wrongYou 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