Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use a variable declared in another class in Visual C++

How to use a variable declared in another class in Visual C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
13 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L lolici

    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;
    
    J Offline
    J Offline
    Jochen Arndt
    wrote on last edited by
    #2

    Just pass it as argument to InsertItems(). If you need it also with other functions save it in a member variable. But it is actually not used in a meaningful context in your code. You are just overwriting the cell content multiple times with the same values.

    L 1 Reply Last reply
    0
    • J Jochen Arndt

      Just pass it as argument to InsertItems(). If you need it also with other functions save it in a member variable. But it is actually not used in a meaningful context in your code. You are just overwriting the cell content multiple times with the same values.

      L Offline
      L Offline
      lolici
      wrote on last edited by
      #3

      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.

      J 1 Reply Last reply
      0
      • L lolici

        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.

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #4

        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;
        // ...
        };

        L 1 Reply Last reply
        0
        • J Jochen Arndt

          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;
          // ...
          };

          L Offline
          L Offline
          lolici
          wrote on last edited by
          #5

          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).

          J 1 Reply Last reply
          0
          • L lolici

            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).

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #6

            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;
            }

            L 2 Replies Last reply
            0
            • L lolici

              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;
              
              M Offline
              M Offline
              Munchies_Matt
              wrote on last edited by
              #7

              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.

              _ 1 Reply Last reply
              0
              • J Jochen Arndt

                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;
                }

                L Offline
                L Offline
                lolici
                wrote on last edited by
                #8

                Thank you very much for your help!!! :)

                1 Reply Last reply
                0
                • J Jochen Arndt

                  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;
                  }

                  L Offline
                  L Offline
                  lolici
                  wrote on last edited by
                  #9

                  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! :)

                  J 1 Reply Last reply
                  0
                  • M Munchies_Matt

                    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.

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #10

                    Very good point !

                    M 1 Reply Last reply
                    0
                    • _ _Flaviu

                      Very good point !

                      M Offline
                      M Offline
                      Munchies_Matt
                      wrote on last edited by
                      #11

                      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.

                      L 1 Reply Last reply
                      0
                      • L lolici

                        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! :)

                        J Offline
                        J Offline
                        Jochen Arndt
                        wrote on last edited by
                        #12

                        You are calling InsertItems() from OnInitDialog(). Then you can't pass the variable from the parent dialog and have to call SetCon() from the parent dialog. Remove the argument from InsertItems() and use m_nCon within that function.

                        1 Reply Last reply
                        0
                        • M Munchies_Matt

                          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.

                          L Offline
                          L Offline
                          lolici
                          wrote on last edited by
                          #13

                          I try everything you propose!!every answer is helpful as I trying something like this for the first time!!thank you for your proposes!! :)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups