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. Accessing a variable from another class

Accessing a variable from another class

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
3 Posts 2 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.
  • S Offline
    S Offline
    sugumar
    wrote on last edited by
    #1

    I have 2 class - CUserDlg,CMenuDlg. I collect a string value from CUserDlg and pass/use this value to/in CMenuDlg. I define the variable as Public in CUserDlg and Collect value for it. Public : CString szName; UserDlg.Cpp: szName = "Hai"; CDialog::EndDialog(0); CMenuDlg dlg; dlg.DoModal(); and close CUserDlg screen and calls CMenuDlg screen. I define CUserDlg object in MenuDlg header file. CUserDlg* UserData; And when I access UserData->szName in CMenuDlg.Cpp file, i get only NULL value. Pls help me out.

    M 1 Reply Last reply
    0
    • S sugumar

      I have 2 class - CUserDlg,CMenuDlg. I collect a string value from CUserDlg and pass/use this value to/in CMenuDlg. I define the variable as Public in CUserDlg and Collect value for it. Public : CString szName; UserDlg.Cpp: szName = "Hai"; CDialog::EndDialog(0); CMenuDlg dlg; dlg.DoModal(); and close CUserDlg screen and calls CMenuDlg screen. I define CUserDlg object in MenuDlg header file. CUserDlg* UserData; And when I access UserData->szName in CMenuDlg.Cpp file, i get only NULL value. Pls help me out.

      M Offline
      M Offline
      Michael P Butler
      wrote on last edited by
      #2

      sugumar wrote: I define CUserDlg object in MenuDlg header file. CUserDlg* UserData; And when I access UserData->szName in CMenuDlg.Cpp file, i get only NULL value. Where do you assign the original CUserDlg pointer to the UserData pointer. I usually pass the pointer via the constructor, but your code doesn't appear to do that. Roughly, UserDlg.cpp CUserDlg::OnOK() { CMenuDlg dlg(this) dlg.DoModal() } CMenuDlg.h // forward declare class CUserDlg; class CMenuDlg : public CDialog { public: CMenuDlg(CUserDlg* pUserDlg, CWnd* pParent = NULL); // standard constructor altered private: CUserDlg* m_pUserDlg; } CMenuDlg.cpp #include "UserDlg.h" CMenuDlg::CMenuDlg(CUserDlg* pUserDlg, CWnd* pParent) { m_pUserDlg = pUserDlg; } Then when you want to access szName void CMenuDlg::SomeFunc() { // sample use CString temp = m_pUserDlg->szName; } Michael CP Blog [^]

      S 1 Reply Last reply
      0
      • M Michael P Butler

        sugumar wrote: I define CUserDlg object in MenuDlg header file. CUserDlg* UserData; And when I access UserData->szName in CMenuDlg.Cpp file, i get only NULL value. Where do you assign the original CUserDlg pointer to the UserData pointer. I usually pass the pointer via the constructor, but your code doesn't appear to do that. Roughly, UserDlg.cpp CUserDlg::OnOK() { CMenuDlg dlg(this) dlg.DoModal() } CMenuDlg.h // forward declare class CUserDlg; class CMenuDlg : public CDialog { public: CMenuDlg(CUserDlg* pUserDlg, CWnd* pParent = NULL); // standard constructor altered private: CUserDlg* m_pUserDlg; } CMenuDlg.cpp #include "UserDlg.h" CMenuDlg::CMenuDlg(CUserDlg* pUserDlg, CWnd* pParent) { m_pUserDlg = pUserDlg; } Then when you want to access szName void CMenuDlg::SomeFunc() { // sample use CString temp = m_pUserDlg->szName; } Michael CP Blog [^]

        S Offline
        S Offline
        sugumar
        wrote on last edited by
        #3

        thanks for your immediate response and which also solves my problem.Tnahk You Michael

        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