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?
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
}
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 is not legal.
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.
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.
Re: Wish to access previous CDocument items when a new one is opened in MFC.
CVSTADoc *pDoc = (CVSTADoc*) myDoc;
Note the * in the cast.