[.net] Inheritance question [solved]

Started by
1 comment, last by Calin 17 years, 12 months ago
I have a base class 'baseCtrl' and two classes that inherit from it: 'Button' and 'Form'. In guiManager class I have a List of 'baseCtrl' named ManagerBuffer in which all Buttons and Forms get pumped in. Each Form has a container for Buttons called Bns. Now what I'm trying to do and can't succed is loop through ManagerBuffer and throw Buttons in Forms.The problem I'm getting is that Bns is Form specific and can't be called while iterating through ManagerBuffer Here's a snippet:

 List<baseCtrl> ManagerBuffer;

//...

do
            {
                CurrentNofC = ManagerBuffer.Count;
                bool BreackLoops = false;

                for (int a = 0; ((a < ManagerBuffer.Count) && (!BreackLoops)); ++a)
                {
                    for (int b = 0; ((b < ManagerBuffer.Count) && (!BreackLoops)); ++b)
                    {
                        if ((ManagerBuffer[a].Name.CompareTo(ManagerBuffer.Parent) == 0) // If a cotrol (a)is the  parent of another control (b) 
                            && (ManagerBuffer[a].Name.CompareTo(ManagerBuffer.Name) != 0))  // And the cotrols are not the same control
                        {
                            ManagerBuffer[a].Bns.Add(ManagerBuffer); ////Here's my problem

                            ManagerBuffer.Remove(ManagerBuffer);
                            BreackLoops = true;
                        }


                    }
                }
               

            } while (CurrentNofC > ManagerBuffer.Count);





Any advice how to do it? [Edited by - Calin on April 21, 2006 12:55:42 PM]

My project`s facebook page is “DreamLand Page”

Advertisement
 List<baseCtrl> ManagerBuffer;//...do            {                CurrentNofC = ManagerBuffer.Count;                bool BreackLoops = false;                for (int a = 0; ((a < ManagerBuffer.Count) && (!BreackLoops)); ++a)                {                    for (int b = 0; ((b < ManagerBuffer.Count) && (!BreackLoops)); ++b)                    {                        if ((ManagerBuffer[a].Name.CompareTo(ManagerBuffer.Parent) == 0) // If a cotrol (a)is the  parent of another control (b)                             && (ManagerBuffer[a].Name.CompareTo(ManagerBuffer.Name) != 0)  // And the cotrols are not the same control                         // *** added a line: is the target able to accept new buttons?                            && (ManagerBuffer[a] is Form))                        {                            ((Form)ManagerBuffer[a]).Bns.Add(ManagerBuffer); // Problem solved :)                            ManagerBuffer.Remove(ManagerBuffer);                            BreackLoops = true;                        }                    }                }            } while (CurrentNofC > ManagerBuffer.Count);


You might want to consider having a Container class (as a subclass of Control):
public class Container: baseCtrl { public List<baseCtrl> Controls;}

A Form is then a subclass of Container. This is a very common model in designing GUI systems. My fixes to your code would be pretty similar, just change 'Form' to 'Container' in both places, and change 'Bns' to 'Controls' (a more sane name if containers can contain things other than buttons!).
Nice. Actually at fist it didn't work, I had to modify the code to his:
                         //...                         // *** added a line: is the target able to accept new buttons?                            && (ManagerBuffer[a] is Form)                            && (ManagerBuffer is Button))                        {                            ((Form)ManagerBuffer[a]).Bns.Add((Button)ManagerBuffer); // Problem solved :)                            ManagerBuffer.Remove(ManagerBuffer);                            BreackLoops = true;                        }


Couldn't have done it without you though. Thanks!
I'd rate you up but you're already rated to top in my profile :)

My project`s facebook page is “DreamLand Page”

This topic is closed to new replies.

Advertisement