CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 1999
    Posts
    327

    how can I get a member variable from another class

    I have the following setup:

    CHudForm:oPrint()
    { UpdateData(TRUE);
    extern void PrintForm1(CPage* pPage);
    PrintForm1(ps);
    delete ps;
    }



    and then the function PrintForm1 in a separate file has the structure;

    void PrintForm1()
    {
    }



    How can I get a member variable from the CHudForm class? I tried setting up

    void PrintForm1()
    { CHudForm vdlg;
    vdlg.m_edit;
    }



    but this isn't getting the value. I don't understand how I can get a member variable from the class CHudForm. Any response any one can give me will be greatly appreciated.




  2. #2
    Join Date
    Apr 1999
    Posts
    306

    Re: how can I get a member variable from another class

    Try this:

    CHudForm::m_member;

    if this does not work try by making a member varialbe to your app class and get it by calling ((CMyApp*)AfxGetApp())->m_member


  3. #3
    Join Date
    May 1999
    Posts
    327

    Re: how can I get a member variable from another class

    The scope resolution operator only works for global variables. Calling;

    ((CMyApp*)AfxGetApp())->m_member



    retrieved the program title. Do you know of any other commands which can retrieve a member variable from a class? I appreciate any response any one can give me.


  4. #4
    Guest

    Re: how can I get a member variable from another class

    Depends on which member of which class you are trying to get, but most cases the members of a class have get's and set's, somewhere in the inheritance tree.

    direct access of a member would be Class->m_Member or Class.m_Member, only if the member had public access.

    Hope this helps


  5. #5
    Join Date
    Apr 1999
    Location
    Frankfurt, Germany
    Posts
    113

    Re: how can I get a member variable from another class

    I'm not sure whether I understand Your problem.
    Is it right that Your code compiles and runs without any problems and the only thing is, that m_edit stores the wrong value?
    Then Your problem is, that it is not possible to get the member variables of a CLASS (unless they're static but that's another problem) but only of an OBJECT (or instance) of this class. Different objects of the same class may have different values in their member variables. As far as I can see You have an object of CHudForm with some data You want to access and so You create a new object (i.e. another one) to get the data.
    In PrintForm1 You have to get access to the object that called it. A possible way to do this is to change PrintForm1 so that it takes a second parameter of the type CHudForm* hf and call PrintForm1(ps, this). Now hf->m_edit should have the right value...
    HTH
    Rudolf


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured