How to delegate UI methods automatically

Asked

Viewed 64 times

0

I created these methods in the interface:

public interface IEvents
    {
        void OnEscrever(string text);
        void OnEnable(bool b);
        void OnDisable(bool b);
    }

I’ll have some classes that will inherit these methods:

public class Herda1 : IEvents
    {
        public void OnDisable(bool b)
        {
            throw new NotImplementedException();
        }

        public void OnEnable(bool b)
        {
            throw new NotImplementedException();
        }

        public void OnEscrever(string text)
        {
            throw new NotImplementedException();
        }
    }

public class Herda2 : IEvents
    {
        public void OnDisable(bool b)
        {
            throw new NotImplementedException();
        }

        public void OnEnable(bool b)
        {
            throw new NotImplementedException();
        }

        public void OnEscrever(string text)
        {
            throw new NotImplementedException();
        }
    }

I want every method of the class Herda1 and Herda2 or other classes they inherit IEvents, receive values per parameters.

Let’s imagine the component Timer from Windows Form, it has its event called Timer1_Tick by default, I cannot see how the script of this control unfolds internally, I can only pull the value through the method parameter, or by the instance.

What if I wanted to play that method on the interface? E All classes that inherit this interface, could receive by parameter values developed internally? And these methods were already delegated by default?

In java, I used (Implements Interface) to implement the methods. In C# I have no idea how to do it.

Just follow my lead:

inserir a descrição da imagem aqui

I want when the interface methods are implemented automatically, to be delegates in any of the above green classes. Is that possible? If so, how?

Let’s suppose the very event of C#

    public delegate void Disable(bool b);
    public delegate void Enable(bool b);
    public delegate void OnEscrever(string text);

    public class ImplementaInterface : IEvents1 // efetua trabalho interno
    {
        public event Disable OnDisable;
        public event Enable OnEnable;
        public event OnEscrever OnEscrever;

        public ImplementaInterface()
        {
            this.OnDisable += new Disable(??????????); // eis a questão
            this.OnEnable += new Enable(??????????); // eis a questão
            this.OnEscrever += new OnEscrever(??????????); // eis a questão

            this.OnEnable(true);
            this.OnDisable(true);
            this.OnEscrever("Ola Mundo!");
        }
    }

    public interface IEvents1 // qualquer classe pode herdar essa interface
    {
        event Disable OnDisable;
        event Enable OnEnable;
        event OnEscrever OnEscrever;
    }

    public class Herda1 : IEvents1 // esta classe esta herdando a interface
    {
        public event Disable OnDisable; // isso não é método delegado
        public event Enable OnEnable;
        public event OnEscrever OnEscrever;
    }

    public class Herda2 : IEvents1 // esta também está herdando
    {
        public event Disable OnDisable;
        public event Enable OnEnable;
        public event OnEscrever OnEscrever;
    }

Notice that I put in the interface the operator event, when some class instantiates this interface, it will not receive methods, but variable what I do not want. To understand, note the question comment, this is where I want to delegate the interface methods.

The code isn’t 100%, but that’s all I’m getting at.

  • Don’t I understand your doubt? a confusion only, in this case you implement interface in the classes is equal in C#.

  • I don’t know C#, but when I used to use an interface in Java, I got automatically delegated methods.

  • if you are talking about methods already implemented the approach is class abstract? that would be it.?

  • 1

    Yes, too, but receive parameters and any class that inherit the interface, have methods already delegated without I need to delegate, because if I do this, will not fulfill the internal script work, but it is I who will do this work.

  • Put an example in your question, explaining the functionality, I’m wanting to understand where you’re going, I think (achometric) you need a class abstract with methods already implemented or inheritance in a base class ... !!!

  • 1

    I left an example at the end of the/

  • 1

    You need to implement an independent method whatever it is, so in case it has to be a class abstract or even a base class making inheritance seeing from afar I think that’s it.

  • Has as an example?

  • Check it out: https://www.codepile.net/pile/AzWqV1nJ but, I’m not sure that’s it.

Show 5 more comments

1 answer

1

If I understand correctly, you have to wait for the C# 8 that will have it called Default Interface Methods. If you cannot wait you will have to implement the methods in the classes, there is no way.

What you can do if it is all the same implementation is to create a static class with the implemented methods, there in each class in the implementation of the method you just call the static method, going over the received parameters, this way is delegating the execution is no need to repeat the code, and if change the static method reflects in the execution of all classes. This is the same solution you had to do in Java until version 7.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.