How to use a variable declared in another class in Visual C++
-
Yes this is actually what I want to do! :) But how can I pass it as an argument?? Sorry for asking again, I'm new in programming.
When
InsertItems()
is called from the parent dialog:class CDataDialog : public CDialog
{
// ...
void InsertItems(int nCon);
// ...
// Optional:
// int m_nCon;
};void CDataDialog::InsertItems(int nCon)
{
// Optional:
// m_nCon = nCon;
// ...
for (int i = 0; i < nCon)
// ...
}If it is not called from the parent dialog pass it using a setter function:
class CDataDialog : public CDialog
{
// ...
void SetCon(int nCon) { m_nCon = nCon; }
int m_nCon;
// ...
}; -
When
InsertItems()
is called from the parent dialog:class CDataDialog : public CDialog
{
// ...
void InsertItems(int nCon);
// ...
// Optional:
// int m_nCon;
};void CDataDialog::InsertItems(int nCon)
{
// Optional:
// m_nCon = nCon;
// ...
for (int i = 0; i < nCon)
// ...
}If it is not called from the parent dialog pass it using a setter function:
class CDataDialog : public CDialog
{
// ...
void SetCon(int nCon) { m_nCon = nCon; }
int m_nCon;
// ...
};But by this way how Can I give the value of the variable when the executable runs?Variable m_DialogCon was declared in CFeaturesDialog and when I give the value in the first dialog of executable I want to create the list in the second dialog (CDataDialog).
-
But by this way how Can I give the value of the variable when the executable runs?Variable m_DialogCon was declared in CFeaturesDialog and when I give the value in the first dialog of executable I want to create the list in the second dialog (CDataDialog).
It depends on when and how you are creating the dialog and calling its member functions:
void CFeaturesDialog::SomeFunc()
{
CDataDialog *pDlg = new CDataDialog(this);
// When InsertItems() is called here
pDlg->InsertItems(m_DialogCon);
// Or pass value
pDlg->SetCon(m_DialogCon);
pDlg->DoModal();
delete pDlg;
} -
Hello everyone, I have created a list view(Declared in CDataDialog) with data . I want the list view to have as rows-cells as the conductors so I used :for (int i = 1; i<= m_DialogCon; i++) (m_DialogCon is the variable which representes the number of conductors but was declared in CFeaturesDialog). How could I use m_DialogCon variable in CDataDialog? Here is the code:
void CDataDialog::InsertItems()
{
HWND hWnd = ::GetDlgItem(m_hWnd, IDC_LIST1);// Set the LVCOLUMN structure with the required // column information LVCOLUMN list; list.mask = LVCF\_TEXT | LVCF\_WIDTH | LVCF\_FMT | LVCF\_SUBITEM; list.fmt = LVCFMT\_LEFT; list.cx = 50; list.pszText = L"Conductor"; list.iSubItem = 0; //Inserts the column ::SendMessage(hWnd, LVM\_INSERTCOLUMN, (WPARAM)0, (WPARAM)&list); list.cx = 100; list.pszText = L"Resistivity"; list.iSubItem = 1; ::SendMessage(hWnd, LVM\_INSERTCOLUMN, (WPARAM)1, (WPARAM)&list); list.cx = 100; list.pszText = L"Permeability"; list.iSubItem = 2; ::SendMessage(hWnd, LVM\_INSERTCOLUMN, (WPARAM)2, (WPARAM)&list); list.cx = 100; list.pszText = L"Outer Diameter"; list.iSubItem = 3; ::SendMessage(hWnd, LVM\_INSERTCOLUMN, (WPARAM)3, (WPARAM)&list); list.cx = 100; list.pszText = L"Inner Diameter"; list.iSubItem = 4; ::SendMessage(hWnd, LVM\_INSERTCOLUMN, (WPARAM)4, (WPARAM)&list); list.cx = 100; list.pszText = L"Rdc"; list.iSubItem = 5; ::SendMessage(hWnd, LVM\_INSERTCOLUMN, (WPARAM)5, (WPARAM)&list); list.cx = 100; list.pszText = L"x component"; list.iSubItem = 6; ::SendMessage(hWnd, LVM\_INSERTCOLUMN, (WPARAM)6, (WPARAM)&list); list.cx = 100; list.pszText = L"y component"; list.iSubItem = 7; ::SendMessage(hWnd, LVM\_INSERTCOLUMN, (WPARAM)7, (WPARAM)&list); // Inserts first Row with four columns . for (int i = 1; i<= m\_DialogCon; i++)//here is the variable m\_DialogCon //from CFeaturesDialog { SetCell(hWnd, L"1", 0, 0); SetCell(hWnd, L"0.0000386063", 0, 1); SetCell(hWnd, L"1", 0, 2); SetCell(hWnd, L"0.025146", 0, 3); SetCell(hWnd, L"0.00971", 0, 4); SetCell(hWnd, L"0.09136", 0, 5); SetCell(hWnd, L"0", 0, 6); SetCell(hWnd, L"15.24", 0, 7); }
}
void CDataDialog::SetCell(HWND hWnd1,
CString value, int nRow, int nCol)
{
TCHAR szString[256];
wsprintf(szString, value, 0);//Fill the LVITEM structure with the //values given as parameters. LVITEM lvItem;
Well, the whole point of C++, any object oriented code in fact, is that you DONT share variable. So, you need to rethink your design, and how these two classes interact.
-
It depends on when and how you are creating the dialog and calling its member functions:
void CFeaturesDialog::SomeFunc()
{
CDataDialog *pDlg = new CDataDialog(this);
// When InsertItems() is called here
pDlg->InsertItems(m_DialogCon);
// Or pass value
pDlg->SetCon(m_DialogCon);
pDlg->DoModal();
delete pDlg;
} -
It depends on when and how you are creating the dialog and calling its member functions:
void CFeaturesDialog::SomeFunc()
{
CDataDialog *pDlg = new CDataDialog(this);
// When InsertItems() is called here
pDlg->InsertItems(m_DialogCon);
// Or pass value
pDlg->SetCon(m_DialogCon);
pDlg->DoModal();
delete pDlg;
}I tried this
CDataDialog *pDlg = new CDataDialog(this);
// When InsertItems() is called here
pDlg->InsertItems(m_DialogCon);as you wrote me above but I have one more function where error appears in function InsertItems()
BOOL CDataDialog::OnInitDialog()
{
//CDialog::OnInitDialog();
ListView_SetExtendedListViewStyle(::GetDlgItem
(m_hWnd, IDC_LIST1), LVS_EX_FULLROWSELECT |
LVS_EX_GRIDLINES);InsertItems ();//error shows:too few arguments in function call ::ShowWindow(::GetDlgItem(m\_hWnd, IDC\_EDIT1), SW\_HIDE); return TRUE;
}
Thank you in advance! :)
-
Well, the whole point of C++, any object oriented code in fact, is that you DONT share variable. So, you need to rethink your design, and how these two classes interact.
-
I was not sure I should make that point, since it isnt really very helpful, but fundamentally this isnt a programming question, its an architecture one, and the OP clearly needs to go back to basics on this and rework his architecture. Of course you can use 'friend' classes, or use the classic C# hack Getxxxx() Setxxx() functions. A hack so bad you wonder why bother using C# and why not just use C. Anyway, enough of my ramblings.
-
I tried this
CDataDialog *pDlg = new CDataDialog(this);
// When InsertItems() is called here
pDlg->InsertItems(m_DialogCon);as you wrote me above but I have one more function where error appears in function InsertItems()
BOOL CDataDialog::OnInitDialog()
{
//CDialog::OnInitDialog();
ListView_SetExtendedListViewStyle(::GetDlgItem
(m_hWnd, IDC_LIST1), LVS_EX_FULLROWSELECT |
LVS_EX_GRIDLINES);InsertItems ();//error shows:too few arguments in function call ::ShowWindow(::GetDlgItem(m\_hWnd, IDC\_EDIT1), SW\_HIDE); return TRUE;
}
Thank you in advance! :)
You are calling
InsertItems()
fromOnInitDialog()
. Then you can't pass the variable from the parent dialog and have to callSetCon()
from the parent dialog. Remove the argument fromInsertItems()
and usem_nCon
within that function. -
I was not sure I should make that point, since it isnt really very helpful, but fundamentally this isnt a programming question, its an architecture one, and the OP clearly needs to go back to basics on this and rework his architecture. Of course you can use 'friend' classes, or use the classic C# hack Getxxxx() Setxxx() functions. A hack so bad you wonder why bother using C# and why not just use C. Anyway, enough of my ramblings.