如何在比较小于,大于或等于?之间动态切换?

时间:2009-09-25 18:29:36

标签: c#

基本上我给用户提供了根据大小过滤一组文件的选项。

用户从下拉列表中选择一个比较类型(大于,小于,等于),然后输入一个大小(以字节为单位)进行比较。这就是我到目前为止所做的:

switch (cmboCompareType.SelectedText)
{
    case "Greater Than":
        fileOK = fi[i].Length > int.Parse(txtByteValue.Text);
        break;
    case "Less Than":
        fileOK = fi[i].Length < int.Parse(txtByteValue.Text);
        break;
    case "Equal To":
        fileOK = fi[i].Length == int.Parse(txtByteValue.Text);
        break;
}

有没有更优雅的方式来进行这种比较,而无需在C#中重复这么多代码?

8 个答案:

答案 0 :(得分:18)

两个选项:

1)使用CompareTo和Sign:

int requiredSign;
switch (cmboCompareType.SelectedText)
{
  case "Greater Than": requiredSign = 1; break;
  case "Less Than": requiredSign = -1; break;
  case "Equal To": requiredSign = 0; break;
  default: throw new ArgumentException();
}
fileOK = Math.Sign(fi[i].Length.Compare(txtByteValue.Text)) == requiredSign;

2)使用代表:

static readonly Func<int, int, bool> GreaterThan = (x, y) => x > y;
static readonly Func<int, int, bool> LessThan = (x, y) => x < y;
static readonly Func<int, int, bool> Equal = (x, y) => x == y;
...

Func<int, int, bool> comparison;
switch (cmboCompareType.SelectedText)
{
  case "Greater Than": comparison = GreaterThan; break;
  case "Less Than": comparison = LessThan; break;
  case "Equal To": comparison = Equal; break;
  default: throw new ArgumentException();
}
fileOK = comparison(fi[i].Length, int.Parse(txtByteValue.Text));

答案 1 :(得分:10)

int value = int.Parse(txtByteValue.Text);
int len = fi[i].Length;

switch (cmboCompareType.SelectedText)
{
    case "Greater Than": fileOK = len > value; break;
    case "Less Than": fileOK = len < value; break;
    case "Equal To": fileOK = len == value; break;
}

TADA!减少重复。 ; P

答案 2 :(得分:5)

我不是案例陈述的粉丝,但同样的事情,只是另一种方式。


var fileOK = new Dictionary<string, Func<int, int, bool>>
    (StringComparer.OrdinalIgnoreCase)
    {
        { "Greater Than", (x, y) => x > y },
        { "Less Than", (x, y) => x < y },
        { "Equal To", (x, y) => x == y }
    }[cmboCompareType.SelectedText](fi.Length, int.Parse(txtByteValue.Text));

答案 3 :(得分:1)

int actual = Math.Sign(fi[i].Length.CompareTo(int.Parse(txtByteValue.Text)));
int expected;

switch (cmboCompareType.SelectedText)
{
    case "Greater Than": expected = +1; break;
    case "Less Than":    expected = -1; break;
    case "Equal To":     expected =  0; break;
}

fileOK = (actual == expected);

答案 4 :(得分:1)

如果您有地图,可以使用以下内容:

private int CompareOp(string Text)
{
    switch (cmboCompareType.SelectedText)
    {
        case "Greater Than": 
            return 1;
        case "Less Than": 
            return -1;
        case "Equal To": 
            return 0;
    }
}

// In your method:
fileOK = (fi[i].Length.CompareTo(value) == CompareOp(cmboCompareType.SelectedText);

答案 5 :(得分:1)

在int

上创建扩展方法
public static int Compare(this int a, int b, string compareType)
{
    switch (CompareType)
    {
        case "Greater Than":
            return fi[i].Length > int.Parse(txtByteValue.Text);
            break;
        case "Less Than":
            return fi[i].Length < int.Parse(txtByteValue.Text);
            break;
        case "Equal To":
            return fi[i].Length == int.Parse(txtByteValue.Text);
            break;
    }
}

然后将其用作:

fileOK = fi[i].Length.Compare(int.Parse(txtByteValue.Text), cmboCompareType.SelectedTex);

答案 6 :(得分:1)

您还可以将值-1,0,1添加到每个组合框项的value属性中,然后代码如下所示:

fileOK = fi[i].Length.CompareTo(int.Parse(txtByteValue.Text) == cmboCompareType.SelectedValue;

答案 7 :(得分:0)

我自己的想法,受到使用Math.Sign()的解决方案的启发。由于下拉框有三个值,其索引为0,1和2,我可以减去1得到-1,0,1,然后用它来与符号进行比较,如下:

int sign = Math.Sign(fi[i].Length.CompareTo(int.Parse(txtByteValue.Text)));
fileOK = sign == (cmboCompareType.SelectedIndex - 1);

这取决于按此特定顺序放置的列表框中的项目:
少于
等于
大于

当然,只有这些是列表框中唯一的值才会起作用。可能不是最好的解决方案,但我得知我只用两行代码完成了这个。

编辑:技术上我可以简化为一行代码:

fileOk=(cmboCompareType.SelectedIndex-1) == Math.Sign(fi[i].Length.CompareTo(int.Parse(txtByteValue.Text)));