Thread: Question about using variables from one class in another...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Question Question about using variables from one class in another...

    hi, i'm new to this forum, but i've been looking for a solution to this for some time. if anyone could give me help, or at least point me in the right direction, i'd be greatful.


    I amd using MS Visual C++ 6.0 on a windows xp pro system. I created a SDI project with database support. I'm looking for a way to transfer a variable from my CProgramView.cpp portion to the CMainFrame.cpp portion of my code. What i really want to do is have a variable save to file when the user exits the program. I tried deleting the [x] button and such, but i can't do that. then i tried linking the [x] button to the close command inside the cprogramview.cpp that i created. that didn't work.

    so i'm looking for a way to get variable 'a' which is an int (must be long to save tho, i don't know why) from the view portion of my program to the mainframe of the program.

    any help would be nice.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Somewhere in the CProgramView class add something like below to save data. You can catch the WM_Close event in CMainFrame to save the data, or use ExitInstance() in the CWinApp-derived application class.

    Code:
    	CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
    	pFrame->m_number = 123;
    >must be long to save tho, i don't know why

    is it being saved to an sql database? The database probably doesn't use ints, just longs.
    Last edited by Ancient Dragon; 08-17-2006 at 01:32 PM.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That is extremely vague. Is this MFC, pure Win32, C++/CLI, or any other permutation of the alphabet I didn't think of?

    I'm guessing MFC due to the CMainFrame thing.

    CProgramView should not be saving any data according to the document/view architecture. CDocument should be managing all of the data. The most CView would ever do is perhaps save scroll values and other information that would aid in a persistent desktop setup.

    You can have CDocument catch the save (CMainFrame and CView won't even catch it) and then pass control to a CMainFrame save handler which would then call Serialize() for CMainFrame which would then call the corresponding Serialize() for CDocument.

    //Open file and archive
    ...
    ...
    CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
    pFrame->Serialize(ar);

    Sharing information between view, frame, and document can be a bit messy in MFC.
    Last edited by VirtualAce; 08-17-2006 at 01:35 PM.

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I recommand not using that old IDE.
    I am not sure if I understood your question well, but generally:
    When the user presses the [x] button in top-right of the a window, Windows send a message to that program that indicates program is going to be closed. You should write a message handler to catch the message and do the desired work. To do so, go to Windows programming board.

    Another recommandation: If you are new to MFC and you are learning it, stop learning it and try to learn .NET which will replace MFC soon.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Bubba
    That is extremely vague. Is this MFC, pure Win32, C++/CLI, or any other permutation of the alphabet I didn't think of?
    He mentioned CMainFrame, which only occurs in MFC.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by siavoshkc
    IAnother recommandation: If you are new to MFC and you are learning it, stop learning it and try to learn .NET which will replace MFC soon.
    Some more mis-informatin. Visual C++ 2005 includes MFC and I don't see it going away for quite a long time. I do agree with you that nobody should start new development projects with VC++ 6.0.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Visual C++ 2005 includes MFC and I don't see it going away for quite a long time.
    I read somewhere that it will survive at least for five years, but nobody should start learning it I think.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    June 2005 Article by Steve Teixeira from Microsoft.
    The Microsoft Foundation Class (MFC) Library continues to be the most popular C++ application framework in use today. Thousands of independent software vendors (ISVs) and IT departments continue to invest heavily in MFC, both for existing code and for new projects. Microsoft is committed to ensuring MFC remains a competitive and viable application framework and MFC developers are equipped to take advantage of new platform technologies
    Now why not learn MFC when there are thousands of MFC programs out there that need maintenance and upgrade? And there are more new programs being developed too.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    Quote Originally Posted by siavoshkc
    I recommand not using that old IDE.
    I am not sure if I understood your question well, but generally:
    When the user presses the [x] button in top-right of the a window, Windows send a message to that program that indicates program is going to be closed. You should write a message handler to catch the message and do the desired work. To do so, go to Windows programming board.

    Another recommandation: If you are new to MFC and you are learning it, stop learning it and try to learn .NET which will replace MFC soon.

    i can't believe i never thought of that!!!! (joking....)

    as i said, i'm using Visual C++ 6.0 i created a projcet MFC app wizzard(exe_, made it SDI with database support. i used the dialog box to create my program, and all the handlers wen to the Cprogramview.cpp code for me to fill out.

    i tried the CMainFrame* pFrame.. thing, but when i run it, the program shuts down, giving me the "send MS a report" dialog box.

    and the reason i'm using vc++ 6.0 is because it is what was given to me to use. i don't think i can convince anyone to upgrade. so it's not my decision. i just go with the flow.
    Last edited by shades234; 08-17-2006 at 02:18 PM.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Why not learning .NET when many are porting their codes to it? .NET can do whatever MFC does and much more. I don't insist on it. I am not agree with managed code really. It is up to OP to decide.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    i tried the CMainFrame* pFrame.. thing, but when i run it, the program shuts down, giving me the "send MS a report" dialog box.
    Are you sure you are running your code in debug mode?
    (Your MFC version is 4.x?)
    Last edited by siavoshkc; 08-17-2006 at 02:23 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    Quote Originally Posted by siavoshkc
    Are you sure you are running your code in debug mode?
    (Your MFC version is 4.x?)
    1)no
    2)i have no clue.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by siavoshkc
    Why not learning .NET when many are porting their codes to it?
    Because you don't have to pass the /clr switch for MFC.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Because you don't have to pass the /clr switch for MFC.
    What do you mean exactly?

    Because I don't like neither MFC nor managed things (though I like .NET programming more than MFC) I don't insist on using .NET.
    Last edited by siavoshkc; 08-17-2006 at 03:19 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I'm just giving a reason.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring SDL_Rect variables in a class?
    By pwnz32 in forum C++ Programming
    Replies: 10
    Last Post: 07-27-2008, 07:12 PM
  2. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  3. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Global variables used in a linked class, getting errors.
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2005, 12:25 AM