Hi, I have a question regarding Methods.
I have a method that takes a base class. I am getting an Error when I pass class that is derived of the base to it.
For Example

Class TaskToRun
{
public virtual void run ();
}

Class SayHi : TaskToRun
{
public run()
{
Console.WriteLine("Hi");
}

Now I can't pass SayHi to a method that takes TaskToRun as its parameter:

RunTask(TaskToRun task)
{
delegate void MethodToRun();
MethodToRun methodtorun = new MethodToRun(task.run);
methodtorun();
}

Thank you so much for any help!

Recommended Answers

Hello, HBMSGuy.
Are you sure, that you have error about passing derived class when method asks the base one? Only thing, that I see it's a few mistakes (in other area):

Class TaskToRun
{
     public virtual void run ();
}

here if you don't specify any functionality …

Jump to Post

All 3 Replies

Hello, HBMSGuy.
Are you sure, that you have error about passing derived class when method asks the base one? Only thing, that I see it's a few mistakes (in other area):

Class TaskToRun
{
     public virtual void run ();
}

here if you don't specify any functionality to the method 'run', call the calss (also as this method) as abstract. Or add empty brackets pair: { } - if you want to leave it as virtual.

Class SayHi : TaskToRun
{
    public run()
    {
        Console.WriteLine("Hi");
    }
}

if you decided to make your base class abstract, you should add 'override' keyword to method run . Also the return type is also needed :P
if you decided to leave your method in base class as virtual - you have 2 ways to go:

Warning 'SayHi.run()' hides inherited member 'TaskToRun.run()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

RunTask(TaskToRun task)
{
   delegate void MethodToRun();
   MethodToRun methodtorun = new MethodToRun(task.run);
   methodtorun();
}

The delegate declaration should be at class level (like you would declare variable in the class) .. or higher - in namespace. You decide.
Also there're some notes about initializing and calling it. Look here, there is good example: delegate (C# Reference).

Well, here's what we have in result (my version):

delegate void MethodToRun();

class TaskToRun
    {
        public virtual void run() { }
    }

class SayHi : TaskToRun
    {
        public override void run()
        {
            Console.WriteLine("Hi");
        }
    }
public class Test
    {        
        void RunTask(TaskToRun task)
        {
            MethodToRun methodtorun = task.run;
            methodtorun();

        }
        static void Main()
        {
             RunTask(new SayHi());
        }
    }
commented: Ideal answer (Y) +7

Thank you so much. I think your first idea was what I was looking for.
You were correct, how I was trying to complete the task was wrong.

OK. I thought that was my issue, but it wasn't. :(
Sorry there is a lot of code.

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Trajectory
{
    abstract class TaskToRun
    {
        public abstract void run();
    }
    class ProjectilePath : TaskToRun
    {   //This will contain the data for each one of the launchers combinations
        //Many Varibles
        public override void run()
        {
            Console.WriteLine("Running in background");
        }
    }
    class Threads
    {   //Will help create and manage the threads that are to be used by the program
        private TaskToRun[] dervivedobject;
        private delegate void MethodToRun();
        private MethodToRun[] methodtorun;
        public Threads(ref TaskToRun[] in_derivedobject)
        {
            this.dervivedobject = in_derivedobject;
            //Initilize the Array of Delegates
            Array.Resize(ref this.methodtorun, (int)this.numberoftasks_backing);
            for (int index = 0; index < this.derivedobject.Length; index++)
            {
                methodtorun[index] = new MethodToRun((dervivedobject[index]).run);
                Console.WriteLine("Added method to methodtorun[{1}]", index);
            }
        }
        //Other Methods
    }
    class Test
    {
        public static void Main()
        {
        ProjectilePath[] arrayoftasks = new ProjectilePath[50];
        Threads treads = new Threads(arrayoftasks);
        }
    }
}

Thanks so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.