A
A
Anton_repr2020-06-03 11:16:51
C++ / C#
Anton_repr, 2020-06-03 11:16:51

Should I use enums here?

I was looking for projects to parse and found this code: 5ed75c0aaa579747128461.png
It seems to me that instead of classes there should be enums here?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Collin, 2020-06-03
@Anton_repr

Worth it, it will be more convenient and understandable.

namespace TryEnum
{
    enum DownloadType
    {
        Video, Audio, Custom, Unknown, Default
    }
    enum ForceIpProtocol
    {
        IPv4, IPv6
    }
    enum ProxyProtocol
    {
        HTTPS, HTTP, SOCKS4, SOCKS5
    }
}

And to use like this:
5ed7670d68591771166057.png
And about interfaces... Interfaces should not be instead of classes; the class implements the interface (the syntax looks like inheritance , but this is not quite inheritance, there is a slightly different implementation). And the interface is needed if you need to implement polymorphism in the code and / or to simplify access to public fields in classes that implement this interface. For example: we have many classes with different implementations, but they all share the same properties (for example, public string Name ( get; set; ); public int Age( get; set; ); etc...) . The interface in this case is needed so that through it we get access to multiple behavior, using different classes containing properties of the same name, without producing code. For clarity:class ClassName : IClassName
we have the Create() method; in which we want to create new people
static void Create()
        {
            // empty...
        }

There are different classes that create a Human and implement the IHuman interface
class SimpleHuman : IHuman
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public SimpleHuman()
        {
            Name = "Generic human...";
            Age = 20;
        }
        public SimpleHuman(string name) : this()
        {
            Name = name;
        }
        public SimpleHuman(string name, int age) : this()
        {
            Name = name;
            Age = age;
        }
    }

class CoolHuman : IHuman
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public CoolHuman()
        {
            Name = "BOB!";
            Age = 20;
        }
        public CoolHuman(string name) : this()
        {
            Name = name;
        }
        public CoolHuman(string name, int age) : this()
        {
            Name = name;
            Age = age;
        }
    }

interface IHuman
    {
        string Name { get; set; }
        int Age { get; set; }
    }

And now, thanks to the implementation of the interface in these classes, we can conveniently use this very interface, for example:
class Create
    {
        public IHuman Human { get; set; }
        public Create()
        {
            Human = new CoolHuman();
        }
        public void Hello()
        {
            Console.WriteLine("Hello, {0}. Today you are {1} years old", Human.Name, Human.Age);
        }
    }

Specifically, in your case, the interface is not needed.

#
#, 2020-06-03
@mindtester

programming is an interesting thing - does it work? Do not touch!

It seems to me that instead of classes there should be interfaces here?
in the text of the question
Should I use enums here?
you already decide in the Wishlist:
- transfers?
- interfaces?
- both?
.. why? (c) from a good anecdote
and the moral is simple - if you understand why? cut, test, use!
ps from the outside, all this is impossible to evaluate without detailed usage scenarios

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question