Newbie: pb of scope when instanciating class



 DEVELOP > c-Plus-Plus > Newbie: pb of scope when instanciating class

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "cosmocat"
Date: 16 Oct 2003 03:07:22 AM
Object: Newbie: pb of scope when instanciating class
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
.

User: "Rob Williscroft"

Title: Re: Newbie: pb of scope when instanciating class 16 Oct 2003 06:20:38 AM
cosmocat wrote in news:28ccee3d.0310160007.26524af9@posting.google.com:

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.
--
http://www.victim-prime.dsl.pipex.com/
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER