Jump to content
  • Advertisement
Sign in to follow this  
Calin

[.net] Inheritance question [solved]

This topic is 4503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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!).

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!