0

How do I use something like this in C#.

 Console.WriteLine("yaya instant");
 Server.registerEvent(new Event(5000) {
    public void doWork() {
        this.stop();
        Console.WriteLine("yaya 5 seconds later");
    }
  });

Event class and the doWork() method is declared inside Event class.

Pretty much what is going on is doWork() is abstract method which should get created by the code above.

Whats the proper syntax to do this in C#? is it even possible to do anonymous methods like this?. (The above code is not valid syntax, if someone doesn't understand this question.)

Thank you I appreciate the help.

SSpoke
  • 5,656
  • 10
  • 72
  • 124
  • You wrote "Event class and the doWork() method is declared inside Event class." How can Event class be declared inside Event class? Is it declared inside itself? – Andrew Savinykh Jun 10 '11 at 09:55
  • public abstract void doWork(); inside Event.cs there is no method. – SSpoke Jun 10 '11 at 10:13

5 Answers5

0

No you can't do this. You need to create a sub class of Event that accepts an action.

public class MyEvent: Event
{
    private Action _action;        

    public MyEvent(int milliseconds, Action action)
        : base(milliseconds)
    {
        _action= action;
    }

    public override void doWork()
    {
        action()
    }
}

Then you can do this:

Server.registerEvent(new MyEvent(5000, () => 
    {
        this.stop();
        Console.WriteLine("yaya 5 seconds later");
    }));

You just need to declare MyEvent once (call it something better), and then you can create instances of it with different actions when you need them.

Ray
  • 45,695
  • 27
  • 126
  • 169
0

You can't have anonymous classes in C#. Is that what you are asking? But in C# you normally wouldn't implement a class for handling an event anyway.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • What is my other option for crafting event on demand all over my code other then using timer class. – SSpoke Jun 10 '11 at 09:55
  • I don't understand really what you want to achieve. – Daniel Hilgarth Jun 10 '11 at 09:57
  • I want to make events on demand all over classes.. yet keeping a nice design. Without making events in a new class.. but keeping all the logic in the same class file. Ah it's alright I guess i'll try something else. – SSpoke Jun 10 '11 at 10:01
  • I still don't understand your requirement. What is an "event on demand"? – Daniel Hilgarth Jun 10 '11 at 10:02
  • Ah nothing just trying to keep it pretty looking and creating events when I want too in a simplistic ways to make scripts easily craftable. – SSpoke Jun 10 '11 at 10:11
0

The closest thing I can think of is this:

using System;
using System.Threading;

namespace SO6304593
{    
    class Program
    {
        static void Main(string[] args)
        {
             Console.WriteLine("yaya instant");

            Thread t1 = new Thread(delegate(object x) { Thread.Sleep((int)x); Console.WriteLine("Hello 1");});
            Thread t2 = new Thread(x => {Thread.Sleep((int)x); Console.WriteLine("Hello 2");});

            t1.Start(5000);
            t2.Start(3000);
            t1.Join();
            t2.Join();
        }
    }
}

Is this what you are after? The above are two examples of using anonymous methods in C#: the delegated syntax and the lambda syntax.

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
0

Give your event class a property of type Action:

public class Event
{
    public Action doWork { get; set; }
}

Then use as follows:

Console.WriteLine("yaya instant");
Server.registerEvent(new Event(5000) {
    doWork = delegate {
        this.stop();
        Console.WriteLine("yaya 5 seconds later");
    }
});
Simon Bartlett
  • 2,010
  • 14
  • 15
0

To achieve the method injection add an Action arg in the constructor to Event then you could do it like this;

Console.WriteLine("yaya instant");
Server.registerEvent(
    new Event(5000, () {
        stop();
        Console.WriteLine("yaya 5 seconds later");
        )
    );

Event::Event(int time, Action action){...}
Adam Straughan
  • 2,766
  • 3
  • 20
  • 27