Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
namespace ConsoleApplication1
{

   public class A
    {
       public void Methodtoimp()
        {
           // Console.WriteLine("A class call");//what the code
          //  Console.WriteLine("B class call");// have to write here??
        }                                               
    }

  public class B:A
    {
        static void Main(string[] args)
        {

            A obj1=new A();
            obj1.Methodtoimp();
            B obj2=new B ();
            obj2.Methodtoimp();

            Console.Read();
        }
    }
}


what the code write above so that when i call the A object the my output must be"A class call" and for B obj2 call output should be "B class call"

Thank you
Sachin
Posted
Updated 27-Jul-12 9:54am
v2

You need to use this.GetType().Name to obtain the run-time type of the instance used for a call and get its simple (not fully-qualified) name. So, it will be:
C#
Console.WriteLine(string.Format("The run-time type of the calling instance is {0}", this.GetType().Name));
// I don't want to use your incorrect "class call"
// ".this" can be omitted

// or, equivalent simpler form of WriteLine:
Console.WriteLine("The run-time type of the calling instance is {0}", this.GetType().Name);

It's important to understand that you use instance (non-static) methods. These method has additional (hidden, implicit) parameter called "this", used to carry the instance passed as specified by your variable name on the left part of '.' (dot) in obj1.Methodtoimp(); or obj2.Methodtoimp(); that is, obj1, obj2 or the like.

(Simplified form of System.Console.WriteLine without using string.Format is possible because of this overload: http://msdn.microsoft.com/en-us/library/828t9b9h.aspx[^].)

I would also want to warn you that using the type object, type name or something like this to define logic of your code would be the big abuse defeating the purpose of OOP. With OOP, you should use late binding based on virtual and overridden methods and/or interfaces. Well, so far, you are not doing anything bad…

—SA
 
Share this answer
 
v4
Comments
chinta123 27-Jul-12 16:21pm    
Thank you.But the problem is thar when i write Console.WriteLine("The run-time type of the calling instance is {0}", this.GetType().Name); then i am getting output as

The run-time type of the calling instance is A
The run-time type of the calling instance is A
I want only one line.please tell me..
Sergey Alexandrovich Kryukov 27-Jul-12 16:25pm    
Who told you to write two lines? Write just one. I said "or, equivalent...".
Instead of writing the code, you should understand what are you doing first.
--SA
chinta123 27-Jul-12 16:27pm    
no no... my output is showing like this

The run-time type of the calling instance is A
The run-time type of the calling instance is A

why it prints two times??i need only one
chinta123 27-Jul-12 16:30pm    
yes i understood ..thank you very much..Sorry for my mistake..
Sergey Alexandrovich Kryukov 27-Jul-12 16:40pm    
Oh, good. Thanks for reporting in back and finally accepting it formally.
Good luck,
--SA
You need to define Methodtoimp in class B as well as class A, then just include the appropriate WriteLine statement in each class's implementation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900