This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi. I have three different classes I am working with. 1. OrdEntMenu 2.LogonDialog 3.SQLConnection OrdEntMenu is a main order entry menu. LogonDialog is a Dialog box that is supposed to come up so the user can sign on to the database, and SQLConnection is the database connection class. I am having a hard time figuring out what I need to do to make these three classes work together. Here is the main method for OrdEntMenu:
Here is code for my SQLConnection class:
My problem lies in the line where I am calling methods on the LogonDialog class. I know it is not initialized here. However, if I initialize it aren't I creating a totally different object than the one I created in my OrdEntMenu class? I need to get to the object of the LogonDialog class that I created in the OrdEntMenu class so I can get the text the user keyed so I can pass that to the database connection object. I hope this is understandable. Am I going about this all wrong, or is it something simple I am missing? Thanks for any help in advance! [ March 04, 2002: Message edited by: Jennifer Sohl ]
Hi, 1. you can make LogonDialog ld as a global varible, static LogonDialog ld = new LogonDialog(); then you can access it by MAINCLASS.ld.METHODNAME (if ld is a static varible in MAINCLASS class) 2. Or you can pass ld to OrdEntMenu, either to its construct or using a setter, LogonDialog ld = new LogonDialog(); OrdEntMenu tr = new OrdEntMenu(ld); or tr.setLogonDialog(ld); kawaii
A very important Design Principle: keep your classes loosely coupled. IOW, each class should know as little as possible about other classes. Google for "Demeter's Law" for guidelines on how to maintain proper knowledge of other classes. DO NOT use a global static variable. Passing LoginDialog as a parameter to a setXXX method is somewhat OK but the ideal way would be to just pass the username and password Strings as parameters to the SQLConnection constructor. SQLConnection does not really need to care about the source of the username and password and by making it know about LoginDialog, you couple the two classes unnecessarily and inappropriately.
Joined: Feb 28, 2001
Thanks for the replies. In reference to passing the username and password "Strings" to the SQL Connection constructor... I'm a bit confused. Don't I still have to pass LoginDialog to the constructor? If I pass username and password into the constructor as String objects, how does it know I mean the username and password Strings from the LogonDialog object? I seem to be a bit confused. Could someone please straighten me out? Thanks a million! I really appreciate it.
Don't I still have to pass LoginDialog to the constructor? If I pass username and password into the constructor as String objects, how does it know I mean the username and password Strings from the LogonDialog object? The point is, does SQLConnection really have to care where the userName and password came from? All it cares is that it has these values to make the connection, right? So, instead of extracting the strings from the dialog inside the SQLConnection constructor, extract them inside the LogonDialog event handler. The LogonDialog event handler more appropriately has intimate knowledge of LogonDialog whereas SQLConnection does not.
OH!!! Now I see what you're saying. I had myself completely turned around. Looks like another case of making something harder than it really is. I'm good at that. Thanks SO much for your help!! You just made my day a little brighter!