locked
Serialization problem RRS feed

  • Question

  • I use MFC.
    This is the first time I am doing serialization.I read a book and found all this out.

    I need to serialize variables in my View class.
    The class name is
    CDrumSoftwareView

    So I put(Sorry,the code block tool did not work):
    virtual void Serialize(CArchive &ar);

    and defined it like this:
    void CDrumSoftwareView::Serialize(CArchive &ar)
    {
    CView::Serialize(ar);
    PageBoxes.Serialize(ar);//PageBoxes is CList object used in the class(it is a member).
    if(ar.IsStoring())
    {
    for(int x=0;x<16;x++)
    {
    ar<<Highlighted[x];//Highlighted is an int array.
    }
    }
    else
    {
    for(int x=0;x<16;x++)
    {
    ar>>Highlighted[x];
    }
    }
    }

    Is the code correct?
    I find that it is not saving anything at all.
    It does not save PageBoxes at all.
    What could be the problem?
    Friday, February 12, 2010 1:21 PM

Answers

  • Not all the variables in the whole application.  All the variables that will be saved to file when the user clicks Save, and loaded from file when the user clicks Open.  That's the purpose of the CDocument class: To load/save the file and hold the file data in memory.  You can't save stuff in dialogs because the dialog might not exist when the user clicks Save.  Views and dialogs can come and go but the document remains in memory.

    It's fine to put data into classes that you create, but if they contain file data they should be instantiated as members of the CDocument.
    • Marked as answer by nerdinator Sunday, February 14, 2010 7:44 AM
    Saturday, February 13, 2010 7:27 PM

All replies

  • Is it that the class has to be inherited from CObject to the serialized?

    Oh sorry,view is inherited from cobject.
    But then what is the problem that this is not called at all?
    • Edited by nerdinator Friday, February 12, 2010 2:33 PM was wrong
    Friday, February 12, 2010 2:04 PM
  • The Serialize function is part of the CDocument class, if you created a project using the framework, you will find the serialize function in your CDrumSoftwareDoc class.  Add your code above to this function.

    Your Highlighted array should be a member of the CDrumSoftwareDoc class and your view class should retrieve the values using:

    CDrumSoftwareDoc* pDoc = GetDocument();
    pDoc->Highlighted

    EDIT: PageBoxes should be in the CDrumSoftwareDoc class too
    • Edited by Eric Haddan Friday, February 12, 2010 5:08 PM x
    Friday, February 12, 2010 5:06 PM
  • I understand.
    But,I have seen the same code used for serialization of members in custom built classes(the little ones that we write).
    Are you telling me that the class should be directly inherited from the CObject class?

    By the way,is there any alternative to this inbuilt 'Serialization'?
    Friday, February 12, 2010 5:25 PM
  • PageBoxes should be inherited from CObject, either directly or indirectly.  Don't worry about how the document or view classes are created.  That is handled for you. 

    This is definitely the easier way to go if you just keep all of your data in the document class.

    If you do this and are still having problems, can you post the PageBoxes class?
    Friday, February 12, 2010 5:39 PM
  • It is not a class.Like I had said,it is a CList variable.
    Friday, February 12, 2010 5:45 PM
  • I don't see anything wrong with the code you show, but a lot depends on what you are storing in the CList class.  You're going to have to step down into it with the debugger to learn more.  Also review the CList documentation re "collection class helpers" to see if you left something out. 

    There are an enormous number of alternatives to the inbuilt serialization.  Any file format you can imagine can be written, including txt files and xml files.  You have to do a bit more work for these, but one advantage they have over MFC serialization is that you can open the file and examine it (with notepad, etc.). 

    Friday, February 12, 2010 5:48 PM
  • It's not with the CList. I put a breakpoint in the serialization function(the code I have typed above). Started Debugging,then I made some changes in the open window(interacted with the app) and saved it.It never stopped at the breakpoint. On the other hand,the same with CDoc,it ran through the function.(But my app is built in the view class:()
    Friday, February 12, 2010 5:58 PM
  • If you were hoping that your view's serialize function would magically be called then you are mistaken.  On the other hand, your CDocument class's serialize function is called by the framework when the Save command is executed. 
    Friday, February 12, 2010 6:51 PM
  • Then how do I get it to be called?It is quite disorganized to put all the variables in the CDocument class.
    So,how do I serialize variables in the View class.

    In the book I referred to,there was some class written(called CElement),with the same kind of code for serialization,
    except that there was  CObject::Serialize(ar); instead of  CView::Serialize(ar);.

    Saturday, February 13, 2010 9:50 AM
  • The code is not the problem: It's irrelevant if it never gets called!

    The MFC framework architecture puts the load/save code in the document class for lots of good reasons, including the fact that many applications have (or could have in the future) more than one view.  Your feeling that this is "quite disorganized" contradicts decades of experience writing GUI applications.  If you are learning MFC then I suggest that it is much better to work "with" it instead of trying to work "against" it.  Data that your application is going to load/save belongs in the CDocument class.  The view can easily use the built in GetDocument() function to access this data.

    Saturday, February 13, 2010 6:13 PM
  • Thanks for the response.

    All the variables in my whole application in the CDocument class?
    What about the classes that I create?
    They will have variables that need to be stored.

    There are dialog classes with variables.

    So,how do I serialize these?

    Saturday, February 13, 2010 6:46 PM
  • Not all the variables in the whole application.  All the variables that will be saved to file when the user clicks Save, and loaded from file when the user clicks Open.  That's the purpose of the CDocument class: To load/save the file and hold the file data in memory.  You can't save stuff in dialogs because the dialog might not exist when the user clicks Save.  Views and dialogs can come and go but the document remains in memory.

    It's fine to put data into classes that you create, but if they contain file data they should be instantiated as members of the CDocument.
    • Marked as answer by nerdinator Sunday, February 14, 2010 7:44 AM
    Saturday, February 13, 2010 7:27 PM
  • I decided to call the serialize function in the view class from the the one the CDocument class.
    Since I have no idea of how the serialization works in MFC,
    I don't know if there will be any harm caused if I serialize members from many different classes.
    Is there any harm?
    Monday, February 15, 2010 12:22 PM