-2

So we have Order which is Logic and IOrderData which connects the View and the Logic when it comes to data like properties. Order has properties of enums PayMethod and OrderStatus. How do I make properties of PayMethod and OrderStatus in IOrderData without the interface-layer knowing either Logic nor View?

Example:

Project Logic:

public class Order : IOrder, IOrderData
{
    public enum PayMethod
    {
        iDeal,
        creditcard,
        PayPal
    }

    public enum OrderStatus
    {
        NotPaid,
        InTheMaking,
        Shipped,
        Delivered,
        Confirmed
    }

public OrderStatus Status { get; set; }
public PayMethod Paymethod { get; set; }

Project DataInterface:

public interface IOrderData
{
    public OrderStatus Status { get; set; } //doesn't work
    public PayMethod Paymethod { get; set; } //doesn't work
}

My solution: I just made a new Class Library for Enums with classes of PayMethod and OrderStatus and everywhere where enums we're used I referred to the Class Library.

zhrgci
  • 584
  • 1
  • 6
  • 25

3 Answers3

3

This code doesn't compile because your enums are members of the Order class. If you move them outside the class and make them top-level members of your namespace, as stated in official documentation:

Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct.

This will solve the problem:

public enum PayMethod
{
    iDeal,
    creditcard,
    PayPal
}

public enum OrderStatus
{
    NotPaid,
    InTheMaking,
    Shipped,
    Delivered,
    Confirmed
}
public interface IOrderData
{
    OrderStatus Status { get; set; } 
    PayMethod Paymethod { get; set; }
}
public class Order : IOrder, IOrderData
{

public OrderStatus Status { get; set; }
public PayMethod Paymethod { get; set; }
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • top-level members of my namespace. So does that mean putting it above the namespace or make a whole new project dedicated to enums? – zhrgci Nov 25 '19 at 08:37
  • @Zheng-rongCai If the enums are part of the interface, generally you would put their definitions in the same source code file as the interface itself. – Matthew Watson Nov 25 '19 at 08:39
  • This means to put them directly under the namespace, not inside the class as the code in the question. You don't need a different project to put them into. – Zohar Peled Nov 25 '19 at 08:42
  • But the interface can't know the Logic(where the enum is) so it still gives the error – zhrgci Nov 25 '19 at 09:29
  • Enums are not logic. they are merely a group of constants. The interface isn't logic either, it's merely a contract. All the logic is on the type that implements the interface. – Zohar Peled Nov 25 '19 at 09:32
  • So if I don't put it in the logic namespace, where do I put them than? Because the type still coudn't be found. – zhrgci Nov 25 '19 at 09:39
  • The enums and interfaces belong on the same project. The implementation can be on a different project (I think I've finally understood what it is you're asking here). – Zohar Peled Nov 25 '19 at 10:29
1

Your enums are subtypes to Order therefore you have to fully qualify the name with Order. like so:

public interface IOrderData
{
    Order.OrderStatus Status { get; set; }
    Order.PayMethod Paymethod { get; set; }
}

Also public doesn't work for interface-members in C# 7.3 and below (see this question for C# 8+).

Alternatively, you can move the enums outside of the Order class like described in this other answer.

Joelius
  • 3,839
  • 1
  • 16
  • 36
1

Move the declaration of the enums outside of the class declaration, otherwise the interface won't know anything about that enum.

public enum PayMethod
{
    iDeal,
    creditcard,
    PayPal
}

public enum OrderStatus
{
    NotPaid,
    InTheMaking,
    Shipped,
    Delivered,
    Confirmed
}

public class Order : IOrderData
{
    public OrderStatus Status { get; set; }
    public PayMethod Paymethod { get; set; }
}

public interface IOrderData
{
    OrderStatus Status { get; set; }
    PayMethod Paymethod { get; set; }
}
Cid
  • 14,968
  • 4
  • 30
  • 45