Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how can I access variable in View class from my dialog member function?

how can I access variable in View class from my dialog member function?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mr2003
    wrote on last edited by
    #1

    hi I have a single document , MFC project, with a few dialog classes. In one of the dialog member functions, i have to get access to a member variable of my View class. How can I do that.. its basically a pointer to the view class what is the code Ehsan Behboudi

    J P 2 Replies Last reply
    0
    • M mr2003

      hi I have a single document , MFC project, with a few dialog classes. In one of the dialog member functions, i have to get access to a member variable of my View class. How can I do that.. its basically a pointer to the view class what is the code Ehsan Behboudi

      J Offline
      J Offline
      John M Drescher
      wrote on last edited by
      #2

      Pass the dialog a pointer to the view class before you call DoModal(). John

      M 1 Reply Last reply
      0
      • J John M Drescher

        Pass the dialog a pointer to the view class before you call DoModal(). John

        M Offline
        M Offline
        mr2003
        wrote on last edited by
        #3

        how u write a sample code please? Ehsan Behboudi

        J 1 Reply Last reply
        0
        • M mr2003

          how u write a sample code please? Ehsan Behboudi

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #4

          Where do you create the dialog? From your view?? John

          M 1 Reply Last reply
          0
          • J John M Drescher

            Where do you create the dialog? From your view?? John

            M Offline
            M Offline
            mr2003
            wrote on last edited by
            #5

            i created the dialog class associated with my menu. say, click on File, then , Move,, the dialog (COrigin) pops up now,, in the dialog, when the user clicks on a button, IDC_BUTTON1 the function associated with this button is: void COrigin::OnButton1() { //I have to get a pointer to change a variable in the View or Doc class //which ever is easier. } Ehsan Behboudi

            M J 2 Replies Last reply
            0
            • M mr2003

              i created the dialog class associated with my menu. say, click on File, then , Move,, the dialog (COrigin) pops up now,, in the dialog, when the user clicks on a button, IDC_BUTTON1 the function associated with this button is: void COrigin::OnButton1() { //I have to get a pointer to change a variable in the View or Doc class //which ever is easier. } Ehsan Behboudi

              M Offline
              M Offline
              Maximilien
              wrote on last edited by
              #6

              If the data is needed in your view, you might want to keep the variable as a member of the view, and pass a pointer to the view to the dialog. or send a message ( with SendMessage ) containing the variable and handle it in the view.


              Maximilien Lincourt "Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )

              1 Reply Last reply
              0
              • M mr2003

                i created the dialog class associated with my menu. say, click on File, then , Move,, the dialog (COrigin) pops up now,, in the dialog, when the user clicks on a button, IDC_BUTTON1 the function associated with this button is: void COrigin::OnButton1() { //I have to get a pointer to change a variable in the View or Doc class //which ever is easier. } Ehsan Behboudi

                J Offline
                J Offline
                John M Drescher
                wrote on last edited by
                #7

                So the dialog is created in the view or the doc? I assume it is the view. Add variable to your dialog

                class CMyDialog : public CDialog
                {
                public:
                CMyView* m_pMyView;
                // The rest of the dialog code goes here

                }

                Then in our view

                // I assume this is the code that you show the dialog after the correct menu button is pushed

                CMyView::OnMenuButtonPushed()
                {
                CMyDialog dlg;

                dlg.m_pMyView = this;
                dlg.DoModal();

                }

                John

                1 Reply Last reply
                0
                • M mr2003

                  hi I have a single document , MFC project, with a few dialog classes. In one of the dialog member functions, i have to get access to a member variable of my View class. How can I do that.. its basically a pointer to the view class what is the code Ehsan Behboudi

                  P Offline
                  P Offline
                  PremL
                  wrote on last edited by
                  #8

                  Getting your data directly from the view in a single document application is bad design to some degree, because it breaks the SDI template. You can expect major reusability issues when you link your dialog directly to the view, since the view in turn is linked to the document. If you can help it, try declaring member variables for the data in the dialog and fill those members with the actual data from the document just before you call domodal. After the dialog returns, read the changes from the member variables back to the document. This way you’ll get a dialog class, which does not ‘directly’ depend on a particular view or document class. Concepts like this will not make your app work any different, but they will help maintenance and improve reusability. Lorenz Prem Microsoft Corporation

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups