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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to access the variables of View Class

How to access the variables of View Class

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
4 Posts 3 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.
  • N Offline
    N Offline
    nripun
    wrote on last edited by
    #1

    I created an SDI application. When i click on one of the menu items a dialog box pops-up. I want the values of a few variables that are declared as public in the View class to be accessed and modified in the dialog box. I tried creating an object of the view class in the dialog class, but doesnt work.How do i do it? Please Help.

    R C 2 Replies Last reply
    0
    • N nripun

      I created an SDI application. When i click on one of the menu items a dialog box pops-up. I want the values of a few variables that are declared as public in the View class to be accessed and modified in the dialog box. I tried creating an object of the view class in the dialog class, but doesnt work.How do i do it? Please Help.

      R Offline
      R Offline
      Rahim Rattani
      wrote on last edited by
      #2

      Use CMyView* myView = (CMyView*)AfxGetMainWnd()->GetActiveView(); to get a pointer to the view. Rahim Rattani Software Engineer, Matrix Systems (Pvt) Ltd., Karachi - Pakistan

      1 Reply Last reply
      0
      • N nripun

        I created an SDI application. When i click on one of the menu items a dialog box pops-up. I want the values of a few variables that are declared as public in the View class to be accessed and modified in the dialog box. I tried creating an object of the view class in the dialog class, but doesnt work.How do i do it? Please Help.

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        nripun wrote: I tried creating an object of the view class in the dialog class :wtf: Errr, I think this is not a good solution ;P What you need to do is, when creating your dialog, send the variables to the dialog (using a public member function) before calling DoModal. Exemple:

        CYourDialog::CDialog
        {
        // MFC stuff here....

        public:
        void SetVariable(int Value) { m_ViewVar = Value; }

        private:
        int m_ViewVar;
        }

        Then when creating your dialog you can do that:

        CYourDialog Dlg;
        Dlg.SetVariable(Variable);
        Dlg.DoModal();

        So nowm inside your dialog class know the value of the variable from your View. If you need to retrieve it, the same, add a member function to retrieve the value. Why using functions and not public variable ? In fact this is a good habit to have: your member variables should be private (or protected in case of inheritance) and they can be accessed through Get/Set methods... Hope this helps

        N 1 Reply Last reply
        0
        • C Cedric Moonen

          nripun wrote: I tried creating an object of the view class in the dialog class :wtf: Errr, I think this is not a good solution ;P What you need to do is, when creating your dialog, send the variables to the dialog (using a public member function) before calling DoModal. Exemple:

          CYourDialog::CDialog
          {
          // MFC stuff here....

          public:
          void SetVariable(int Value) { m_ViewVar = Value; }

          private:
          int m_ViewVar;
          }

          Then when creating your dialog you can do that:

          CYourDialog Dlg;
          Dlg.SetVariable(Variable);
          Dlg.DoModal();

          So nowm inside your dialog class know the value of the variable from your View. If you need to retrieve it, the same, add a member function to retrieve the value. Why using functions and not public variable ? In fact this is a good habit to have: your member variables should be private (or protected in case of inheritance) and they can be accessed through Get/Set methods... Hope this helps

          N Offline
          N Offline
          nripun
          wrote on last edited by
          #4

          Thank You, Its working.

          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