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

passin a variable from another class

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpvisual-studiohelpquestion
10 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.
  • N Offline
    N Offline
    Natural_Demon
    wrote on last edited by
    #1

    HRESULT Cfirstw32mfcDlg::OnButtonOK(IHTMLElement* /*pElement*/) { UpdateData(TRUE); if(( EDIT1 == "" )&&( EDIT2 == "" )) { AfxMessageBox("You must provide a username and a password or click Cancel",MB_ICONSTOP); return NULL; } AfxGetApp()->WriteProfileString("Settings", "email", EDIT1); AfxGetApp()->WriteProfileString("Settings", "password", EDIT2); //CAddzonename* m_Zonename; //LPCTSTR m_Zonename = m_Zonename; /* if(m_Zonename) { AfxGetApp()->WriteProfileString("Settings", "Zonename", m_Zonename); } */ return 0; } ....................................................... // CAddzonename dialog header file class CAddzonename : public CDialog { DECLARE_DYNAMIC(CAddzonename) public: // global variable CString m_Zonename; ....................................................... i have tried this this ... CAddzonename* m_Zonename; CString m_Zonename2 = m_Zonename; if(m_Zonename2) { AfxGetApp()->WriteProfileString("Settings", "Zonename", m_Zonename2); } and return this ... j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(222) : error C2440: 'initializing' : cannot convert from 'CAddzonename *' to 'ATL::CStringT' with [ BaseType=char, StringTraits=StrTraitMFC ] No constructor could take the source type, or constructor overload resolution was ambiguous what am i doing wrong? kind regards, marco

    C 1 Reply Last reply
    0
    • N Natural_Demon

      HRESULT Cfirstw32mfcDlg::OnButtonOK(IHTMLElement* /*pElement*/) { UpdateData(TRUE); if(( EDIT1 == "" )&&( EDIT2 == "" )) { AfxMessageBox("You must provide a username and a password or click Cancel",MB_ICONSTOP); return NULL; } AfxGetApp()->WriteProfileString("Settings", "email", EDIT1); AfxGetApp()->WriteProfileString("Settings", "password", EDIT2); //CAddzonename* m_Zonename; //LPCTSTR m_Zonename = m_Zonename; /* if(m_Zonename) { AfxGetApp()->WriteProfileString("Settings", "Zonename", m_Zonename); } */ return 0; } ....................................................... // CAddzonename dialog header file class CAddzonename : public CDialog { DECLARE_DYNAMIC(CAddzonename) public: // global variable CString m_Zonename; ....................................................... i have tried this this ... CAddzonename* m_Zonename; CString m_Zonename2 = m_Zonename; if(m_Zonename2) { AfxGetApp()->WriteProfileString("Settings", "Zonename", m_Zonename2); } and return this ... j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(222) : error C2440: 'initializing' : cannot convert from 'CAddzonename *' to 'ATL::CStringT' with [ BaseType=char, StringTraits=StrTraitMFC ] No constructor could take the source type, or constructor overload resolution was ambiguous what am i doing wrong? kind regards, marco

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Natural_Demon wrote: AfxGetApp()->WriteProfileString("Settings", "Zonename", m_Zonename2); I'm assuming that your application overrides WriteProfileString to take a CAddzonename pointer. AfxGetApp will return an instance of the base class, you need to cast it to the specialised class that contains this method. as it stands, it's trying to call the base class method, as that's all it can see. I think. It's kind of hard to wade through all of that badly formated code. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

      N 1 Reply Last reply
      0
      • C Christian Graus

        Natural_Demon wrote: AfxGetApp()->WriteProfileString("Settings", "Zonename", m_Zonename2); I'm assuming that your application overrides WriteProfileString to take a CAddzonename pointer. AfxGetApp will return an instance of the base class, you need to cast it to the specialised class that contains this method. as it stands, it's trying to call the base class method, as that's all it can see. I think. It's kind of hard to wade through all of that badly formated code. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

        N Offline
        N Offline
        Natural_Demon
        wrote on last edited by
        #3

        //CAddzonename* m_Zonename; <-- seems to be good, but how to use it?? if(m_Zonename) { AfxGetApp()->WriteProfileString("Settings", "Zonename", m_Zonename); } returns ... j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(224) : error C2065: 'm_Zonename' : undeclared identifier BTW, i tried that before Bad = knowing 2 much

        C 1 Reply Last reply
        0
        • N Natural_Demon

          //CAddzonename* m_Zonename; <-- seems to be good, but how to use it?? if(m_Zonename) { AfxGetApp()->WriteProfileString("Settings", "Zonename", m_Zonename); } returns ... j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(224) : error C2065: 'm_Zonename' : undeclared identifier BTW, i tried that before Bad = knowing 2 much

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          That's a whole different problem. m_Zonename does not exist in the class where you're trying to use it, obviously. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

          N 1 Reply Last reply
          0
          • C Christian Graus

            That's a whole different problem. m_Zonename does not exist in the class where you're trying to use it, obviously. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

            N Offline
            N Offline
            Natural_Demon
            wrote on last edited by
            #5

            how smart u are, i found that also out, but what do i need or can i do about it?? Bad = knowing 2 much

            C 1 Reply Last reply
            0
            • N Natural_Demon

              how smart u are, i found that also out, but what do i need or can i do about it?? Bad = knowing 2 much

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Natural_Demon wrote: how smart u are, i found that also out, but what do i need or can i do about it?? If you're going to be sarcastic, you can get stuffed. If you don't know how to declare or scope a variable, I suggest buying and reading C++ for dummies before asking questions here. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

              N 1 Reply Last reply
              0
              • C Christian Graus

                Natural_Demon wrote: how smart u are, i found that also out, but what do i need or can i do about it?? If you're going to be sarcastic, you can get stuffed. If you don't know how to declare or scope a variable, I suggest buying and reading C++ for dummies before asking questions here. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                N Offline
                N Offline
                Natural_Demon
                wrote on last edited by
                #7

                i'm not getting sarcastic, but dont tell me it's obivious. i'm trying to find out how, i'm already reading stuf the whoile weekend andf i have read about creating a global variable, but when i copy(adapting the code afcourse) the code, visual doesn't accept it. somehow. Bad = knowing 2 much

                C 1 Reply Last reply
                0
                • N Natural_Demon

                  i'm not getting sarcastic, but dont tell me it's obivious. i'm trying to find out how, i'm already reading stuf the whoile weekend andf i have read about creating a global variable, but when i copy(adapting the code afcourse) the code, visual doesn't accept it. somehow. Bad = knowing 2 much

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  Global variables are bad design anyhow. However, in C++, to declare something as global ( that is, not within the scope of any class ), it will still only be visible in files where the file that declares it is included. I found the best way to do this is to declare stuff in stdafx.h. But a better option would be to declare it within the class that you're trying to get at with AfxGetApp(), although you still need to cast that, as I said before. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                  N 1 Reply Last reply
                  0
                  • C Christian Graus

                    Global variables are bad design anyhow. However, in C++, to declare something as global ( that is, not within the scope of any class ), it will still only be visible in files where the file that declares it is included. I found the best way to do this is to declare stuff in stdafx.h. But a better option would be to declare it within the class that you're trying to get at with AfxGetApp(), although you still need to cast that, as I said before. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                    N Offline
                    N Offline
                    Natural_Demon
                    wrote on last edited by
                    #9

                    i tried to cast like this... CAddzonename* m_Zonename; //CString m_Zonename2 = m_Zonename; and it returns this ... j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(222) : error C2440: 'initializing' : cannot convert from 'CAddzonename *' to 'ATL::CStringT' with [ BaseType=char, StringTraits=StrTraitMFC ] if i do this ... CString m_Zonename2; CAddzonename* m_Zonename; m_Zonename2 = m_Zonename; j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(222) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CAddzonename *' (or there is no acceptable conversion) ................................................................... CAddzonename* m_Zonename; //m_Zonename2 = m_Zonename; //if(m_Zonename) //{ AfxGetApp()->WriteProfileString("Settings", "Zonename", m_Zonename); } this will return ... j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(226) : error C2664: 'CWinApp::WriteProfileStringA' : cannot convert parameter 3 from 'CAddzonename *' to 'LPCTSTR' Bad = knowing 2 much

                    C 1 Reply Last reply
                    0
                    • N Natural_Demon

                      i tried to cast like this... CAddzonename* m_Zonename; //CString m_Zonename2 = m_Zonename; and it returns this ... j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(222) : error C2440: 'initializing' : cannot convert from 'CAddzonename *' to 'ATL::CStringT' with [ BaseType=char, StringTraits=StrTraitMFC ] if i do this ... CString m_Zonename2; CAddzonename* m_Zonename; m_Zonename2 = m_Zonename; j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(222) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CAddzonename *' (or there is no acceptable conversion) ................................................................... CAddzonename* m_Zonename; //m_Zonename2 = m_Zonename; //if(m_Zonename) //{ AfxGetApp()->WriteProfileString("Settings", "Zonename", m_Zonename); } this will return ... j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(226) : error C2664: 'CWinApp::WriteProfileStringA' : cannot convert parameter 3 from 'CAddzonename *' to 'LPCTSTR' Bad = knowing 2 much

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      Natural_Demon wrote: CString m_Zonename2; CAddzonename* m_Zonename; m_Zonename2 = m_Zonename; Duh. Why do you expect to convert a string from an instance of your class ? Natural_Demon wrote: this will return ... j:\Visual Studio Projects\win32\firstw32mfc2\firstw32mfcDlg.cpp(226) : error C2664: 'CWinApp::WriteProfileStringA' : cannot convert parameter 3 from 'CAddzonename *' to 'LPCTSTR' Bloody hell. Follow my advice, or stop asking me. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                      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