Click to See Complete Forum and Search --> : exchange vaiable among dialog


kevin shen
August 20th, 1999, 08:37 PM
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

Dmitriy
August 20th, 1999, 10:07 PM
1)Create variable
dialog1* p1;
in dialog2;
2)before DoModal
set this variable

dialog2.p1=this;

and now you can do it.


Dmitriy, MCSE

kevin shen
August 23rd, 1999, 02:00 PM
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

ChristianM
August 23rd, 1999, 02:05 PM
in d2.. try with :
d1 *test = GetParent();
this function will return a pointer on your d1.. and access variable like this :
test->var;

Dmitriy
August 23rd, 1999, 02:57 PM
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::d1()
{
m_d1.m_pd1=this;//pointer to d1 from d2
}


Dmitriy, MCSE

kevin shen
August 23rd, 1999, 05:40 PM
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::d1()
{
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

Dmitriy
August 23rd, 1999, 05:58 PM
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::d1()
{
m_d1.m_pd1=this;//pointer to d1 from d2
}


Dmitriy, MCSE

kevin shen
August 23rd, 1999, 07:50 PM
I put linesclass d1;
class d2 {
d1* m_pd1;
...
}



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

But I put
d1::d1()
{
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

Dmitriy
August 23rd, 1999, 08:46 PM
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::d1()
{
m_d1.m_pd1=this;//pointer to d1 from d2
}

Dmitriy, MCSE

kevin shen
August 24th, 1999, 11:49 AM
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

Dmitriy
August 24th, 1999, 12:12 PM
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

Anthony Mai
August 26th, 1999, 05:52 PM
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::Dlg1Func()
{
Dialog2 dlg2(this);

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

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

Dmitriy
August 26th, 1999, 06:15 PM
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