![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
This isn't possible in C#. It is in C++, but that's another story.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#4 |
|
Professional Programmer
|
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.
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#6 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 730
Rep Power: 4
![]() |
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.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#7 |
|
Professional Programmer
|
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.
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|