Newbie: pb of scope when instanciating class

C

cosmocat

Hi all
I'm a newbie in C++Builder and I need and advice about the way of
instatiating a class in order to have a scope gobal to my form

On my form, I have 2 Buttons
Button_initialise : Instanciate the class whit a parameter for the
constructor
Button_callMethod : Use a property from the class

So my code looks like:



Declaration of myclass---------------------------------
Class Myclass
}
public:

Myclass(AnsiString AccountNumber) //Constructor
__property Int Aproperty = { read=PProperty };
}



code of form 1--------------------------------------



void TForm1:Button_initialiseClick(Tobject *Sender)
//----------first proc
{Myinstance Myclass("Parameter"); // Instanciate the class}



void TForm1:Button_callMethodClick(Tobject *sender)
//----------Second proc
{Int Value=Myinstance.Aproperty; //Use the class}



Of course, the compilation hangs on
void TForm1:Button_callMethodClick(Tobject *sender)
Because Myinstance is not declared in this proc

Here is My question

Where (and how) should I declare Myinstance in order to have her
visible in the
second proc, The constraint is that the parameter is unknew util user
clik button1 in proc 1

For example, Myclass could be a accountmanager and i need to ask the
user for the account number before instanciate it

Thanx for answering
 
R

Rob Williscroft

cosmocat wrote in
Hi all
I'm a newbie in C++Builder and I need and advice about the way of
instatiating a class in order to have a scope gobal to my form

Well Form is unknown in Standard C++, but since I know a Form is
just a non-standard class, I'll take it from there.

On my form, I have 2 Buttons
Button_initialise : Instanciate the class whit a parameter for the
constructor
Button_callMethod : Use a property from the class

[snip]

Somewhere you have something like:

#include "whatever"
//add:
#include <memory>

class TForm1: public TWhatever
{
//add:
std::auto_ptr< Myinstance > myinstance;
};

void TForm1:Button_initialiseClick(Tobject *Sender)
//----------first proc
{Myinstance Myclass("Parameter"); // Instanciate the class}

{
myinstance.reset( new Myinstance( "Paramiter" ) );
}
void TForm1:Button_callMethodClick(Tobject *sender)
//----------Second proc
{Int Value=Myinstance.Aproperty; //Use the class}
{
if ( myinstance.get() )
{
Int Value = myinstance->Aproperty;
}
else
{
// Error handling here ??
}
}

HTH

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top