• Post Reply Bookmark Topic Watch Topic
  • New Topic

passing variables. Please HELP!!  RSS feed

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 3 classes A, B and C. Class A is the main class(therefore, objects of class B and C are in here). When a button is pressed on the user interface(defined in class A), a method in Class B is invoked. This method has a variable that needs to be accessed by class C.
Options: 1> pass that variable from the method in class B back to class A.
Then pass this variable to class C, then C can do something to it.
2> pass the variable from the method in class B to class C.
I don't know how to do either of these. I have looked in books but can't find anything like this. Could you please provide an example of how my problem could be resolved ie, eg of how one of the options may be done.
Also, objects of classes B and C have been created in class A - is this correct; should it be this way. Please help!
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// objects of classes B and C have been created in class A
Do you mean instantiated or defined?
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean instantiated (they are defined separately) - outside class A
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If thats the case, then it should be fine. In which case you option 2 is probably a non-starter. Option 1 - you could have the method you call in ClassB return this variable you need to pass to ClassC, or you could add an accessor method (i.e. getWhatever:<variable type> ) to ClassB. This making any sense?
[ March 10, 2004: Message edited by: john smith ]
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes sense but I have no idea whatsoever how you would code this and I'm not familiar with accessor methods. Could you please provide me with an example of how option 1 could be done. Thanks
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ March 10, 2004: Message edited by: john smith ]
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have basically done this but the program won't return to class A it stays in class B (ie, it stops running after it gets to the method that you have suggested). I think in effect what I'm trying to say is - how do i pass 'control' back to class A because from what I can see the program doesn't know what to do after the variable has been returned - even when class A is trying to use it! It's a bit complicated I know!
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i pass 'control' back to class A because from what I can see the program doesn't know what to do after the variable has been returned

Hmm. The basic effect of returning something from one object to another is that variable is passed to the calling object - so by 'control' is with the calling object. It can't get stuck in a class, unless the program ends or an exception is thrown. Can you post any code?
[ March 10, 2004: Message edited by: john smith ]
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here is a bit of code which I think is most relevant - there's so much of it doing other things that it's not worth putting up.
The first method simply calls a method of class B when a button is pressed.

Now class B:
has following method to pass the array/variable:

[ March 10, 2004: Message edited by: Naf Rash ]
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks OK to me. However, what are you assigning your 2 dimensional array to? You call getMatrix() but don't seem to do anything with the return value - what were you wanting to happen?
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the matrix is then going to be used in class C so basically I could make a call within the method in class A to invoke the relevant method (in class C). But regardless of this my problem remains - the program doesn't return to the required method in class A(which I have established through the use of breakpoints) it simply stops at class B. Can you think of why this is?
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. If there is no exception thrown, you are sure the method is being called and all your method does is return a value then there is absolutely no reason why it would "simply stop".
One other thing I noticed - why do you have a method which returns the parameter passed to it?
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so you think it should be like this instead?

I will try this and see how it goes. if this doesn't work I suppose I could come back to ask more questions if that's ok. Thanks
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Suppose I write a method like this:

All I am doing is returning the value I passed in! Its a pointless method. So all you are doing is passing your 2D array of doubles into a method and back out again. I'm just not sure why you would want to do this.
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help -it was really appreciated. I've moved on from my problem and now am encountering others - hopefully which I can sort out! Thanks again - I know I can ask stupid questions (but then that's the learning process!!!)

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!