NET enumeration of symbolic constants for string values

I have a list of fairly meaningless codes that I process using a VB.NET Windows application. For the business logic that I write to process these codes, I would like to use meaningful constants (e.g., ServiceNotCoveredor MemberNotEligible) instead of source codes (e.g., "SNCV"and "MNEL").

As far as I can tell, Enums can only display numeric values, not strings. Thus, the best I could come up with is a static class that provides constants as readonly static strings that are internally set equal to code values, as follows.

Public Class MyClass

    private _reasonCode as String()
    Public Property ReasonCode() As String
        'Getter and Setter...
    End Property

    Public Class ReasonCodeEnum
        Private Sub New()
        End Sub
        Public Shared ReadOnly ServiceNotCovered As String = "SNCV"
        Public Shared ReadOnly MemberNotEligible As String = "MNEL"
        'And so forth...
    End Class

End Class

'Calling method
Public Sub ProcessInput()
    Dim obj As New MyClass()
    Select Case obj.ReasonCode
        Case MyClass.ReasonCodeEnum.ServiceNotCovered
            'Do one thing
        Case MyClass.ReasonCodeEnum.MemberNotEligible
            'Do something different
        'Other enum value cases and default
    End Select
End Sub

, MyClass.ReasonCode ReasonCodeEnum, ReasonCodeEnum .

, Enum , , , .

+3
7

VB.Net, , :

+1

enum, Select Case:

Public Enum ReasonCode
    ServiceNotCovered
    MemberNotEligible
End Enum


Private mapping As New Dictionary(Of String, ReasonCode)
' Add the required mappings and look up the dictionary...
+7

:

1) Description . .

:

  • switch

:

  • OO

2) .NET enum - - Java. ( ) /. #, , - , #, VB:)

public sealed class Reason
{
    public static readonly Reason ServiceNotCovered = new Reason("SNCV");
    public static readonly Reason MemberNotEligible = new Reason("MNEL");

    private readonly string code;

    private Reason(string code)
    {
        this.code = code;
    }

    public string Code
    {
        get { return code; }
    }
}

( , # - , VB Select ), , , . . . :

public class Reason
{
    public static readonly Reason ServiceNotCovered = new ServiceReason();
    public static readonly Reason MemberNotEligible = new EligibilityReason();

    private readonly string code;

    private Reason(string code)
    {
        this.code = code;
    }

    public string Code
    {
        get { return code; }
    }

    public abstract void DoSomething();

    private class ServiceReason : Reason
    {
        internal ServiceReason() : base("SVNC") {}

        public override void DoSomething()
        {
            // Whatever
        }
    }

    private class EligibiltyReason : Reason
    {
        internal EligibiltyReason() : base("MNEL") {}

        public override void DoSomething()
        {
            // Do something else
        }
    }
}

, , , , - .

, VB , #, , ( , ) .

, "" . , - . , - Enum.IsDefined, .

+2

, , , ,

Dictionary<string, MyEnum> StringEnumMap;

google ( ) Simple State Machine codeplex . , , , . :

var phoneCall = new StateMachine<State, Trigger>(State.OffHook);

phoneCall.Configure(State.OffHook)
    .Allow(Trigger.CallDialed, State.Ringing);

phoneCall.Configure(State.Ringing)
    .Allow(Trigger.HungUp, State.OffHook)
    .Allow(Trigger.CallConnected, State.Connected);

phoneCall.Configure(State.Connected)
    .OnEntry(t => StartCallTimer())
    .OnExit(t => StopCallTimer())
    .Allow(Trigger.LeftMessage, State.OffHook)
    .Allow(Trigger.HungUp, State.OffHook)
    .Allow(Trigger.PlacedOnHold, State.OnHold);

phoneCall.Configure(State.OnHold)
    .SubstateOf(State.Connected)
    .Allow(Trigger.TakenOffHold, State.Connected)
    .Allow(Trigger.HungUp, State.OffHook)
    .Allow(Trigger.PhoneHurledAgainstWall, State.PhoneDestroyed);
+1

. , , ?

Const ServiceNotCovered As String = "SNCV"
Const MemberNotEligible As String = "MNEL"

. / - .

- , . , , - - ( ) .

, , , , .

+1

, ( Enum) . , , .

0

, , . "NA" , . "". , , . , , .

:

using System.ComponentModel;
...
EnumConverter conv = new EnumConverter( typeof( MyEnum ) );
...
conv.ConvertToString( ... );
conv.ConvertFromString( ... );

, , switch.

0

Source: https://habr.com/ru/post/1709528/


All Articles