We help IT Professionals succeed at work.

CObArray in VC++ 6.0

glivie
glivie asked
on
Hi,

I need some help for the following.

I need to get some input data from a dialog box and when i "save" it, the data should be stored in a CObArray. I want to store the data as Objects(its understood  since i want to use CObArray). Once i save the data, i should be able to show the data in another dialog box using the CObArray.

Please get back with some working code.

Thankyou,
glivie.





Comment
Watch Question

Meir RivkinFull stack Software Engineer
BRONZE EXPERT
Commented:
what kind of data? structures, classes, simple variables???

if the data is varied u gonna have a problem store them in one CObArray unless u will know exactly their type in each one of the array's cells.

say u wanna store data which is a class contains string and integer.
here an example:

class CData
{
int m_value;
CString m_str;

public:
CData(CString& str, int val):m_str(str),m_value(val){}
CData(}:m_str(""),m_value(0){}

void SetData(CString& str, int val){m_str = str;
m_value = val;
}
void GetData(CString& str, int& val){
str = m_str; val = m_value;
}
}

create some new data:
CData* pData1 = new CData("jonny", 15);
CData* pData2 = new CData("bob", 23);
m_obArray.Add(pData1);
m_obArray.Add(pData2);

now your CObArray member should be declared in the dialog with an access fucntion:

CObArray& GetDataArray(){return CObArray;}

example of using this function:

CObArray& obArray = ptrMyDialog->GetDataArray();

for(int i=0; i<obArray.GetSize(); i++)
{
int val;
CString str;

//must do casting to the class CData
((CData*)obArray[i])->GetData(str, val);

....do somthing with the data....
}


more OO way is to create CObArray derived class which inherits the functionality of the CObArray and maintain your data inside, for example:


class CDataInfoArray : public CObArray
{
public:
void Add(CData* pData){CObArray::Add((CObject*)pData);}
void RemoveAt(int index, int count=1){CObArray::RemoveAt(index, count);}
};

cheers

Meir RivkinFull stack Software Engineer
BRONZE EXPERT

Commented:
if u need a sample post yor e-mail...
Commented:

Modify Ur Dialogs contructor and pass a CObjArray reference or pointer to Ur first dialog class

see the Dialog class



class CNewDlg : public CDialog
{
// Construction
public:
     CNewDlg( /*Added*/ CObArray * pUrObjArray, CWnd* pParent = NULL)   // standard constructor
{
  m_pURObjArray = pUrObjArray;


}

CObArray* m_pURObjArray;
//keepping the ObjArray pointer as member because u want
//to fill data within other member functions in this class

}

CObjArray URObjArray;

URObjArray.RemoveAll();

CNewDlg dlg( &URObjArray, parentWnd );
dlg.Domodal(); //or create what u want


//After dialog operations U'l get the array with the added objects.because Ur passing the reference to CNewDlg contructor

Use the same method to pass obarray to next Dialog and use that objects.be carefull that don't delete the objects whose pointers are added to the array . if u delete that object and use that pointer on the next dialog application will crash crash crash.



best of luck
jobs


BRONZE EXPERT
Author of the Year 2009

Commented:
Is this question about saving the data to disk and reading it back later (say, next week) or is it more about passing an array full of data from one dialog to another in a running program?

-- Dan
BRONZE EXPERT

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Split between job_s and sedgewick
Please leave any comments here within the next four days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

khkremer
EE Cleanup Volunteer

Explore More ContentExplore courses, solutions, and other research materials related to this topic.