C#长开关语句

时间:2015-06-30 19:52:31

标签: c#

我已经看过StackOverflow上有关c ++长开关语句的文章,但c#在这方面有所不同。

具体来说,我想在c#代码中替换一个非常长的switch语句,其中每个case语句都做同样的事情,只是字段名更改。

代码如下所示:

 case Fields.TRANSMITTERCONTACTPHONEEXT:
                {
                    int lengthNeeded = 0;
                    int currentLength = TransmitterContactTelephoneExtension.Length;
                    int lengthRequired = TransmitterContactTelephoneExtensionLength;
                    if (currentLength < lengthRequired)
                    {
                        lengthNeeded = lengthRequired - currentLength;
                        for (int i = 0; i < lengthNeeded; i++)
                        {
                            TransmitterContactTelephoneExtension += " ";
                        }
                    }
                } break;
            case Fields.TRANSMITTERFEIN:
                {
                    int lengthNeeded = 0;
                    int currentLength = TransmitterFEIN.Length;
                    int lengthRequired = TransmitterFEINLength;
                    if (currentLength < lengthRequired)
                    {
                        lengthNeeded = lengthRequired - currentLength;
                        for (int i = 0; i < lengthNeeded; i++)
                        {
                            TransmitterFEIN += " ";
                        }
                    }
                } break;

我想把它归结为一个单一的函数,可以找出我的意思,而不必使用switch语句。有没有办法传入包含字段名称的变量?

5 个答案:

答案 0 :(得分:2)

这看起来像字符串填充函数的代码重复多次。你可以拥有

case Fields.TRANSMITTERCONTACTPHONEEXT:
     TransmitterContactTelephoneExtension = TransmitterContactTelephoneExtension.PadRight(TransmitterContactTelephoneExtensionLength, ' ');
     break;
    ...

任何时候你发现自己一遍又一遍地重复代码,你可能会把它分解成一个单独的函数(如果还没有),只需用正确的参数调用它。

这也让我想知道你是否需要switch case语句,而不仅仅是一系列pad语句。但是你的代码中的内容会越来越远(在范围内)。你的帖子没有给我们足够的信息去那里。

最后,有些适用于你的问题,我的经验法则(不是原本我的,但我忘了我得到它的地方)是如果一个方法不仅仅是一个有意的(有意模糊的术语),那么我需要把它分解成其他单独的方法。这允许我查看一个方法并在不滚动的情况下理解它。它还迫使我将较长的过程分成较小的逻辑步骤。

答案 1 :(得分:1)

将所有可更改的值放入数组并将其索引到其中。确保enum int值与目标数组中的数据相同。

var current = (int) Fields.TRANSMITTERCONTACTPHONEEXT;

int lengthNeeded = 0;
int currentLength = LengthData[ current ] ;
int lengthRequired = RequiredData[current ];

if (currentLength < lengthRequired)
{
      lengthNeeded = lengthRequired - currentLength;
      for (int i = 0; i < lengthNeeded; i++)
      {
         Extensions[ current ] = Extensions[ current ] + " ";
      }
}

这篇文章介绍了交换机中操作的模式,并没有解决各个操作的任何本地化缺陷。请查看每个操作,并在需要的地方进行改进,以获得最佳的运营效率。

答案 2 :(得分:0)

预先声明变量,并仅将switch用于不同的分配:

int currentLength;
int lengthRequired;

switch (whatever) {
case Fields.TRANSMITTERCONTACTPHONEEXT:
    currentLength = TransmitterContactTelephoneExtension.Length;
    lengthRequired = TransmitterContactTelephoneExtensionLength;
    break;
case Fields.TRANSMITTERFEIN:
    currentLength = TransmitterFEIN.Length;
    lengthRequired = TransmitterFEINLength;
    break;
default:
    throw new Exception(); // Without this, the compiler will complain about uninitialized variables
}

int lengthNeeded = 0;
if (currentLength < lengthRequired)
{
    lengthNeeded = lengthRequired - currentLength;
    for (int i = 0; i < lengthNeeded; i++)
    {
        TransmitterFEIN += " ";
    }
}

switch (whatever) {
case Fields.TRANSMITTERCONTACTPHONEEXT:
    TransmitterContactTelephoneExtension += " ";
    break;
case Fields.TRANSMITTERFEIN:
    TransmitterFEIN += " ";
    break;
}

编辑:如果您可以选择用数组替换变量,OmegaMan的解决方案会更好。

答案 3 :(得分:0)

你真的应该把它分解成一个方法。这样你就不会一遍又一遍地重复代码。这样效率很低,并且在更新时可能会导致潜在的错误。

而不是循环,你应该在字符串上使用PadRight()方法。

我会这样做:

case Fields.TRANSMITTERCONTACTPHONEEXT:
     TransmitterContactTelephoneExtension = PadString(TransmitterContactTelephoneExtensionLength, TransmitterContactTelephoneExtension);
     break;
case Fields.TRANSMITTERFEIN:
     TransmitterFEIN = PadString(TransmitterFEINLength, TransmitterFEIN);
     break;


private string PadString(int requiredLen, string value)
{
     if (value == null) return String.Empty.PadRight(requiredLen, ' '); //Create an empty string when the value is null
     return value.PadRight(requiredLen, ' ');
}

答案 4 :(得分:0)

对我来说,似乎我们看不到的代码需要进行一些重构,但根据我们可以看到的内容,我建议您执行以下操作:

// 1. Have a class to hold your data
class FieldData
{
    public string Value { get; set; }
    public int LengthRequired { get; set; }

    public string RightPaddedValue
    {
        get { return Value.PadRight(LengthRequired, ' '); }
    }
}

// 2. Fill your data into a dictionary somehow... for example:
Dictionary<Fields, FieldData> fields = new Dictionary<Fields, FieldData>
{
    { 
        Fields.TRANSMITTERCONTACTPHONEEXT, 
        new FieldData {
            Value = TransmitterContactTelephoneExtension,
            LengthRequired = TransmitterContactTelephoneExtensionLength
        }
    },
    { 
        Fields.TRANSMITTERFEIN,
        new FieldData {
            Value = TransmitterFEIN,
            LengthRequired = TransmitterFEINLength
        }
    }
};

// 3. Then use the data from that dictionary in your code:
FieldData data = fields[selectedField];
data.RightPaddedValue; // use RightPaddedValue