April 26, 2007
okibi escribió:
> Johan Granberg Wrote:
> 
>> okibi wrote:
>>
>>> okibi Wrote:
>>>
>>>> Johan Granberg Wrote:
>>>>
>>>>> okibi wrote:
>>>>>
>>>>>> I was wondering how I would go about passing a variable between two
>>>>>> classes that are within different modules in a program. Here is an
>>>>>> example of what I mean:
>>>>>>
>>>>>> Let this be modMain:
>>>>>>
>>>>>> //begin modMain
>>>>>>
>>>>>> module modMain;
>>>>>>
>>>>>> //imports go here
>>>>>>
>>>>>> import modPopUp;
>>>>>>
>>>>>> class modMain : MainWindow //this is for gtkD
>>>>>> {
>>>>>>   //this() would build the MainWindow
>>>>>>     modPopOp popWindow = new modPopUp();
>>>>>> }
>>>>>>
>>>>>> void main(char[][] args)
>>>>>> {
>>>>>>   GtkD.init(args);
>>>>>>   modMain mMain = new modMain();
>>>>>>   GtkD.main();
>>>>>> }
>>>>>>
>>>>>> //end modMain
>>>>>>
>>>>>> Now let this be modPopUp:
>>>>>>
>>>>>> //begin modPopUp
>>>>>>
>>>>>> module modPopUp:
>>>>>>
>>>>>> //imports go here
>>>>>>
>>>>>> class modPopUp : MainWindow
>>>>>> {
>>>>>>   //this() would build the MainWindow
>>>>>>     char[] myStr;
>>>>>>   //function runs to generate myStr
>>>>>> }
>>>>>>
>>>>>> //end modPopUp
>>>>>>
>>>>>> What I'm asking is how do I pass myStr from modPopUp back to modMain?
>>>>>>
>>>>>> Thanks!
>>>>> Would popWindow.myStr work or have I misunderstood your question?
>>>> It doesn't throw an error, however it grabs a null value instead of what
>>>> the function sets the variable to.
>>> Now if I reference the variable right after creating the instance of
>>> modPopUp, it will go ahead and try to grab the value without waiting to
>>> the instance to run the function to set it. I bet that's why it's grabbing
>>> a null value. Is there another method to pass the variable, or is there a
>>> way to lock modMain until modPopUp returns?
>> I can't determin that from the code above, it would be useful to know what
>> calls the function that sets myStr. If it is called from the constructor my
>> earlier suggestion would work, but if it is called by some other thread
>> something more is required. If as I suspect from your comments myStr is set
>> from another thread you should be able to acquire a lock in the constructor
>> and then release it in the function that sets myStr. 
>>
>> hope this helps you.
> 
> Well, I know the value is coming up null as it doesn't wait for the instance of modPopUp to finish, and is there not getting the data. How would I go about locking modMain until modPopUp is finished?

It would be much more simpler if you'd tell us your problem instead of how to accomplish your particular solution to the problem. Instead of saying "after modPopUp finishes", say "When I press the button, a text is assigned to myStr. Then I want to do something in modMain".

If you want to do the above, the best way is with the observer pattern.

http://en.wikipedia.org/wiki/Observer_pattern
1 2
Next ›   Last »