473,378 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to invoke base class static method hidden by inheritance, using derived class?

Hi buddies!

Can you tell me how to invoke base class static method hidden by inheritance, using derived class as in the following case?

using System;

class ParentClass
{
public static int i;

public static void showMethod()
{
Console.WriteLine("The value of i in ParentClass is = " + i);
}
}

class ChildClass : ParentClass
{
new static int i;

public ChildClass(int a, int b)
{
ParentClass.i = a;
i = b;
}

new public static void showMethod()
{
ParentClass.showMethod();
Console.WriteLine("The value of i in ChildClass is = " + i);
}
}

class MainClass
{
static void Main()
{
ParentClass.showMethod();

ChildClass cc = new ChildClass(1, 2);
ChildClass.showMethod();
}
}
Jul 25 '07 #1
3 1817
RoninZA
78
Remember, that because you are inheriting ChildClass from ParentClass, that ChildClass IS also a ParentClass, with extended functionality.

Watch -

Parent class:
Expand|Select|Wrap|Line Numbers
  1. public class ParentClass
  2. {
  3.     public int Counter = 0;
  4.  
  5.     public int GetCounter()
  6.     {
  7.         return Counter;
  8.     }
  9. }
  10.  
Child class:
Expand|Select|Wrap|Line Numbers
  1. public class ChildClass : ParentClass
  2. {
  3.     //public int Counter = 0;  Doesn't compile, already defined in parent
  4.     public int ChildCounter = 1;
  5.  
  6.     //public int GetCounter() Doesn't compile, already defined in parent
  7.  
  8.     public int GetChildCounter
  9.     {
  10.         return ChildCounter;
  11.     }
  12.  
  13.     public int GetParentCounter
  14.     {
  15.         return this.Counter;
  16.     }
  17. }
  18.  
And then the calling code:
Expand|Select|Wrap|Line Numbers
  1. public void main()
  2. {
  3.     ChildClass childClass = new ChildClass();
  4.     MessageBox.Show(childClass.GetCounter().ToString()); //shows 0 - ParentClass method
  5.     MessageBox.Show(childClass.GetParentCounter().ToString()); //shows 0 - ChildClass method
  6.     MessageBox.Show(childClass.GetChildCounter().ToString()); //shows 1 - ChildClass method
  7. }
  8.  
capiche? ;)
Jul 25 '07 #2
RoninZA
78
And another thing - remember that you can also cast Childclass to a ParentClass, but NOT vice versa... in other words, you can load ParentClass and ChildClass objects into a List<ParentClass>, but you can only load ChildClass objects into a List<ChildClass>.

Happy inheriting... leveraged properly, it can be extremely powerful, and save you a hell of a lot of code duplication!
Jul 25 '07 #3
Got it. Thank you very much!
Jul 27 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
5
by: Nathan Bullock | last post by:
Hi, I have a base class, say Base and there are two classes, say Class1 and Class2 which are derived from Base. Is there any way for me, say from a static method in Base, to get a list of all...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
5
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1"...
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
10
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.