tags:

views:

66

answers:

1

I find myself stumnped by what should be a simple problem. How do you make a class (created in a seperate file) have global scope when using forms in Microsoft C++. I can declare, initalize, and use it in a textbox leave or keypress function but when I move to a button it is out of scope (the button and textbox have global scope, but I just am stumped by how they do it). The problem is to make a clone of the class (object) and to do that it is advantageous to have the global scope. I realize this is homework, and I am not asking for code, just point me in the right direction. Thanks DJ

using namespace Circle; 

form1 code textBox1->Keypress(...) { 

Circle^ C = gcnew Circle(Convert.ToDouble(textBox1->Text)); // write to labels on form the rad, area, and circum 
} 
button1->click(...) { // I want to make a clone of the instance above 

Circle^ C = gcnew Circle(Convert.ToDouble(textBox1->Text)); 
Circle^ CL = C->Clone(C->Radius); // Clone and radius are members of the class Circle 

} 

The above code works because I created C again.

The following code will not work:

button1->clicked(...) 

{ 

Circle CL = C->Clone(C->Radius); 

}
+2  A: 

You need to use a header file and include that in your Form class. Keep in mind if the class does not need to be in the same namespace then you have to define using namespace your_namespace; in your form class as well or refer to your class object by your_namespace::class

See also: Introduction to C++

0A0D
OK- did that. I need to be more specific. When I press enter using textBox1->KeyPress I create an instance like this CIRCLE^ C = gcnew CIRCLE(6) //actual number comes from converting text to double. I then print the resulting radius (6), area, and circumference to the form. I now want to create a clone of C when I press button1->click. If I create a new instance and then use it I can create the clone, but I can't use the instance created in textBox->KeyPress. I keep getting an "Object reference not set to an instance of an object" error. Where am I going wrong?
@dj1: Can you edit your question and post some code? It is hard to tell in the comments.
0A0D
using namespace Circle;form1 codetextBox1->Keypress(...){ Circle^ C = gcnew Circle(Convert.ToDouble(textBox1->Text)); // write to labels on form the rad, area, and circum}button1->click(...){ // I want to make a clone of the instance above Circle^ C = gcnew Circle(Convert.ToDouble(textBox1->Text)); Circle^ CL = C->Clone(C->Radius); // Clone and radius are members of the class Circle}// The above code works because I created C again.// The following code will not workbutton1->clicked(...){ Circle CL = C->Clone(C->Radius);}this code gives me the error.
@dj1: I added your code to your question and properly formatted it. I still cannot see your problem. It all really does not make any sense.
0A0D
The problem is that I want an object to be persistent throughout the durtion of the program. I just don't know how to get it into the code so that it will be. Maybe it can't, but like any variable it should be available when called globally.
@dj1: It needs to be declared in global scope. If the object it was declared it dies, then it will die as well. Your other option is to declare it as static.
0A0D
Thanks for the assist. While it didn't answer the question directly it did give me some options to explore. I finished the assignment and have turned it in for a grade.