Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

How to pass variable value?

If I have two classes, I set a value to a string variable in one class, eg class A. How can I pass my string variable value to a method in another class, eg. class B.

I don't want to change the constructors.

Also, how can I call a method of another class (B) to do things on that class (B) from another class (A). Eg. I want to insert some text that generated by selecting options from a dialog(class B) to a textarea (class A).
0
hcyu
Asked:
hcyu
1 Solution
 
hcyuAuthor Commented:
Edited text of question
0
 
msmolyakCommented:
If the class B has a property theString, you can set it up either in the constructor (which you do not want to do) or you can create a methods setTheString(String value) and call it to set that property.

As for the second part, make class A a listener of events happenning in the dialog B. Or if the dialog B is modal you can show it to the user in the main thread (as soon as the dialog appears on the screen the main thread stops until the dialog disappears) and right after that get all the neceesary information from dialog B and apply it to A.

E.g,,

public class A
{
   ...
   B myDialog = new B();
   myDialog.setVisible()
   // The processing stops until B is made invisible (probably in B itself)
   // Now that the B is closed
   String text = myDialog.getText();
   this.textArea.append(text);
   ...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now