By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,780 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,780 IT Pros & Developers. It's quick & easy.

inheritance and static

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/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?
--
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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.