Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 28, 2014 Write variable from other 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 |
February 28, 2014 Re: Write variable from other function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 |
February 28, 2014 Re: Write variable from other function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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.
|
February 28, 2014 Re: Write variable from other function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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!
|
February 28, 2014 Re: Write variable from other function | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 |
February 28, 2014 Re: Write variable from other function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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`.
|
February 28, 2014 Re: Write variable from other function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | > 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()'"
|
February 28, 2014 Re: Write variable from other function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 |
February 28, 2014 Re: Write variable from other function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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() );
|
February 28, 2014 Re: Write variable from other function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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
|
Copyright © 1999-2021 by the D Language Foundation