Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Inheritance problem (http://www.programmingforums.org/showthread.php?t=6203)

OpenLoop Oct 1st, 2005 3:18 PM

Inheritance problem
 
I have two classes A and B. B inherits A, somthing like this
:

public Class A: Windows.Form
{
      private Button btn1;
      public TextBox txt1;
}
public Class B:A
{
      public TextBox txt2;
}


when i display form B, I get the button from A in addition to both text boxes. My questions is, how do i get B to inherit all of A's NON PRIVATE members only? (I don't want the button to show up in B)

Thank you

uman Oct 1st, 2005 6:25 PM

This isn't possible in C#. It is in C++, but that's another story.

OpenLoop Oct 1st, 2005 7:05 PM

I got a workarround. I created a BaseForm with no buttons and only the things i want common in all my forms. Then each new form inherts the base form.

I'm surprised C# doesn't have that capability, I guess you can't have anything.

Thanks for the info

xavier Oct 1st, 2005 11:33 PM

If the button is private , it means that it's only seen in its class, it will not be accesible in other classes ... So, how the hell did it show you the private button from A ?

Or am I misunderstanding the problem ?

If i have this :
:

        public class A
        {
                private static int i = 0;
                public static int t = 1;
        }

        public class B : A
        {
                public static int p = 2 ;
                public static void Main()
                {
                        Console.WriteLine("i = {0} , t={1}, p={2}",i,t,p);
                        Console.ReadLine();
                }
        }


It sais it can't access i because of its protection level.

OpenLoop Oct 2nd, 2005 1:00 PM

xavier, that's what i thought should happen, but it didn't. I guess it has to do with the nature of the program, being graphic and all. But like i said, i got arround it.

Dameon Oct 2nd, 2005 1:14 PM

A private member is still inherited. It just can't be accessed by subclasses.

:

class Person
{
private int age = 0;

public int Age
{
  get
  {
      return this.age;
  }
  set
  {
  if (value < 0)
      throw new Exception("A person can not have a negative age");
  this.age = value;
  }
}

}

class Employee : Person
{

  public void accessTest()
  {
      this.age = -5; //Can't do this, because age is private
      this.Age = -10 //Can do this, but it will throw an exception. See how private
                          //members can keep subclasses out of your business?
  }
}


A protected member can be accessed by subclasses (and I think nested classes). If age was protected, then both lines in accessTest would be valid.

When you inherited your form, notice that in both forms the constructor called InitializeComponent(). InitializeComponent is a private method for both forms. Because it is private, they can both exist without having a name conflict. Note that it is not being overriden. When you create an instance of your inherited form, the base constructor is called, and in turn the original InitializeComponent. This sets up the text box and the button. Then, the subclass constructor calls its InitializeComponent. This sets up the second text box.

xavier Oct 2nd, 2005 11:50 PM

The thing was, that i wrote a similar thingy to what OpenLoop described, but i forgot to initialize the button in the base class, so it didn't apear :o .

Thanks for the explanation Dameon.


All times are GMT -5. The time now is 4:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC