Link to home
Start Free TrialLog in
Avatar of nvlinh
nvlinh

asked on

How to chang a variable from other class in VC++?

I have a class CADlg content a variable m_string.
In this class, i new a Object of class CBDlg. In CBDlg, I want to change the value in obj of class CADlg. HOw to implement. If possible, please send to me a example.
Thanks
Avatar of jacobkristensen
jacobkristensen

I'm not sure that I understand exactly what you want, but maybe something like this:

(CADlg*)GetParent()->m_string = "New value";
I'not sure either, but did I get it: You have a dlg in dlg?

class CADlg
{

  protected:

  CBDlg m_dlgB; // ??
}



Hello,

2 Options:

You can change the variable from class A when dialog B is called with success :

Class CADlg
{
protected:
  long Var;
public:
  long GetVar() { return Var; }
  void SetVar(long V) { Var = V;}
}

CADlg::DoFunctionIdontKnow()
{
  CBDlg Dlg;
  if (Dlg.DoModal() == IDOK)
  {
     Var = Dlg.<your_variable>
  }

}


Second option : change the variables of A realtime while using dialog B
Suppose you want to do it in function "MyFunction"

CBDlg::MyFunction
{
  long NewValue = (3 * 6) + 9;
  (CADlg*)GetParent()->SetVar(NewValue);
  // This makes sure the value of the protected    
  // variable 'Var' is set, whatever happens in
  // dialog B
}

This last option isn't a proper way to program in c++, it has something of the VB way of life...

Sux6,
Tim Musschoot
Avatar of Zoppo
Hi nvlinh,

as you can see from previous comments there may be many possibilities
to do this which depend on the kind of problem you have. So, could you
please describe your problem a bit more detailed, at lease we need to
know how the two dialogs are related together (i.e. CBDlg is modal and
displayed from within a function of CADlg, so CADlg is the parent dialog
of CBDlg ... or CBDlg is displayed after CADlg is closed with OK).
Best would be you explain what you want your program to do in this context
in details ... even you should post some relevant code how you use the
dialogs now.

ZOPPO
ASKER CERTIFIED SOLUTION
Avatar of Thomas_Dzieran
Thomas_Dzieran

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi Thomas_Dzieran,

Though you post is excellent, there is no way that it should be considered a finale "Answer"  The question is too ambiguous to be answered.  Please, post comments like everybody else.  Thanks!

Hi DanRollins,
Yes, I agree with your comment.
It is not a final answer.
There are many ways to accomplish this task.
Next time I will post comment to similar questions.
I'm fairly new to this site.
Thanks for advice.
Thomas Adam Dzieran