Calling the overriden method from the base class

This is a discussion on Calling the overriden method from the base class within the C# Programming forums, part of the General Programming Boards category; Code: public class BaseClass { public virtual void MyMethod() { ...do something... } } public class A : BaseClass { ...

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

    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 01: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,738
    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, 09: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, 10:03 PM
  4. Replies: 3
    Last Post: 12-03-2001, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21