Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Dialog on MFC application
C++
MyDialog :
{
 int variable1;
 int variable2;
 Class1 cls = new Class1();
}




In Class1, have some function1
C++
Class1()
{
  void funtion1();
}


How to Access and return to variable1 in Class1::Function1()

C++
Class1::Function1()
{
  MyDialog dlg = new MyDialog ();
  int x = dlg->variable1; //if like this, variable1 alway=0, because in above line, i'm define new myDialog()
 dlg->variable2 = x;
}



I think to delegate on .NET but in MFC application, I can't get it done ?
Posted
Updated 6-Jul-15 22:34pm
v5
Comments
Richard MacCutchan 7-Jul-15 4:18am    
Just call Function1 using the class or object in the dialog. Although your question is not very clear.
Mạnh Lê 7-Jul-15 4:32am    
It's clearly

There is no point discussing how to solve a semantic problem as long as your code is so riddled with severe compiler errors! it's hard to tell which of those are part of the semantic problem that you describe, or whether we should simply ignore your entire code because it is not a literal copy anyway.

Please complete and compile your code first. Then copy and paste that code here - the code that you actually compiled. Just by fixing the compiler errors you'll solve at least half of the problem. After that, the actual fix will be a minor change (what Richard described in his comment). Right now however, we'd need to rewrite your entire code just for it to make sense.


P.S.: just one big tip to get started, as I've noticed you mentioned .NET:
Don't use new unless you have to! Learn the difference of the heap (dynamically allocating with new) and the stack (no dynamic allocation); prefer the stack whenever you can!

This has nothing to do with your problem, but it will solve some of the compiler errors.
 
Share this answer
 
v2
Comments
Richard MacCutchan 7-Jul-15 5:30am    
So true.
No it's not clearly. Your code does not make much sense. You create a new dialog and immediately try to use a variable that has not been initialized. You also try to create a new dialog inside your external class.
You need something more like:
C++
MyDialog :
{
 int variable1;
 int variable2;
 Class1* cls = new Class1();

 variable1 = < some value >
 variable2 = cls->function1(variable1);
}

// ...
Class1()
{
  int funtion1(int value)
  {
      int newvalue;
      // add code to use the input to create the result
      return newvalue;
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900