2011-05-05 49 views
1

我有這個2枚舉,我需要涉及PendingStatus到的TransactionStatus一對多枚舉在C#

講解,對每個事務有我使用的TransactionStatus枚舉爲一個單一的狀態,然後爲每個TransactionStatus對象有我使用PendingStatus枚舉的許多懸而未決的原因?

/// <summary> 
/// Represent all available status for Transaction 
/// </summary> 
public enum TransactionStatus 
{ 
    New =0, 
    Submitted =1, 
    PendingStatus = 2, 
    Accepted = 3, 
    Rejected =4, 
    InProgress =5, 
    Completed=6, 
    Failed=7, 
    Canceled=8 
} 

/// <summary> 
/// Represent all available pending status for Transaction 
/// </summary> 
public enum PendingStatus 
{ 
    PendingA =0, 
    PendingX =1, 
    PendingY = 2, 
} 

我該如何解決這個問題?

回答

1

您可以使用枚舉值作爲位標記,如解釋here

這樣,您可以將PendingStatus和TransactionStatus混合在一起。

/// <summary> 
/// Represent all available status for Transaction 
/// </summary> 
[Flags] 
public enum TransactionStatus 
{ 
    New = 0, 
    Submitted = 1, 
    PendingStatus = 2, 
    Accepted = 4, 
    Rejected = 8, 
    InProgress = 16, 
    Completed = 32, 
    Failed = 64, 
    Canceled = 128 
} 

/// <summary> 
/// Represent all available pending status for Transaction 
/// </summary> 
[Flags] 
public enum PendingStatus 
{ 
    PendingA = 256, 
    PendingX = 512, 
    PendingY = 1024 
} 

// Example to set transaction as accepted and pending 

var MyTransactionStatus = Accepted & PendingA; 

// How to check transaction is pendingA regardless of its status ? 

if (MyTransactionStatus & PendingA == PendingA) ... 
+0

您可能想要演示如何。 – jgauffin 2011-05-05 19:15:44

+0

設置標誌枚舉使用十六進制fwiw更加清晰 – 2011-05-05 19:22:43

1

我會使用FlagsAttribute。所以你可以使用二進制比較。

[FlagsAttribute] 
public enum TransactionStatus 
{ 
    New = 0, 
    Submitted = 1, 
    PendingStatus = 2, 
    Accepted = 4, 
    // (...) 
} 

[FlagsAttribute] 
public enum PendingStatus 
{ 
    PendingA = 256, 
    PendingX = 512, 
    PendingY = 1024, 
}