?
Solved

500pts: Class designing?

Posted on 2006-11-29
9
Medium Priority
?
190 Views
Last Modified: 2010-04-16
Hi there,

I wanted to check on something, i have the idea that classes should never be coupled i.e. depend on another class etc..

i.e. should never create a class with a method where you pass  in another class for example... as if this changes... that it can cause a ripple effect on all classes... this i understand...

Does this also go for passing in things like listboxes etc? I presume it does, it that case if i need to populate a listbox and 1 of my classes knows how to do this... what is the best/correct way of doing this..

returning a hashtable or something and filling it in code ?????

Any comments really appreciated...

Thanks in advance

Ian
0
Comment
Question by:ianinspain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 18035321
Please give more information about this class - what should it do, which information should it keep etc.
0
 

Author Comment

by:ianinspain
ID: 18035612
For example.. I have a listbox in the UI ... and a class is called ... passing in a listbox... the listbox fills with the status of loggin in i.e Initialising, setting up abc, etc etc..  The class basically does a login....

Of course it works but i believe that i have now coupled this with a Listbox... is this ok? ... hence it depends on a listbox being passed in... just a little confused...

Like i say i know that to pass a class to another class makes it coupled hence changes to one can make a ripple effect and cause code to break everywhere...

I really wanted to know if i am doing this the right way, i should i look at an altenative.. i..e. raising an event for each stage of login and get the event to load the listbox via the UI??

0
 

Author Comment

by:ianinspain
ID: 18035628
I have learnt so so much over the past 6 months but i am trying to get rid of my bad habits and start putting some best practices into practice :-)...

I want to ensure i am doing things the correct way

I tried searching google but couldn't find any sort of best practices class design sutff.. i..e why not couple etc etc

Ian
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:AlexFM
ID: 18035660
I understand that you don't like that class works with Listbox. Other client possibly has other control, or wants to print information to Console window.
If class has methods which accepts ListBox, replace ListBox parameter with other types like string, string[] or any other types appropriate to pass your information.
If class has ListBox member which is set by client, and periodically adds lines to this ListBox, remove this class member. Use events instead. For example, when something happens, class raises event passing all required information as event parameters. Client creates instance of your class and subscribes to its events. In event handler client adds information to Listbox or does anything else.
Events help to isolate class from its clients. Event source does its own work and raises events when necessary, without any knowledge about clients.
0
 

Author Comment

by:ianinspain
ID: 18036369
Hi AlexFM,

yes.. so you can confirm that this is the way to go.... you should never accept things like listboxes, etc as input variables on methods of a class because it because coupled... is this correct?

Instead should use simple types i.e. strings, ints, and the good old fashioned hashtables and eventargs etc??

This is what i was thinking but wanted some confirmation on the matter....

Thanks again...
0
 
LVL 7

Expert Comment

by:mjmarlow
ID: 18036686
Hi ian,

In order to be completely decoupled, you would have to define methods that take "Object" as parameter(s).  This technique has a lot of problems.  

1.  Performance.  Functions will need to convert input arg at runtime.
2.  Lack of contract.  Defining class as input parameters automatically defines (documents to a certain degree) a public "contract" on how to invoke method.

Use objects as parameters when ever you can!  There is nothing wrong with using a ListBox or any other class as an input parameter.  

Of course there are situations where late binding makes sense.  But these are exceptions not the rule.  

0
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 18036707
>> You should never accept things like listboxes, etc as input variables on methods of a class because it because coupled... is this correct?

Generally, this is right. But there are some situations when this is necessary. For example, you write function which does something with ListBox control. In this case it should have ListBox parameter. Like function which moves selected item from one listbox to another. It should look like:
void MoveSelectedItem(ListBox from, ListBox to)

In your case, if ListBox is not important, use only parameters which pass information between client and server. Class should not now anything (or have minimal knowledge) about its clients.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 18036769
>> Use objects as parameters when ever you can!

Sorry, I don't agree with this. Never use objects unless there is no other way.
Microsoft made a lot of work to remove using of Object parameters, replacing old containers with generic containers. Using Objects is not type-safe, and slower than using specific types. For reference types, there is slight performance loss for casting. For value types, there is significant performance loss for boxing.
0
 

Author Comment

by:ianinspain
ID: 18075773
Thanks AlexFM, as usually excellent helpful advice ... thanks again...

0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question