Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 1st, 2005, 3:18 PM   #1
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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
OpenLoop is offline   Reply With Quote
Old Oct 1st, 2005, 6:25 PM   #2
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
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
uman is offline   Reply With Quote
Old Oct 1st, 2005, 7:05 PM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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
OpenLoop is offline   Reply With Quote
Old Oct 1st, 2005, 11:33 PM   #4
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 367
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
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 !
xavier is offline   Reply With Quote
Old Oct 2nd, 2005, 1:00 PM   #5
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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.
OpenLoop is offline   Reply With Quote
Old Oct 2nd, 2005, 1:14 PM   #6
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 730
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Oct 2nd, 2005, 11:50 PM   #7
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 367
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
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 !
xavier is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:25 AM.

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