简体   繁体   English

将字符串验证为Int32或Int64

[英]Validate strings as Int32 or Int64

I use the following code to validate income numbers from ajax calls: 我使用以下代码来验证来自ajax调用的收入数字:

    public Tuple<bool, int> ValidateInt(string TheCandidateInt)
    {
        int TheInt = 0;

        if (Int32.TryParse(TheCandidateInt, out TheInt))
        {
            return new Tuple<bool, int>(true, TheInt);
        }
        else
        {
            return new Tuple<bool, int>(false, 0);
        }
    }

    public Tuple<bool, short> ValidateShort(string TheCandidateShort)
    {
        short TheShort = 0;

        if (Int16.TryParse(TheCandidateShort, out TheShort))
        {
            return new Tuple<bool, short>(true, TheShort);
        }
        else
        {
            return new Tuple<bool, short>(false, 0);
        }
    }

I also have the same types of function for Byte and Short . 对于ByteShort我也有相同类型的函数。 As you can see, I pass in a string and the return is a Tuple where Item1 is a boolean and Item2 is the value. 如您所见,我传入一个字符串,返回值是一个元组,其中Item1是布尔值,而Item2是值。

Is there a way to change these 4 methods into 1 and somehow factor out the data type of the parsing? 有没有办法将这4种方法更改为1,并以某种方式将解析的数据类型分解出来?

Thanks. 谢谢。

You can merge your methods into one using generics: 您可以使用泛型将方法合并为一种:

public static Tuple<bool, T> ValidateValue<T>(string input) where T : struct
{
    T result = default(T);
    try
    {
        result = (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(input);
    }
    catch
    {
        return new Tuple<bool, T>(false, result);
    }
    return new Tuple<bool, T>(true, result);
}

As to factoring out the data type of parsing, you will still have to specify it as generic parameter. 至于分析的数据类型,您仍然必须将其指定为通用参数。

ValidateValue<byte>("255");

You can create a delegate that encapsulates the TryParse call and by that create a more general version of your validation methods. 您可以创建一个封装了TryParse调用的委托,从而创建一个更通用的验证方法版本。 The following sample shows the outline: 下面的示例显示了大纲:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestTryParseDelegate
{
    class Program
    {
        private delegate bool TryParseDelegate<T>(string candidate, out T result)
            where T : struct;

        private static Tuple<bool, T> Validate<T>(string candidate, TryParseDelegate<T> tryParseDel)
            where T : struct
        {
            T result;
            return Tuple.Create(tryParseDel(candidate, out result), result);
        }

        public static Tuple<bool, int> ValidateInt(string TheCandidateInt)
        {
            return Validate<int>(TheCandidateInt, int.TryParse);
        }

        public static void Main()
        {
            var result = ValidateInt("123");
            Console.WriteLine(result);
        }
    }
}

I'd still suggest to encapsulate the general version in specialized methods as in your sample as the delegate is a implementation detail that you might not want to publish. 我仍然建议像您的示例一样,将通用版本封装在专用方法中,因为委托是您可能不希望发布的实现细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM