Jump to page: 1 2
Thread overview
How to create instance of class that get data from 2 another instance?
Jan 01, 2015
Suliman
Jan 01, 2015
anonymous
Jan 02, 2015
Suliman
Jan 02, 2015
Suliman
Jan 02, 2015
Tobias Pankrath
Jan 02, 2015
Suliman
Jan 02, 2015
Tobias Pankrath
Jan 02, 2015
Suliman
Jan 02, 2015
Tobias Pankrath
Jan 02, 2015
Suliman
Jan 02, 2015
Suliman
Jan 02, 2015
Suliman
Jan 04, 2015
Ali Çehreli
Jan 04, 2015
Suliman
Jan 04, 2015
Suliman
Jan 05, 2015
Ali Çehreli
January 01, 2015
I have instance of class, that need data from 2 another classes. So I decided that I need to create it only after I get the data from both instance.

auto mysql = new MySQL(parseconfig, seismoDownload); // the problem is here! (LOC 170)

mysql need settings form parseconfig, and link from instance of seismoDownload class.

But I am getting error: undefined identifier parseconfig, did you mean class p
arseConfig?

What I am doing wrong?

http://www.everfall.com/paste/id.php?iwh4qdcqv6zi

January 01, 2015
On Thursday, 1 January 2015 at 22:15:52 UTC, Suliman wrote:
> I have instance of class, that need data from 2 another classes. So I decided that I need to create it only after I get the data from both instance.
>
> auto mysql = new MySQL(parseconfig, seismoDownload); // the problem is here! (LOC 170)
>
> mysql need settings form parseconfig, and link from instance of seismoDownload class.
>
> But I am getting error: undefined identifier parseconfig, did you mean class p
> arseConfig?
>
> What I am doing wrong?
>
> http://www.everfall.com/paste/id.php?iwh4qdcqv6zi

Well, there's no `parseconfig` there. Do you expect the `parseconfig` from line 30 or line 193 to be available in line 170?
January 02, 2015
>> What I am doing wrong?
>>
>> http://www.everfall.com/paste/id.php?iwh4qdcqv6zi
>
> Well, there's no `parseconfig` there. Do you expect the `parseconfig` from line 30 or line 193 to be available in line 170?

From line 30.

In line 193 I am get in instance of class instance of parseconfig to be able to use config inside MySQL class.

What I should to do in situation where for creation of class I should pass to constructor data instances from 2 different classes?
January 02, 2015
I looked at my code and it's seems that I can create instance of MySQL in main.

void main()
{
auto parseconfig = new parseConfig();
auto mysql = new MySQL(parseconfig, seismodownload);
}

In parseConfig I am creation instance of seismoDownload:

class parseConfig
{
....
if (checkLinkCode(emsc_csem) == 200)
{
auto seismodownload = new seismoDownload(emsc_csem);
seismodownload.parse();
}
....
}

So I should be able to call it in main ( I talk about "auto mysql = new MySQL(parseconfig, seismodownload);")


But I am getting error:
Error: undefined identifier seismodownload, did you mean class  seismoDownload?

January 02, 2015
On Friday, 2 January 2015 at 11:21:08 UTC, Suliman wrote:
> I looked at my code and it's seems that I can create instance of MySQL in main.
>
> void main()
> {
> auto parseconfig = new parseConfig();
> auto mysql = new MySQL(parseconfig, seismodownload);
> }
>
> In parseConfig I am creation instance of seismoDownload:
>
> class parseConfig
> {
> ....
> if (checkLinkCode(emsc_csem) == 200)
> {
> auto seismodownload = new seismoDownload(emsc_csem);
> seismodownload.parse();
> }
> ....
> }
>
> So I should be able to call it in main ( I talk about "auto mysql = new MySQL(parseconfig, seismodownload);")
>
>
> But I am getting error:
> Error: undefined identifier seismodownload, did you mean class  seismoDownload?

Anything declared in main() is local to main and not a global.
January 02, 2015
> Anything declared in main() is local to main and not a global.

Ok! So I need create multiple instances of the parseconfig?

Or please suggest me any solution, I really can't understand how to be in such situation...
January 02, 2015
On Friday, 2 January 2015 at 14:50:54 UTC, Suliman wrote:
>> Anything declared in main() is local to main and not a global.
>
> Ok! So I need create multiple instances of the parseconfig?
>
> Or please suggest me any solution, I really can't understand how to be in such situation...

Have you ever used a C-like programming language or are you a bloody beginner? So that I can figure out how basic the explanation has to be.

Basically every pair of {} introduces a scope. And you can only refer to things declared in the current or a parent scope.

{
   int x;
   {
       // x available here
   }
}
// x not available anymore

For your example this means that

void main()
{
     auto parseconfig = new parseConfig();
}
// parseConfig unavaiable since here

class whatever
{
   // cannot use parseconfig
}

What you can do is to declare the config outside any braces and make it a module global. But you'll need a module constructor (look them up in the docs). They are declared with this() outside of any scope in a module.

----
parseConfig parseconfig;

this()
{
    parseconfig = new parseConfig(...);
}

void main() { /* can use parseconfig */ }

class SomeClass { /* should be able to as well */ }
---
January 02, 2015
Thanks! I will try!
D is my first compilable language, I wrote only some scripts without OO before.

So in my case your suggestion is best practice? Or there is any more simple way to pass config and data to MySQL class?
January 02, 2015
On Friday, 2 January 2015 at 15:33:06 UTC, Suliman wrote:
> Thanks! I will try!
> D is my first compilable language, I wrote only some scripts without OO before.
>
> So in my case your suggestion is best practice? Or there is any more simple way to pass config and data to MySQL class?

I think it's fine to have a global config instance. Alternatively you can pass the config to both the seismoDownlead and MySqlWhatever classes.

// there ist enforce(cond, msg) for this in std.exception/std.conv, dunno.
if (!exists(confpath))  throw new Exception("ERROR: config.ini do not exists");
// it becomes
enforce(exists(confpath), "ERROR: config.ini does not exist");
January 02, 2015
> I think it's fine to have a global config instance. Alternatively you can pass the config to both the seismoDownlead and MySqlWhatever classes.
>
> // there ist enforce(cond, msg) for this in std.exception/std.conv, dunno.
> if (!exists(confpath))  throw new Exception("ERROR: config.ini do not exists");
> // it becomes
> enforce(exists(confpath), "ERROR: config.ini does not exist");

Not fully understand. How I need to declare global config instance?

void main()
{
	
}

this()
{
	auto parseconfig = new parseConfig();
}

But how this() will be called in nothing in main?

And I can't understand how to pass the config to both the seismoDownlead and MySqlWhatever classes.

I know how to pass, but I do not understand how to do it with current situation with scopes.

Here is current version of my code. I am not asking to do work for me, but could you show what and where I should move http://www.everfall.com/paste/id.php?oscnkeq740ue

?

« First   ‹ Prev
1 2