Thread: Calling the overriden method from the base class

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743

    Calling the overriden method from the base class

    Code:
    public class BaseClass
    {
    	public virtual void MyMethod()
    	{
    		...do something...
    	}
    }
    
    public class A : BaseClass
    {
    	public override void MyMethod()
    	{
    		...do something different...
    	}
    }
    
    public class B : BaseClass
    {
    	public override void MyMethod()
    	{
    		...do something different...
    	}
    }
    
    public class AnotherObject
    {
    	public AnotherObject(BaseClass someObject)
    	{
    		someObject.MyMethod(); //This calls the BaseClass method, unfortunately.
    	}
    }
    I would like to call the MyMethod() that is actually found in A or B, assuming the object passed in is actually an instance of A or B, not that which is found in BaseClass. Short of doing something like this:

    Code:
    public class AnotherObject
    {
    	public AnotherObject(BaseClass someObject)
    	{
    		A temp1 = someObject as A;
    		if (A != null)
    		{
    			A.MyMethod();
    		}
    		
    		B temp2 = someObject as B;
    		if (B != null)
    		{
    			B.MyMethod();
    		}
    	}
    }
    How can I do it?
    Last edited by DavidP; 02-19-2010 at 02:25 PM.
    My Website

    "Circular logic is good because it is."

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Code:
    using System;
    
    namespace ConsoleApplication8
    {
        class Program
        {
            static void Main(string[] args)
            {
                AnotherObject test;
                test = new AnotherObject(new A());
                test = new AnotherObject(new B());
                test = new AnotherObject(new BaseClass());
                Console.Read();
            }
        }
    
        public class BaseClass
        {
            public virtual void MyMethod()
            {
                Console.WriteLine("base class");
            }
        }
    
        public class A : BaseClass
        {
            public override void MyMethod()
            {
                Console.WriteLine("class A");
            }
        }
    
        public class B : BaseClass
        {
            public override void MyMethod()
            {
                Console.WriteLine("class B");
            }
        }
    
        public class AnotherObject
        {
            public AnotherObject(BaseClass someObject)
            {
                someObject.MyMethod();
            }
        }
    }
    This seems to work fine, unless I'm not understanding your question?

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Oops. I just realized my problem. One of my several classes which I thought was overriding the said function actually wasn't.

    Problem solved
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot declare inherited class of virtual Base
    By rogster001 in forum C++ Programming
    Replies: 6
    Last Post: 12-17-2009, 10:25 AM
  2. calling a class method within different class method
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2009, 10:56 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 3
    Last Post: 12-03-2001, 01:45 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM