Two questions: Global CLR objects and Method chainig in VC++ 2005

S

Saeed Amrollahi

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
 
C

Cholo Lennon

Saeed said:
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.
 
S

Saeed Amrollahi

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 libraryhttp://www.ddj.com/cpp/184401943)








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 = new DBBroker();

// 2.cpp
extern DBBroker^ DB;
// now use global DB

Thanks
- Saeed
 
S

SvenC

Hi 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.
 
A

adebaene

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
 
B

Ben Voigt [C++ MVP]

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.
 

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

Top