473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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::wstringLoadSQLFilename();
// ...
/*
* 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
Jun 27 '08 #1
5 1733
Saeed 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::wstringLoadSQLFilename();
// ...
/*
* 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


Jun 27 '08 #2
On Apr 16, 12:54*am, "Cholo Lennon" <chololen...@hotmail.comwrote:
Saeed 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::wstringLoadSQLFilename();
// ...
* * * * * * * * /*
* 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 libraryhttp://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- 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
Jun 27 '08 #3
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.

--
SvenC
Jun 27 '08 #4
On 16 avr, 09:28, Saeed Amrollahi <s_amroll...@yahoo.comwrote:
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
Jun 27 '08 #5
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

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
6
by: MechSoft | last post by:
Hi, I am new to C++ from C, I am a bit confused about using global objects in C++. I have a program that need to share some data(held in classes) between files, and of course I thought about using...
5
by: Tony Johansson | last post by:
Hello! Is it possible to mix C++ code for example DLL developed with Visual Studio 6.0 with C++ code developed with Windows Forms Application(.NET)? Is it possible to mix C++ code for example...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
1
by: Johhy | last post by:
Hi, I recently took the web services certification test, and afterward I looked at some braindump-question to see what the correct answer was. However, I still don't know the correct answer and...
23
by: David Colliver | last post by:
Hi, using c#, 1.1 I know that we are not supposed to use global variables etc. in c# I am having a problem, but not sure how to resolve. I did have another post here, but may have over...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
4
by: jubelbrus | last post by:
I need to initiate a global variable to a class before the initialization of any other global variable. The problem is that when I link the application with a dll, some or all global variables in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.