OOP Help!

Hi all

I've been programming commercially in VB for about 2 years now, i know C/C++ quite well and im im pretty much there with C# syntax too.

My question is how does OOP fit into my applications?

Everytime i read tutorials on this subject it all makes perfect sense but when you try and put it in to practice it doesn't seem to go as smooth as i'd like.

As an example i have a tblClients table in SQL which basically holds some basic information on our clients..

Would i build a Client Class for this object? Would i make the columns in the the table the field variables of the class?

I can see how all this would work simply by providing methods of the client class but this doesn't seem right???

Thanks in advance
Who is Participating?

Improve company productivity with a Business Account.Sign Up

ambienceConnect With a Mentor Commented:
>> Client myClient = new Client(ClientID) // c#
(by passing in a clientID you have access to each individual client)

That is possible, though not the only approach, although this appears intuitive. This would require your client to always be created based upon an ID, what if you are adding new clients

>> What if i wanted to perform an action on every client at the same time?  Would this be another different

Dont really understand what you mean by at the same time, perhaps you mean same action on all clients. for e.g. add a project to all clients, well then you can go ahead iterate over the client list and call add_project on all of them. Or better you can have a class say ClientManager.
There are certain trade offs with both approaches.

You can think of the manager as a container of clients, so for e.g. this class is responsible of adding new clients, and methods which apply to all client can be put into this class, say add_project_to_all_clients kind of methods. But then there comes the problem of updating each client object with changes in database.

Also this would ease the case of Deletes, for e.g. what happens if you call Client.Delete(), would your client object be usable ?

such conditions can be handled easily by using a Manager/Container.

One more thing if this is a database app then perhaps you wont like to be accessing the database for each operation when you can do the operations in bulk,  for all clients if client.update_status() results in accessing the database to update a particular client status, this seems to be an overkill, for you can do that in a single operation.

This topic can go on into depth as much as you want to, but perhaps you can give your feed back about what you think of it and then it would be much easier to continue.

>> Just relating all the methods and variables into a class doesn't look very object OO.

a restatement would be, encapsulating all the related methods and variables into a class (can be) considered as OO.
>>Would i build a Client Class for this object? Would i
>>make the columns in the the table the field variables
>>of the class?
The general answer is Yes and Yes, but it depends on the requirements and methods you need to use.

>>I can see how all this would work simply by providing
>>methods of the client class but this doesn't seem
What doesn't work.
OOP is more design and organization. It is possible to develop completely without using objects (OOP programming), but when you start to create complex programs, it starts to get very difficult to tell which functions can change which variables, and which functions depend on which other functions.

This is where OOP comes in. It groups all of your variables and functions that affect each other together. Once you have created this object (group of variables and functions), if you change the object - the way it works internally, you should never have to touch any other objects or code.

Since you've programmed in VB for years, have you noticed all the controls you can use? How they all have methods that you can call or properties you can change? Anything you do to a control doesn't affect your calls to other controls. That control is an object, an isolated piece of working code.

You can have 20 controls or more, of the same type, in a single application, and none would even know the others existed, nor would they need to. A control (object) works all by itself. Where it is doesn't really matter. What else is there doesn't really matter. It knows itself, and it interacts with the outside world through its interface - a known set of functions other code can call to interact with this control.

So, OOP programming, in summary, is a bunch of variables and functions that all work together, are isolated from the outside world, can be interacted with through an interface, and changes to the inside of the object won't affect anything else outside of the object in terms of how you call/use the object.

A lot of nitpickers can tear my explanation up, but the general idea is there. It looks like you've read a fair bit into OOP, so hopefully this'll help make it easier to understand what you've been reading. :)
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

This question is current, others below, are not.  ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
POINTS FOR EXPERTS awaiting comments are listed in the link below
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
Thanks everyone.
Moderator @ Experts Exchange
>> but when you try and put it in
to practice it doesn't seem to go as smooth as i'd like.

It takes time to become smoother and smoother, reading good tutorials is fine but reading good wriiten OOP code for some real-life application is the best source of inspiration and example.

Another thing that can help you become smoother is by studying OO design patterns, most of the times you dont have to invent anything decent on yourself and use design patterns that fit well into your situation. This also gives you the sense of having proper OOD etc. if you follow things intelligently.

>> Would i build a Client Class for this object? Would i make the columns in the the table the field variables
of the class?

This depends upon your situation, maybe you can have a generic table class and field classes and you can create an instance of table class for your tblClients table, the columns of that tables can be represented by a collection of fields class.
What i am saying is to model the Recorset fields pattern that ADO has, and isnt it flexible. but then do you even need a table class ?

OR maybe you can have a specialized class for that table with columns for that table as its data members, would be much more efficient. (you can do this if there arent going to be many tables or different varieties of database access).

But then here is the trade off between genericity and perhaps efficiency, i say you try both approaches and then perhaps you'll be able to appreciate a particular style.
Rob_SavilleAuthor Commented:
Totally understand what everyone is saying..

Given that i have a clients table and there are certain actions you generally take on each client. ie, add project, rename client, delete client etc..

If were to instantiate a new client object would i go ahead and do something like:

Client myClient = new Client(ClientID) // c#

(by passing in a clientID you have access to each individual client)

What if i wanted to perform an action on every client at the same time?  Would this be another different class?

Just relating all the methods and variables into a class doesn't look very object OO.

I think i might be trying to over complicate things.

Im trying to write more a business tool rather than a component so perhaps thats why im getting confused.
Rob_SavilleAuthor Commented:

ok i pretty much know where im going from here, i think im definitely trying to complicate this to much.

Im now compitent with c# syntax and classes and OOP i just need to figure out the best design for what im doing


Usually the best design is to break things up into real world objects, so it is intuitive. We (humans) think in object oriented terms. So:

You have a client. This client could be anything - but it is still an object that you can relate to. It may have a connection. The client connection is a separate object, and its job is to connect the client to whatever, maybe a database. You have a database. Another separate object.

Just like an object is a Microwave, it cooks food, it has certain properties, like width of the oven, and height of the oven. It also has procedures, like Cook, and Timer. It has output functions (get) like the LED display, and input functions (set) like the number pad. That's all there really is to it about object oriented programming - it is "object" oriented...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.