|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Type of base pointerusing System; using System.Collections.Generic; using System.Text; namespace BasePtr { class Program { class A { public virtual void Test() { Console.WriteLine("Method of base class"); } } class B:A { public override void Test() { Console.WriteLine("This is a real virtual method!"); } public void OldTest() { base.Test(); } public void ICanntUnderstand() { Console.WriteLine(base.GetType().ToString()); // WHY IS THE OUTPUT "B?! base class for B is A not A" } } static void Main(string[] args) { A a = new A(); // new A instance a.Test(); // "Method of base class" B b = new B(); // new B instance b.Test(); // "This is a real virtual method!" a = b; a.Test(); // "This is a real virtual method!" b.OldTest(); // "Method of base class" b.ICanntUnderstand(); // WHY IS THE OUTPUT "B?! base class for B is A not A" Console.Read(); } } } This code returns: Method of base class This is a real virtual method! This is a real virtual method! Method of base class BasePtr.Program+B I have a virtual function called Test() in class A. Also i have class B - it's derived from A and has overrided method Test() as well. I can easly access method A.Test() from class B - as written in function B.OldTest(). As well base object doesn't have method OldTest(). You can see it in this error description: Error 1 'BasePtr.Program.A' does not contain a definition for 'OldTest' C:\Documents and Settings\Vitaliy\My Documents\Visual Studio 2005\Projects\BasePtr\BasePtr\Program.cs 27 22 BasePtr base class for B is A and error above shows that base is BasePtr.Program.A. But code "base.GetType()" in any method of class B returns value BasePtr.Program.B. WHY?! Hello, dotNiemand!
you should use base.GetType().BaseType or this.GetType().BaseType to get the parent type... You observe this behavior because of object.GetType() method is not virtual. That means object.GetType will return the type of object instance. base.GetType() you're referring to is "this" instance and has nothing to do with a base class. d> Take a look on this code: d> using System; d> using System.Collections.Generic; d> using System.Text; d> namespace BasePtr d> { d> class Program d> { d> class A d> { d> public virtual void Test() d> { d> Console.WriteLine("Method of base class"); d> } d> } d> class B:A d> { d> public override void Test() d> { d> Console.WriteLine("This is a real virtual method!"); d> } d> public void OldTest() d> { d> base.Test(); d> } d> public void ICanntUnderstand() d> { d> Console.WriteLine(base.GetType().ToString()); // d> WHY d> IS THE OUTPUT "B?! base class for B is A not A" d> } d> } d> static void Main(string[] args) d> { d> A a = new A(); // new A instance d> a.Test(); // "Method of base class" d> B b = new B(); // new B instance d> b.Test(); // "This is a real virtual method!" d> a = b; d> a.Test(); // "This is a real virtual method!" d> b.OldTest(); // "Method of base class" d> b.ICanntUnderstand(); // WHY IS THE OUTPUT "B?! base d> class d> for B is A not A" d> Console.Read(); d> } d> } d> } d> This code returns: d> Method of base class d> This is a real virtual method! d> This is a real virtual method! d> Method of base class d> BasePtr.Program+B d> I have a virtual function called Test() in class A. Also i have class d> B d> - it's derived from A and has overrided method Test() as well. d> I can easly access method A.Test() from class B - as written in d> function B.OldTest(). d> As well base object doesn't have method OldTest(). You can see it in d> this error description: d> Error 1 'BasePtr.Program.A' does not contain a definition for d> 'OldTest' C:\Documents and Settings\Vitaliy\My Documents\Visual d> Studio d> 2005\Projects\BasePtr\BasePtr\Program.cs 27 22 BasePtr d> base class for B is A and error above shows that base is d> BasePtr.Program.A. d> But code "base.GetType()" in any method of class B returns value d> BasePtr.Program.B. d> WHY?! "dotNiemand" <dotNiem***@gmail.com> wrote in message Because the type of the object is "B", not "A".news:1161614415.062246.265560@m7g2000cwm.googlegroups.com... > [...] > base class for B is A and error above shows that base is > BasePtr.Program.A. > But code "base.GetType()" in any method of class B returns value > BasePtr.Program.B. > > WHY?! Your code says to call the "GetType()" method on the "base" class ("A", in this case). You haven't overridden the "GetType()" method in your "A" class (nor should you have), so you get the default behavior for "GetType()", which is to return the true type of the instance. In the words of the *documentation*: "The Type instance that represents the exact runtime type of the current instance" Since the instance was created as type "B", that's what "GetType()" returns. Note that if you inherited your "B" class in a third class "C", and then called your "ICanntUnderstand()" method on an instance of "C", it would return "C", not "B". The result of that method depends not on the class in which it's defined, but rather the type of the object on which it's called. Depending on what you're trying to do exactly, you may find that the C# "typeof" operator is actually what you want. Pete |
|||||||||||||||||||||||