Code Comments
Programming Forum and web based access to our favorite programming groups.Dear all
Hi
I am Saeed Amrollahi. I write C++ programs using VC++ 2005 CLR/CLI. I
have two problems:
1. How to declare/define and use global ref class objects? For example
for database connection/communication, I usually define a class called
DBBroker, When I used MFC, DBBrk wraped the Recordset/ODBC facilities
and now it wraps the ADO.Net facilities:
ref class DBBroker { // A wrapper class for all database manipulation
public:
DBBroker();
void Open();
bool IsOpen();
void SetQuery(const std::wstring&);
void SetUpdQuery(const std::wstring&);
void SetInsQuery(const std::wstring&);
void Close();
/*
* Load functions
*/
std::map<std::wstring, std::wstring> LoadSQLFilename();
// ...
/*
* Get functions
*/
int GetNextAvailInvestorId();
// ...
/*
* Update functions
*/
void UpdAccLogin();
/*
* Insert functions
*/
void InsIndividual();
~DBBroker();
private:
System::Data::OleDb::OleDbConnection^ Conn;
System::Data::OleDb::OleDbCommand^ Command;
System::Data::OleDb::OleDbDataAdapter^ Adapter;
};
Now I want to define one and only one global object:
DBBroker^ g_DBBrk = gcnew DBBroker();
At the moment, I have to define a DBBroker object for each form and
database operation and obviously, it is base practice.
2. As you know, If I have the following native C++ class:
class C {
public:
C& f();
C& g();
};
then, I can write the following code:
C c;
c.f().g();
How to use the method chaining inside a ref class:
public ref class SingleInvestorRegForm : public
System::Windows::Forms::Form
{
public:
SingleInvestorRegForm^ FillNationalityComboBox();
SingleInvestorRegForm^ FillSexComboBox();
};
FillNationalityComboBox()->FillSexComboBox(); // or something like
that
Thanks in advance,
Regards,
S. Amrollahi
Post Follow-up to this messageSaeed Amrollahi wrote:
> Dear all
> Hi
>
> I am Saeed Amrollahi. I write C++ programs using VC++ 2005 CLR/CLI. I
> have two problems:
> 1. How to declare/define and use global ref class objects? For example
> for database connection/communication, I usually define a class called
> DBBroker, When I used MFC, DBBrk wraped the Recordset/ODBC facilities
> and now it wraps the ADO.Net facilities:
>
> ref class DBBroker { // A wrapper class for all database manipulation
> public:
> DBBroker();
> void Open();
> bool IsOpen();
> void SetQuery(const std::wstring&);
> void SetUpdQuery(const std::wstring&);
> void SetInsQuery(const std::wstring&);
> void Close();
> /*
> * Load functions
> */
> std::map<std::wstring, std::wstring> LoadSQLFilename();
> // ...
> /*
> * Get functions
> */
> int GetNextAvailInvestorId();
> // ...
> /*
> * Update functions
> */
> void UpdAccLogin();
>
> /*
> * Insert functions
> */
> void InsIndividual();
>
> ~DBBroker();
> private:
> System::Data::OleDb::OleDbConnection^ Conn;
> System::Data::OleDb::OleDbCommand^ Command;
> System::Data::OleDb::OleDbDataAdapter^ Adapter;
> };
>
> Now I want to define one and only one global object:
> DBBroker^ g_DBBrk = gcnew DBBroker();
> At the moment, I have to define a DBBroker object for each form and
> database operation and obviously, it is base practice.
How about using the singleton pattern?
(http://en.wikipedia.org/wiki/Singleton_pattern). With help of templates you
can
transform a class into a singleton without touching their code (see
Alexandrescu's aproach in the loki library http://www.ddj.com/cpp/184401943)
> 2. As you know, If I have the following native C++ class:
> class C {
> public:
> C& f();
> C& g();
> };
>
> then, I can write the following code:
> C c;
> c.f().g();
> How to use the method chaining inside a ref class:
> public ref class SingleInvestorRegForm : public
> System::Windows::Forms::Form
> {
> public:
> SingleInvestorRegForm^ FillNationalityComboBox();
> SingleInvestorRegForm^ FillSexComboBox();
> };
>
> FillNationalityComboBox()->FillSexComboBox(); // or something like
> that
>
Yes, the sentence is correct.
--
Cholo Lennon
Bs.As.
ARG
Post Follow-up to this messageOn Apr 16, 12:54=A0am, "Cholo Lennon" <chololen...@hotmail.com> wrote: > Saeed Amrollahi wrote: > > > > > > > How about using the singleton pattern? > (http://en.wikipedia.org/wiki/Singleton_pattern). With help of templates y=[/color ] ou can > transform a class into a singleton without touching their code (see > Alexandrescu's aproach in the loki libraryhttp://www.ddj.com/cpp/184401943=[/color ] ) > > > > > > > > > Yes, the sentence is correct. > > -- > Cholo Lennon > Bs.As. > ARG- Hide quoted text - > > - Show quoted text -- Hide quoted text - > > - Show quoted text - Hi Cholo Thank you. Your answer for second question was good. For the first question: my problem isn't single instance creation, Indeed I want to declare/ define a global ref object, then using extern, I want to reuse such object in other translation units: // 1.h public ref class DBBroker { // ... }; // main.cpp // global definition DBBroker^ DB =3D new DBBroker(); // 2.cpp extern DBBroker^ DB; // now use global DB Thanks - Saeed
Post Follow-up to this messageHi Saeed,
> Thank you. Your answer for second question was good. For the first
> question:
> my problem isn't single instance creation, Indeed I want to declare/
> define a global ref object,
> then using extern, I want to reuse such object in other translation
> units:
>
> // 1.h
> public ref class DBBroker {
> // ...
> };
>
> // main.cpp
> // global definition
> DBBroker^ DB = new DBBroker();
>
> // 2.cpp
> extern DBBroker^ DB;
Define a static public ref class with a static public method which
returns your singleton.
--
SvenC
Post Follow-up to this messageOn 16 avr, 09:28, Saeed Amrollahi <s_amroll...@yahoo.com> wrote: > my problem isn't single instance creation, Indeed I want to declare/ > define a global ref object, > then using extern, I want to reuse such object in other translation > units: You can't declare global variable in .NET. The singleton is the right solution (you should also use it in native C++, it is anyway a better pattern, that a global "extern" object). You could also make the DBBRoker class static : if you declare only one instance of this class, there is no real reason to have it instanciable... Arnaud
Post Follow-up to this message> my problem isn't single instance creation, Indeed I want to declare/
> define a global ref object,
> then using extern, I want to reuse such object in other translation
> units:
>
> // 1.h
> public ref class DBBroker {
> // ...
> };
>
> // main.cpp
> // global definition
> DBBroker^ DB = new DBBroker();
>
> // 2.cpp
> extern DBBroker^ DB;
> // now use global DB
This is correct, except ref class instances live on the managed heap, so use
gcnew instead of new.
>
> Thanks
> - Saeed
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.