CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    exchange vaiable among dialog

    Dialog1 call dialog2 <use DoModal()>.

    I know how to get dialog2 variable in dialog1, but I still don't know how to get dialog1 variable in dialog2.

    Thanks,

    kshen

    Best Regards,

    Kevin Shen

  2. #2
    Join Date
    Apr 1999
    Location
    Toronto, ON
    Posts
    713

    Re: exchange vaiable among dialog

    1)Create variable
    dialog1* p1;
    in dialog2;
    2)before DoModal
    set this variable

    dialog2.p1=this;

    and now you can do it.


    Dmitriy, MCSE

  3. #3
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    Re: exchange vaiable among dialog

    I tried your method, but it doesn't work.

    I have 2 dialogs, d1&d2. d1 call d2, I can use the normal way set in d1 like:

    d2 m_d1;//set in d1
    if(m_d1.DoModal()==IDOK){...}

    to call the d2's variables. But I can not use the same way set in d2 to call the d1's variables from d2. The compiler always shows error like "missing ;" and other errors with the

    d1 m_d2; //set in d2

    set in d2. I don't know why and how I can solve the problem. Thank you for your help!

    Regards,

    Kevin Shen

    Best Regards,

    Kevin Shen

  4. #4
    Join Date
    Jun 1999
    Location
    Canada - Québec
    Posts
    273

    Re: exchange vaiable among dialog

    in d2.. try with :
    d1 *test = GetParent();
    this function will return a pointer on your d1.. and access variable like this :
    test->var;



  5. #5
    Join Date
    Apr 1999
    Location
    Toronto, ON
    Posts
    713

    Re: exchange vaiable among dialog

    To avoid this in "d2.h" :
    before definition of the class d2

    put this line
    class d1;

    class d2 {
    d1* m_pd1;
    ...
    }

    in your cpp file for d2:
    #include "d1.h"

    in constructor:
    d1:1()
    {
    m_d1.m_pd1=this;//pointer to d1 from d2
    }


    Dmitriy, MCSE

  6. #6
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    Re: exchange vaiable among dialog

    Sorry, I don't understand you code.
    (1)Why should I avoid put lines
    class d1;

    class d2 {
    d1* m_pd1;
    ...
    }


    in "d2.h"?

    (2)As I do not state the vaiable "m_d1" (avoid state m_d1 in d2.h). How can I put these lines
    d1:1()
    {
    m_d1.m_pd1=this;//pointer to d1 from d2
    }

    in constructor? The constructor is in d2 or d1?

    Thank you for your help.

    kshen








    Best Regards,

    Kevin Shen

  7. #7
    Join Date
    Apr 1999
    Location
    Toronto, ON
    Posts
    713

    Re: exchange vaiable among dialog

    Sorry, I told it not clear. I mentioned "to avoid this" - to avoid problem that you had before (the compiler told you you have to put ";" and so on), not the lines of the code. You should to put this lines

    class d1;
    class d2 {
    d1* m_pd1;
    ...
    }

    in "d2.h"
    therefore your question N2 solved by this, I think.
    Constructor d1 and in this constructor you can put
    d1:1()
    {
    m_d1.m_pd1=this;//pointer to d1 from d2
    }


    Dmitriy, MCSE

  8. #8
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    Re: exchange vaiable among dialog

    I put linesclass d1;
    class d2 {
    d1* m_pd1;
    ...
    }



    in "d2.h". No compiler error appear, thanks!

    But I put
    d1:1()
    {
    m_d1.m_pd1=this;//pointer to d1 from d2
    }


    in d2.cpp.

    compiler shows error:m_d1 is unideclared identifier and m_pd1 must have class/struct/union type. I don't know why. I set m_pd1 as an int public value in d1.

    Thank you for your help.

    kshen






    Best Regards,

    Kevin Shen

  9. #9
    Join Date
    Apr 1999
    Location
    Toronto, ON
    Posts
    713

    Re: exchange vaiable among dialog

    Sorry, I had supposed you had this lines in your d1:

    In d1.h:

    #include "d2.h"

    class d1{
    ...
    d2 m_d1;
    ...
    }

    So, now in d1.cpp:

    d1:1()
    {
    m_d1.m_pd1=this;//pointer to d1 from d2
    }

    Dmitriy, MCSE

  10. #10
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    Re: exchange vaiable among dialog

    I tried you way.

    An error with
    m_d1.m_pd1=this;


    error C2440: '=' : cannot convert from 'class d1 *const ' to 'int'

    (1)m_d1 is#include "d2.h"

    class d1{
    ...
    d2 m_d1;
    ...
    }


    in d1.h

    (2)m_pd1 is an public int set in d1.

    Thank you for your help.

    kshen


    Best Regards,

    Kevin Shen

  11. #11
    Join Date
    Apr 1999
    Location
    Toronto, ON
    Posts
    713

    Re: exchange vaiable among dialog

    If you are using VC++ version 6.0 just send me your project without Debug and Release directories and I will give you back.


    Dmitriy, MCSE

  12. #12
    Join Date
    Jun 1999
    Location
    San Diego, CA
    Posts
    600

    Re: exchange vaiable among dialog

    Sorry Dmitriy:
    Although your code would compile and work I am against programming like that. This breaks down the whole idea of objected oriented programming and C++.
    Codes like A references to B and B also references to A is as bad as the chicken first or egg first problem.
    If A creates B, B should not be allowed to access all public members of A freely. Anything that B wants to know about A should be granted on a "need to know basis". If you hire a gardener to work for you and he begins to ask how much money you have in your bank, isn't it ridiculous?

    Having done with the philosophy. Now to solve
    your problem, do this:

    Dialog1:lg1Func()
    {
    Dialog2 dlg2(this);

    dlg2.var1 = var1;
    dlg2.var2 = var2;
    /* ... */

    if (dlg2.DoModal() = IDOK) {
    var1 = dlg2.var1;
    var2 = dlg2.var2;
    /* ... */
    }
    }



  13. #13
    Join Date
    Apr 1999
    Location
    Toronto, ON
    Posts
    713

    Re: exchange vaiable among dialog

    Sorry, Anthony:
    1) I do not show how evrybody MUST write his program. Everybody has his own style and all cames with expirience. So, I am just trying answered questions.
    2) Dificult to understand what do you want to say by your posting, because you do not explaine what is var1 and so on.
    BTW, I also against put one dialog inside other, just pointer. But it does not matter, you can write your program how you want.


    Dmitriy, MCSE

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