Click here to Skip to main content
15,888,302 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi guys,

I am a relative newbie to OO programming so please be patient :D
For the sake of exercise I've been writing a small desktop app (room booking system, eg. in hotel). In order to visualise all of the rooms available I've created a custom panel control (color coded - red for busy room, green for available and so on). Within my PaintedPanel class I have a method that creates a grid (like a chess board)
of the panels... Using a simple 'for' loop it creates specified number of panels and adds them to parent control. Question is -> is it ok to create an object (or collection of objects) of the particular class within the class itself ? (eg. my PaintedPanel class has a method 'createPanelGrid' in which I create an instances of the PaintedPanel objects).
I decided to do it this way in order to have only one 'onclick' event handler that serves all of the PaintedPanel instances, rather than creating a grid of Panels within my main form logic and having to code multiple event handlers for each and every PaintedPanels.
Does it make sense ? Not sure if I got the idea of custom controls rigth...
Any suggestions will be greatly appreciated.

Cheers,

Mike
Posted

1 solution

You can do that, but your reasoning is slightly flawed.

"I decided to do it this way in order to have only one 'onclick' event handler that serves all of the PaintedPanel instances, rather than creating a grid of Panels within my main form logic and having to code multiple event handlers for each and every PaintedPanels."

The same event handler can handle as many instances as you need: the sender parameter tells you which one it relates to:
void myEventHandler(object sender, EventArgs e)
   {
   MyCustomControl mcc = sender as MyCustomeControl;
   if (mcc != null)
      {
      Console.WriteLine(mcc.APropertyOfTheControl);
      }
   }


Normally, you would keep it so that the Room class does not know or need to know about even the existence of other rooms: instead, You would have a Floor class which knew about all the rooms on a floor, and a Hotel class which knew about all the floors.
 
Share this answer
 
Comments
MTomorowicz 9-Jun-11 7:30am    
Thanks a lot mate !
Specially for the 'sender' part, got it completely wrong from the start. Will experiment with it now but I think your info solved my issue in this case :D
So its back to the lab for me ;)
OriginalGriff 9-Jun-11 8:02am    
You're welcome!
MTomorowicz 9-Jun-11 7:47am    
Quickly re-coded my class according to your suggestion... works like a charm :D
Sergey Alexandrovich Kryukov 9-Jun-11 12:03pm    
Please always tag the question properly. In particular, add the tag "WPF" or "Forms" or "ASP.NET" or -- what else?
--SA
Sergey Alexandrovich Kryukov 9-Jun-11 12:03pm    
If "like a charm", my 5, too :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900