digitalmars.D.learn - Passing Variables between classes in modules
- okibi (33/33) Apr 26 2007 I was wondering how I would go about passing a variable between two clas...
- Johan Granberg (2/53) Apr 26 2007 Would popWindow.myStr work or have I misunderstood your question?
- okibi (2/57) Apr 26 2007 It doesn't throw an error, however it grabs a null value instead of what...
- okibi (2/61) Apr 26 2007 Now if I reference the variable right after creating the instance of mod...
- Johan Granberg (8/76) Apr 26 2007 I can't determin that from the code above, it would be useful to know wh...
- okibi (2/81) Apr 26 2007 Well, I know the value is coming up null as it doesn't wait for the inst...
- Johan Granberg (4/91) Apr 26 2007 I can't really tell without seeing more code, the most important informa...
- okibi (7/100) Apr 26 2007 It really is that simple of code though. The "function" is as follows, h...
- Mike Parker (9/11) Apr 26 2007 Either I'm misunderstanding you, or you are misunderstanding the code.
- okibi (2/15) Apr 26 2007 So how do I get a variable from the object after the constructor returns...
- Ary Manzana (7/86) Apr 26 2007 It would be much more simpler if you'd tell us your problem instead of
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!
Apr 26 2007
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?
Apr 26 2007
Johan Granberg Wrote:okibi wrote:It doesn't throw an error, however it grabs a null value instead of what the function sets the variable to.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?
Apr 26 2007
okibi Wrote:Johan Granberg Wrote: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?okibi wrote:It doesn't throw an error, however it grabs a null value instead of what the function sets the variable to.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?
Apr 26 2007
okibi wrote:okibi Wrote: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.Johan Granberg Wrote: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?okibi wrote:It doesn't throw an error, however it grabs a null value instead of what the function sets the variable to.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?
Apr 26 2007
Johan Granberg Wrote:okibi wrote: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?okibi Wrote: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.Johan Granberg Wrote: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?okibi wrote:It doesn't throw an error, however it grabs a null value instead of what the function sets the variable to.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?
Apr 26 2007
okibi wrote:Johan Granberg Wrote:I can't really tell without seeing more code, the most important information is where the function that creates myStr is called from followed by the functions that creates myStr itself.okibi wrote: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?okibi Wrote: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.Johan Granberg Wrote: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?okibi wrote:It doesn't throw an error, however it grabs a null value instead of what the function sets the variable to.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?
Apr 26 2007
Johan Granberg Wrote:okibi wrote:It really is that simple of code though. The "function" is as follows, however: void setmyStr(Button Button) { myStr = test text"; } This function responds to a button press on the window created in this().Johan Granberg Wrote:I can't really tell without seeing more code, the most important information is where the function that creates myStr is called from followed by the functions that creates myStr itself.okibi wrote: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?okibi Wrote: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.Johan Granberg Wrote: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?okibi wrote:It doesn't throw an error, however it grabs a null value instead of what the function sets the variable to.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?
Apr 26 2007
okibi wrote: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?Either I'm misunderstanding you, or you are misunderstanding the code. When you say 'it doesn't wait for the instance of myPopUp to finish', what do you mean? When you create an object instance, the constructor for that object is called. When the constructor returns, the object has been created. No other methods on that object are executed. Any initialization that needs to be done, such as that of myStr, should take place in the constructor. If you do not initialize myStr in the constructor, then of course it will be null when you try to access it.
Apr 26 2007
Mike Parker Wrote:okibi wrote:So how do I get a variable from the object after the constructor returns?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?Either I'm misunderstanding you, or you are misunderstanding the code. When you say 'it doesn't wait for the instance of myPopUp to finish', what do you mean? When you create an object instance, the constructor for that object is called. When the constructor returns, the object has been created. No other methods on that object are executed. Any initialization that needs to be done, such as that of myStr, should take place in the constructor. If you do not initialize myStr in the constructor, then of course it will be null when you try to access it.
Apr 26 2007
okibi escribió:Johan Granberg Wrote: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_patternokibi wrote: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?okibi Wrote: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.Johan Granberg Wrote: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?okibi wrote:It doesn't throw an error, however it grabs a null value instead of what the function sets the variable to.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?
Apr 26 2007