CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Posts
    3

    Unhappy Wish to access previous CDocument items when a new one is opened in MFC.

    I open a new document, but I need access to the previous document. I can Close and Save the previous document, but I simply can't get access to the objects in that document.

    I have attached the code below

    Code:
    BOOL CVSTADoc::OnOpenDocument(LPCTSTR lpszPathName)
    {    
        if (!CDocument::OnOpenDocument(lpszPathName))
            return FALSE;
        POSITION myPos = GetDocTemplate()->GetFirstDocPosition();
        CDocument *myDoc = GetDocTemplate()->GetNextDoc(myPos);
        if (myDoc != this)
        {
                 /* Need code for this */
        }
        return TRUE;
    }
    Now I have an Array in CVSTADoc which I need access to and that document is on "myDoc" position in the if condition.
    Any help?

  2. #2
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: Wish to access previous CDocument items when a new one is opened in MFC.

    You just need to access it as a normal object, but as your the same class type, the compiler assumes you know what your doing so you can access all the variables as if they are public.

    Code:
    if (mydoc != NULL)
    {
    m_myArray = myDoc->m_myArray; // works if its a vector
    // otherwise do a manual copy
    }
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  3. #3
    Join Date
    Jul 2005
    Posts
    3

    Re: Wish to access previous CDocument items when a new one is opened in MFC.

    CVSTADoc is derived from CDocument. The array I want say "m_myArray" is an object in CVSTADoc class. myDoc on the other hand is a CDocument.

    After using your code this is the error I got...

    error C2039: 'm_myArray' : is not a member of 'CDocument'

    essentionally
    Code:
    myDoc->m_myArray
    is not legal.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Wish to access previous CDocument items when a new one is opened in MFC.

    You just need to cast myDoc to the appropriate type.

  5. #5
    Join Date
    Jul 2005
    Posts
    3

    Re: Wish to access previous CDocument items when a new one is opened in MFC.

    Ok, Im really new to MFC.

    by casting do you mean something like this?

    Code:
    CVSTADoc *pDoc = (CVSTADoc) myDoc;
    It doesn't work.

    Sorry if I sound ignorant, but like I said Im fairly new to MFC.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Wish to access previous CDocument items when a new one is opened in MFC.

    CVSTADoc *pDoc = (CVSTADoc*) myDoc;

    Note the * in the cast.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured