|
Hi all,
I'm writing program about database,
In my program i have three dialogs, and i have to use of CDaoDatabase object in all dialogs ...
I want to use of only one object for do that,
How can i do that ???
Does i must create global variable ?
I mean, writing CDaoDatabase g_db; in first of my source code, and using of extern CDaoDatabase g_db; in other cpp files ???
Do you have any idea ?
My program is in SDI mode ...
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
Make your CDaoDatabase an application data member and provide an accessor function in your app class...
class CYourApp : public CWinApp
{
public:
CDaoDatabase& GetDatabase() { return m_db; }
private:
CDaoDatabase m_db;
};
... and use AfxGetApp in your dialogs to get access to db:
CDaoDatabase &db = static_cast<CYourApp *>(AfxGetApp())->GetDatabase();
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Hi Tomasz, and thanks for reply ...
Can you explain more about your source codes ...
For example, first, you make private member variable CDaoDatabase m_db; and then you make public member function CDaoDatabase &GetDatabase(){return m_db;} !!!
WHY ???
Why you make GetDatabase() function ? why i shouldn't use of only m_db ?
And please explain to me how can i use of GetDatabase() function ...
Because i'm beginner ...
Thanks again ...
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
For example, first, you make private member variable CDaoDatabase m_db; and then you make public member function CDaoDatabase &GetDatabase(){return m_db;} !!! WHY ???
This is called encapsulation. The main benefit is that you can rename m_db and you'll have to change the name only in two places
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Hi again,
AfxGetApp() is object of CWinApp class, so can't use of m_db or GetDatabase() in AfxGetApp() !!!
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
|
Actually, Tomasz, I would not mind having an explanation of the advantage of static_cast over the old fashioined ((CMyApp*)AfxGetApp())->WhatEver() in this case.
|
|
|
|
|
The only syntactic difference is that static_cast won't compile if CYourApp isn't derived from CWinApp, which is highly unlikely. It's just my personal preference to cast the C++ way.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Hi Tomasz,
Thank you for your explain ...
Now, can you write example about using of m_db from AfxGetApp() ?
Because in your example you use of GetDatabase() in AfxGetApp(), now can you write example about using m_db in AfxGetApp() ?
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
AfxGetApp() (if I rememeber right)is a gobal function in MFC which returns a pointer to your CWinApp derived class so you can use "->GetDatabase()" Any public function in this CWinApp derived class called from dialog(other class also outside this discussion) can be called this way.
|
|
|
|
|
In all honesty, I don't feel that exposing your db as a pointer or a reference is the best design. I think it would be far better to simply put the db in the mainframe class (or possible a doc class) and have a means of communicating data back and forth between the db and the UI by some sort of intermediate set of classes.
i.e.:
class CSomeDBData // or a struct if your prefer.
{
//whatever
};
CMainFrmame::OnSomeCommand()
{
CMyDialog MyDlg;
CSomeDBData somedata;
m_db.GetSomeData(&somedata);
MyDlg.EditSomeData(somedata);// or however you like.
if( MyDlg.DoModal() == IDOK )
{
MyDlg.GetSomeEditedData(somedata);// or however you like.
m_db.SetSomeData(&somedata);
}
}
This way, you don't end up with pointers and references to your core objects scattered all over your application. Which, as the application grows, will certainly happen unless you take steps to limit it. If your app is large you should concentrate on keeping data centralized and not globally available to anything that might need to access it or which might store pointers to it. This takes a little extra work, but pays off in the long run.
|
|
|
|
|
Hi,
Thank's it was nice help ...
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
Hi again Stan,
Your idea was very good, but i have little problem ...
I can check the return value of DoModal() for checking the hited button ...
But when i click on button in the dialog, dialog will close !!!
Now, i want to use of button in the dialog in CManFrame class without dialog will close ...
How can i do that ?
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
First, I have never used the DAO classes (I typically use CDatabase/CRecordset for simple stuff and ADO for anything else). So I cannot address issues relatiing to DAO specifically. There may be easier techniques for you to use than what I am suggesting for DAO built into MFC. If you look at MSDN or Codeproject samples you might find something relating to that.
Yes, DoModal returns when the dialog box is closed. That is by design, and is typically the behavior you want. Obviously, I don't know the details of your design, and maybe what I'm suggesting will not work for you. All I'm saying is that it is the approach I *always* take, as I have found it to be a very safe and simple way to do business, especially in a large application that many people have their hands on.
What I am suggesting is that you copy the data that you want to manipulate from the db object, which is "owned" by the MainFrame object, to the dialog box, initializing the dialog box with that data (there are various opinions about how to do that best). Next you allow the user to manipulate that data in the dialog box. Finally, if the user hits OK you copy the edited data from the dialog box and back to the db object, if the user hits cancel, do nothing at all.
In this way, the dialog box never needs to know that the db object even exists, and the db needs to know nothing about the dialog. Only the Mainframe window (and the db object itself, of course) knows, or cares, about the db object, or the dialog. In this way, the db object, and whatever global data it contains, is not made public to the entire application unnecessarily.
If the db data also needs to be accessed from objects other than dialogs, (views, for example), than that might be an argument for giving ownership of the db object to your document class rather than the Mainframe. If more than one document/view needs access to the db data, well, than you might need to consider letting the App class handle it afterall. As the programmer, its your call.
|
|
|
|
|
I want to use CGfxOutBarCtrl class, but I need to display an icons with variable width (height is permanent) in it.
All icons are adding via CImageList but it stretch all of them to the maximal size.
Do you have any ideas how to display such type of icons there?
With the best regards, Vitaly.
|
|
|
|
|
You would have to use two, or more, imagelists. As you found out everything in the imagelist must be the same size, that is how individual images are found, by calculating the offset based on the size.
|
|
|
|
|
i would use the ADodb method to create a link between an vc++ application and Access 97. what is the correct microsoft jet OLEDB i need to use, 4.0 or 3.51?
gerald
|
|
|
|
|
Access97 uses 3.51, but 4.0 should be backward compatible.
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
|
|
|
|
I have editBox (read only) with spin.
the variable of the editBox is int in range 10 - 2000
and the range of the spin is same.
and all is right.
But when the user try to up the number in the editBox over 999
he gets a error messageBox: "Please enter an integer."
WHY ?????????????????????????????????????????????????
Please HELP
|
|
|
|
|
Uncheck the No Thousands property of the spin control - this prevents the control from adding a thousands separator (period or comma) to the number.
--Mike--
http://home.inreach.com/mdunn/
Push the button, Frank.
|
|
|
|
|
Thanks on the quickly answer !!!
|
|
|
|
|
I have gray the desktop, but there still some area cannot be grayed, how can I gain this effect.
I use CreateDIBSection. and BitBlt to copy the memory dc to the desktop.
|
|
|
|
|
I have try to gray the desktop when my program runs. by there are still some problems:
1, If I use CreateDIBSection, What bitsperpixel should I use, 8, or >8?
2, If I can use 32bits, even on 16 desktop settings?
|
|
|
|
|
hi.. I wanna open a folder from my software...Can i do that??????
|
|
|
|
|
I don't know exactly what you mean, but try looking up
SHBrowseForFolder in the MSDN Lib
maXallion "Look for bugs, I hate bugs!" - Warden, The Mummy www.maxallion.de - coded evil & more
|
|
|
|