473,396 Members | 2,003 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,396 software developers and data experts.

inheritance and static

Consider following example:

class A
{
public static Method()
{
/* ... */
}
}

class B : A
{
}

How can i chcek in Method() if it was called as B.Method() or A.Method()?

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl
Mar 30 '06 #1
10 3506
Maybe like this:

class testA
{
public void Method()
{System.Windows.Forms.MessageBox.Show(this.ToStrin g());}
}
class testB : testA
{
}

testA local_A = new testA();
local_A.Method();

testB local_B = new testB();
local_B.Method();

Frank Uray
"Adam Klobukowski" wrote:
Consider following example:

class A
{
public static Method()
{
/* ... */
}
}

class B : A
{
}

How can i chcek in Method() if it was called as B.Method() or A.Method()?

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl

Mar 30 '06 #2
Hi Adam,

Looking at the generated IL (dissasembled using Reflector
http://www.aisto.com/roeder/dotnet/) it looks like the actual call is
to A.Method regardless of how you write the c#.

adam

Adam Klobukowski wrote:
Consider following example:

class A
{
public static Method()
{
/* ... */
}
}

class B : A
{
}

How can i chcek in Method() if it was called as B.Method() or A.Method()?

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl


Mar 30 '06 #3

"Adam Klobukowski" <at***@gabo.pl> wrote in message
news:e0**********@nemesis.news.tpi.pl...
Consider following example:

class A
{
public static Method()
{
/* ... */
}
}

class B : A
{
}

How can i chcek in Method() if it was called as B.Method() or A.Method()?

class A {

public static void M() { M(false); }

protected static void M(bool derived){/* the biz */}}

class B : A{new static void M(){A.M(true);}}
Mar 30 '06 #4
Hi,

Remember that the entire point of the question was the fact that Method was
static
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Frank Uray" <Fr*******@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
Maybe like this:

class testA
{
public void Method()
{System.Windows.Forms.MessageBox.Show(this.ToStrin g());}
}
class testB : testA
{
}

testA local_A = new testA();
local_A.Method();

testB local_B = new testB();
local_B.Method();

Frank Uray
"Adam Klobukowski" wrote:
Consider following example:

class A
{
public static Method()
{
/* ... */
}
}

class B : A
{
}

How can i chcek in Method() if it was called as B.Method() or A.Method()?

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl

Mar 30 '06 #5
Hi,

Frankly I do not think you can tell, either B.Method or A.Method should
gets resolve at compile time to call the correct method.

Why you need to know that?
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Adam Klobukowski" <at***@gabo.pl> wrote in message
news:e0**********@nemesis.news.tpi.pl...
Consider following example:

class A
{
public static Method()
{
/* ... */
}
}

class B : A
{
}

How can i chcek in Method() if it was called as B.Method() or A.Method()?

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl

Mar 30 '06 #6
Ignacio Machin ( .NET/ C# MVP ) napisał(a):
Hi,

Frankly I do not think you can tell, either B.Method or A.Method should
gets resolve at compile time to call the correct method.

Why you need to know that?


Well, the whole idea seems to be flawed end will be redone in some other
way. The point was to be able to have a static function in class that
could beahave in different manner, dependint on classes that inherit
from it.

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl
Mar 30 '06 #7
Hi,

Well, the whole idea seems to be flawed end will be redone in some other
way. The point was to be able to have a static function in class that
could beahave in different manner, dependint on classes that inherit from
it.


What you want is a virtual method.

Also take a look at the singleton pattern, maybe you want something like
that.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 30 '06 #8
> Well, the whole idea seems to be flawed end will be redone in some other
way. The point was to be able to have a static function in class that
could beahave in different manner, dependint on classes that inherit
from it.


Yeah, the base class knows nothing about derived classes. Derived classes
will need to implement their specific functionality. It's very difficult to
break polymorphism, but it's always fun to try.
Mar 30 '06 #9
Not really an answer, but I had a similar question on an interview ...
and the solution I came up with was to throw an exception in the static
method and walk the stack trace to find the class making the call.
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

Frankly I do not think you can tell, either B.Method or A.Method should
gets resolve at compile time to call the correct method.

Why you need to know that?

Mar 30 '06 #10
John Murray <jm*****@pluck.com> wrote:
Not really an answer, but I had a similar question on an interview ...
and the solution I came up with was to throw an exception in the static
method and walk the stack trace to find the class making the call.


That's not reliable - it could be misled by inlining, for instance.

The real solution is not to use a design which goes against the
language and platform idioms.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 31 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: porneL | last post by:
How do I use static functions/properties with inheritance? I've found it very problematic. class Foo { static function A() {/* ??? */::B(); } static function B() {echo 'Foo';} }; class...
0
by: John Hunter | last post by:
I am using pycxx 5.2.2 to implement some extension code and have a problem relating to inheritance. I have a pure virtual base class and two concrete derived classes. In the code below, everthing...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
7
by: Asapi | last post by:
When a derived class inherits from a base class, does the former inherits everything, including public/protected/private instance data, static data, and various methods(static, private/public,...
12
by: Taylor | last post by:
I'm trying to understand inheritance. I'd like to make my own type of IPAddress lets call it myIp. The following gives me CS0029 error: Cannot implicitly convert type 'System.Net.IPAddress' to...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
2
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.