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 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
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
"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);}}
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
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
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
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
> 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.
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?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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,...
|
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...
|
by: Mark |
last post by:
why doesn't .NET support multiple inheritance?
I think it's so silly!
Cheers,
Mark
|
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...
|
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"...
|
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;
};
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |