CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2011
    Posts
    18

    How to access a member variables from another class?

    I have an sdi program with two dialog boxes with its respectively classes (CDialog1 and CDialog2). Through a bottom in the 1st dialog box I call the second dialog box. In the second dialog box, the user gives the values for some variables. I know that I can obtain the values of the second dialog box from the first dialog box using m_variable=dlg.m_variable in the OnSecondDialog function.

    I want to write in two files the variables given by the user in both dialog boxes using the serialize function on my CApplicationDoc file. My question is: how can I access the member variables of CDialog1 and CDialog2 from the CApplicationDoc class?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to access a member variables from another class?

    Quote Originally Posted by laephy View Post
    My question is: how can I access the member variables of CDialog1 and CDialog2 from the CApplicationDoc class?
    Well, the first dialog has to set this variable to the document instance, and the second one - get this variable.
    To achieve it both dialogs need to have a pointer (or reference) to the document instance.
    Last edited by VictorN; May 10th, 2012 at 11:20 AM.
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2011
    Posts
    18

    Re: How to access a member variables from another class?

    Quote Originally Posted by VictorN View Post
    DO achieve it both dialogs need to have a pointer (or reference) to the document instance.
    I am newbie on visual c++ and I have a dummy question. Where is better to define the pointer in the document class CApplicationDoc.h:

    CDialog1::*pointer

    or in the Dialog class CDialog.h:

    CApplicationDoc::*pointer ?
    Last edited by laephy; May 10th, 2012 at 10:51 AM.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to access a member variables from another class?

    Quote Originally Posted by laephy View Post
    CDialog1::*pointer

    or in the Dialog class CDialog.h:

    CApplicationDoc::*pointer ?
    I have no ideas what CDialog1::*pointer is, nor have any clue about CDialog.h.

    Usually, if you have two dialogs CDialog1 and CDialog2 that need to access the CApplicationDoc class you declare a variable in each dialog:
    Code:
    CApplicationDoc* m_pDoc;
    , set it to NULL in ctors, and implement method to set this document pointer in each dialog:
    Code:
    void SetDocument(CApplicationDoc* pAppDoc) {m_pDoc = pAppDoc;}
    Then you call this SetDocument for each dialog before calling corresponding DoModal or Create...

    Now you'll be able to access public CApplicationDoc members/methods from within your dialogs.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured