Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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::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

Report this thread to moderator Post Follow-up to this message
Old Post
Saeed Amrollahi
04-15-08 01:40 PM


Re: Two questions: Global CLR objects and Method chainig in VC++ 2005
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::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





Report this thread to moderator Post Follow-up to this message
Old Post
Cholo Lennon
04-16-08 12:50 AM


Re: Two questions: Global CLR objects and Method chainig in VC++ 2005
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Saeed Amrollahi
04-16-08 09:39 AM


Re: Two questions: Global CLR objects and Method chainig in VC++ 2005
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

Report this thread to moderator Post Follow-up to this message
Old Post
SvenC
04-16-08 09:39 AM


Re: Two questions: Global CLR objects and Method chainig in VC++ 2005
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
adebaene@club-internet.fr
04-16-08 09:39 AM


Re: Two questions: Global CLR objects and Method chainig in VC++ 2005
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Ben Voigt [C++ MVP]
04-17-08 12:37 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

VC++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:48 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.