How can I take the valor of a variable of a .cpp to other .cpp?

Hi,I need to take a valor of a variable CEdit of one .cpp and use it in other .cpp.

I have put this but it doesnt work:

static CString MyVariable;

The compiler works well but in the execution I see that there isnt valor in the new variable.

Help



Answer this question

How can I take the valor of a variable of a .cpp to other .cpp?

  • No-spam Sam

    Sarath. wrote:
    class CMyDialog
    {
    CEdit m_Edit;
    public:
    CString GetEditValue() {m_Edit.GetWindowText()};
    }

    other dialog or CPP file

    CString strText = m_pDlg->GetEditValue();

    You may have to pass the dialog objects pointer to the other class to access the function.

    Another way is to use GetDlgItemText API if you know the resource name.

    I'm not sure whether Ihave answered you or not.



    Jeje,I did this this morning and it didnt work.I dont know the reason but when I tried to write in the file,it didnt write anything,only it wrote the information of CEdit of the last dialog,which wrote in the file.

  • adi151478

    class CMyDialog
    {
    CEdit m_Edit;
    public:
    CString GetEditValue() {m_Edit.GetWindowText()};
    }

    other dialog or CPP file

    CString strText = m_pDlg->GetEditValue();

    You may have to pass the dialog objects pointer to the other class to access the function.

    Another way is to use GetDlgItemText API if you  know the resource name.

    I'm not sure whether Ihave answered you or not.



  • Sarah Cameron

    is the specified static variable part of the dialog class, the you will not be able to access.
    You may to provide some access mechanism to access the file. It is not a good practice to make the control variables static.
    If you say what you want to achieve, we can say some better way to do the same.

    The best way is to provide interface to access or get the value from CEdit or CString.

    The extern mechanism will work fine for sharing data. The way you used it sometimes wrong


  • ReneeC

    Static will be valid only that file.
    You can make it as extern.

    declare extern CString MyVariable;
    in which CPP file where you are using the same variable.
    Also dont forget to remove the static from the original definition

    I have one suggestion is that, if you are using OOP, this method is not good.
    usually, such variables will be declared in the header files. or provide some interfaces to return the handle or value of the same. the meaning of static variables in global scope is to make available to that module (cpp) file only.



  • NateRickard

    I proved to doing a method that give me the valor of the CEdit variable and in the last dialog call this mehod to give me the information that I need to write in the file,but this doesnt work neither,it seems that the variable hasnt any valor.

  • stswordman

    I proved what you said and it doesnt work neither.My application is based in dialogs:

    It has 4 dialogs with one CEdit each one.In the last dialog I have to write in a file all the previous information.In other words in the last dialog(his .cpp or .h,as you want) I have to have access to all variables of CEdits of previous dialogs.Now I have explained better,sorry.Ideas

  • How can I take the valor of a variable of a .cpp to other .cpp?