Solved

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

Posted on 2001-08-09
7
216 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:nvlinh
7 Comments
 
LVL 1

Expert Comment

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

(CADlg*)GetParent()->m_string = "New value";
0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6371521
I'not sure either, but did I get it: You have a dlg in dlg?

class CADlg
{

  protected:

  CBDlg m_dlgB; // ??
}



0
 
LVL 1

Expert Comment

by:Tim_Musschoot
ID: 6371601
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
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 31

Expert Comment

by:Zoppo
ID: 6371637
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
0
 

Accepted Solution

by:
Thomas_Dzieran earned 25 total points
ID: 6375892
There are many ways to do it.
One way is could be this(It depends on your requirements):

When you construct you BDlg object in ADlg you can pass "this" pointer to its constructor.

CBDlgj dlg(this);
dlg.DoModal();


In the header of BDlg include ADlg.h:
#include "ADlg.h"

create a member vareiable of Adlg type:
CADlg* m_pAldg;

In the constructor of your BDlg set your BDlg member variable to pParent:
CBDlg::CBDlg(CWnd* pParent /*=NULL*/)
:CDialog(CBDlg::IDD, pParent)
{
  /.../
  m_pAdlg = (CADlg*)pParent;
}

Now in your CBdlg class you can use:
m_pAdlg->m_SomeVariable = somevalue.
m_pAdlg->SomeFunction();

This is just one way(simple), thera are other ways to.
If you have any problems, I can send you sample app
with two dlgs.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6420499
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!

0
 

Expert Comment

by:Thomas_Dzieran
ID: 6425830
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


0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 95
tripleUp challenge 7 78
Line meaning 9 85
how to use laptop or pad camera in vb.net windows application 2 70
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question