Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Saeed Amrollahi - 15 Apr 2008 07:17 GMT
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
Cholo Lennon - 15 Apr 2008 22:54 GMT
> Dear all
> Hi
[quoted text clipped - 46 lines]
> 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 {
[quoted text clipped - 14 lines]
>    SingleInvestorRegForm^ FillSexComboBox();
> };

> FillNationalityComboBox()->FillSexComboBox(); // or something like
> that

Yes, the sentence is correct.

--
Cholo Lennon
Bs.As.
ARG
Saeed Amrollahi - 16 Apr 2008 08:28 GMT
> > Dear all
> > Hi
[quoted text clipped - 84 lines]
>
> - 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
SvenC - 16 Apr 2008 09:15 GMT
Hi Saeed,

> Thank you. Your answer for second question was good. For the first
> question:
[quoted text clipped - 14 lines]
> // 2.cpp
> extern DBBroker^ DB;

Define a static public ref class with a static public method which
returns your singleton.

--
SvenC
adebaene@club-internet.fr - 16 Apr 2008 09:27 GMT
> 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
Ben Voigt [C++ MVP] - 16 Apr 2008 16:53 GMT
> my problem isn't single instance creation, Indeed I want to declare/
> define a global ref object,
[quoted text clipped - 13 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.