CArray problem
-
Working with EVc++4 on ARMV4I device. I have an CArray :
CArray<CStockEntry, CStockEntry> stockArr;
void SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr);
CArray<CStockEntry, CStockEntry>& GetStockEntryArr();in my .cpp:
void DeptStock::SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr) {
for (int i=0; i <= arr.GetUpperBound(); i++) {
CStockEntry se = arr.GetAt(i);
this->stockArr.Add(se);
}CString s1 = _T("Size of copied Array & Org:");
s1.Format(_T("%s %d %d"), s1, stockArr.GetSize(), arr.GetSize()); // This shows same number of arr & stockArr GetSize
AfxMessageBox(s1);s1.Empty();
return;
}CArray<CStockEntry, CStockEntry>& DeptStock::GetStockEntryArr() {
CString s1 = _T("Size of Returning Array:");
s1.Format(_T("%s %d"), s1, stockArr.GetSize()); // This shows 0 as GetSize
AfxMessageBox(s1);s1.Empty();
return this->stockArr;
}In between Set & Get I don't perform any operations on the stockArr. Why is while set, it is stored properly, but while retriing I get an empty array. Any help is appreciated.
Thanks Terry
-
Working with EVc++4 on ARMV4I device. I have an CArray :
CArray<CStockEntry, CStockEntry> stockArr;
void SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr);
CArray<CStockEntry, CStockEntry>& GetStockEntryArr();in my .cpp:
void DeptStock::SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr) {
for (int i=0; i <= arr.GetUpperBound(); i++) {
CStockEntry se = arr.GetAt(i);
this->stockArr.Add(se);
}CString s1 = _T("Size of copied Array & Org:");
s1.Format(_T("%s %d %d"), s1, stockArr.GetSize(), arr.GetSize()); // This shows same number of arr & stockArr GetSize
AfxMessageBox(s1);s1.Empty();
return;
}CArray<CStockEntry, CStockEntry>& DeptStock::GetStockEntryArr() {
CString s1 = _T("Size of Returning Array:");
s1.Format(_T("%s %d"), s1, stockArr.GetSize()); // This shows 0 as GetSize
AfxMessageBox(s1);s1.Empty();
return this->stockArr;
}In between Set & Get I don't perform any operations on the stockArr. Why is while set, it is stored properly, but while retriing I get an empty array. Any help is appreciated.
Thanks Terry
I tested your code as follows:
struct CStockEntry
{
int entry;
CStockEntry(int i = 0) {entry = i;}
};class DeptStock
{
public:
CArray<CStockEntry, CStockEntry> stockArr;void SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr) { for (int i=0; i <= arr.GetUpperBound(); i++) { CStockEntry se = arr.GetAt(i); this->stockArr.Add(se); } CString s1 = \_T("Size of copied Array & Org:"); s1.Format(\_T("%s %d %d"), s1, stockArr.GetSize(), arr.GetSize()); // This shows same number of arr & stockArr GetSize AfxMessageBox(s1); s1.Empty(); return; } CArray<CStockEntry, CStockEntry>& GetStockEntryArr() { CString s1 = \_T("Size of Returning Array:"); s1.Format(\_T("%s %d"), s1, stockArr.GetSize()); // This shows 0 as GetSize AfxMessageBox(s1); s1.Empty(); return this->stockArr; }
};
...
CArray<CStockEntry, CStockEntry> stockArr; stockArr.Add(CStockEntry(0)); stockArr.Add(CStockEntry(1)); stockArr.Add(CStockEntry(2)); stockArr.Add(CStockEntry(3)); stockArr.Add(CStockEntry(4)); DeptStock test; test.SetCStockEntryArr(stockArr); CArray<CStockEntry, CStockEntry> &stockArrRef = test.GetStockEntryArr();
It works fine. What aren't you showing? FWIW, calling Empty() on those local CStrings is unnecessary. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I tested your code as follows:
struct CStockEntry
{
int entry;
CStockEntry(int i = 0) {entry = i;}
};class DeptStock
{
public:
CArray<CStockEntry, CStockEntry> stockArr;void SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr) { for (int i=0; i <= arr.GetUpperBound(); i++) { CStockEntry se = arr.GetAt(i); this->stockArr.Add(se); } CString s1 = \_T("Size of copied Array & Org:"); s1.Format(\_T("%s %d %d"), s1, stockArr.GetSize(), arr.GetSize()); // This shows same number of arr & stockArr GetSize AfxMessageBox(s1); s1.Empty(); return; } CArray<CStockEntry, CStockEntry>& GetStockEntryArr() { CString s1 = \_T("Size of Returning Array:"); s1.Format(\_T("%s %d"), s1, stockArr.GetSize()); // This shows 0 as GetSize AfxMessageBox(s1); s1.Empty(); return this->stockArr; }
};
...
CArray<CStockEntry, CStockEntry> stockArr; stockArr.Add(CStockEntry(0)); stockArr.Add(CStockEntry(1)); stockArr.Add(CStockEntry(2)); stockArr.Add(CStockEntry(3)); stockArr.Add(CStockEntry(4)); DeptStock test; test.SetCStockEntryArr(stockArr); CArray<CStockEntry, CStockEntry> &stockArrRef = test.GetStockEntryArr();
It works fine. What aren't you showing? FWIW, calling Empty() on those local CStrings is unnecessary. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Hi Mark, If I add objects in array like you are adding, then my code is working perfect. But I am working different way. In my GUI, I add objects in the array in my GUI CPage4 (m_page4 member var), then on Ok button I create DeptStock deptStock object & SetCStockEntryArr(m_page4.GetStockEntries()). TILL this things are fine. Then I pass deptStock to a file to store the object details in file. g_dbOperations.Write_StockTextFile(deptStock);
// WRITE Departments MAP TO FILE
void DbOperations::Write_StockTextFile(DeptStock deptStock) {......... CString toWrite; int index = -1; OperatorDetails od = deptStock.GetOperator(); DeptDetails dd = deptStock.GetDept(); CArray<cstockentry,>& stockArr = deptStock.GetStockEntryArr(); // This returns 0 sizxe array
// stockArr = deptStock.GetStockEntryArr();
/* CStockEntry cse1(2.2f, 5, 10.2f);
CStockEntry cse2(5.0f, 3, 15.0f);
stockArr.Add(cse1);
stockArr.Add(cse2);
*/
CString str(_T("Stock Size:"));
str.Format(_T("%s %d \n %s %d"), str, stockArr.GetSize(), _T("Dept Stock Arr "), deptStock.GetStockEntryArr().GetSize());
AfxMessageBox(str); // Both DISPLAYS 0This is how the flow is. If I remove the above comments of adding objects od CStockEntry to stockArr, it gives proper results. But I have already added the array elements & not able to retrive. I have given a better & clear picture of the flow of my program & error point. Hope you can help me out to track the problem.
Thanks Terry
-
Hi Mark, If I add objects in array like you are adding, then my code is working perfect. But I am working different way. In my GUI, I add objects in the array in my GUI CPage4 (m_page4 member var), then on Ok button I create DeptStock deptStock object & SetCStockEntryArr(m_page4.GetStockEntries()). TILL this things are fine. Then I pass deptStock to a file to store the object details in file. g_dbOperations.Write_StockTextFile(deptStock);
// WRITE Departments MAP TO FILE
void DbOperations::Write_StockTextFile(DeptStock deptStock) {......... CString toWrite; int index = -1; OperatorDetails od = deptStock.GetOperator(); DeptDetails dd = deptStock.GetDept(); CArray<cstockentry,>& stockArr = deptStock.GetStockEntryArr(); // This returns 0 sizxe array
// stockArr = deptStock.GetStockEntryArr();
/* CStockEntry cse1(2.2f, 5, 10.2f);
CStockEntry cse2(5.0f, 3, 15.0f);
stockArr.Add(cse1);
stockArr.Add(cse2);
*/
CString str(_T("Stock Size:"));
str.Format(_T("%s %d \n %s %d"), str, stockArr.GetSize(), _T("Dept Stock Arr "), deptStock.GetStockEntryArr().GetSize());
AfxMessageBox(str); // Both DISPLAYS 0This is how the flow is. If I remove the above comments of adding objects od CStockEntry to stockArr, it gives proper results. But I have already added the array elements & not able to retrive. I have given a better & clear picture of the flow of my program & error point. Hope you can help me out to track the problem.
Thanks Terry
Trupti Mehta wrote:
If I add objects in array like you are adding, then my code is working perfect.
That should help you track it down. If your array is empty, debug the code that's supposed to populate it...why are no items added? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: