验证“设置”的最佳方法财产.. [英] Best approach to validating "set" property ..

查看:59
本文介绍了验证“设置”的最佳方法财产..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


我有一个需要验证输入值的类,当程序员

更改某个类的特定属性时。输入应该只接受

以下模式[a-zA-Z0-9] {1,n}(带至少一个条目的字母数字)。


我对实现正则表达式验证很有抵抗力,因为涉及的开销因为
(不是因为我不能:-)。显然,使用常规字符串操作实现模式验证是可能的。


我应该选择什么?我认为访问频率相当低,但是我不想走错方向 - 也就是未来的API

开发。


//为了简洁省略了类定义..


///< summary>

///获取或设置区域名称。区域名称必须是[a-zA-Z0-9]形式的完全

///限定的ASP.NET控件ID。

///< ; / summary>

公共字符串AreaName

{

get {return this.areaName; }

设置

{

//这里包括验证..

// RegEx或string-ops?

this.areaName = value;

}

}


提前致谢!


-

venlig hilsen /有问题

anders borum

-

Hello!

I have a class that needs to validate the input value, when a programmer
changes a specific property on a class. The input should only accept the
following pattern [a-zA-Z0-9]{1,n} (alpha-numeric with atleast one entry).

I''m a little resistant to implementing a regular expression validation,
because of the overhead involved (not because I can''t :-). Obviously, it''s
possible to implement the pattern validation using regular string ops ..

What should I choose? I imagine the access frequency is rather low, but I
don''t want to go the wrong direction - also in terms of future API
development.

// Class definition omitted for brevity..

/// <summary>
/// Gets or sets the Area name. The Area name must be a fully
/// qualified ASP.NET control id in the form of [a-zA-Z0-9].
/// </summary>
public string AreaName
{
get { return this.areaName; }
set
{
// include validation here ..
// RegEx or string-ops?
this.areaName = value;
}
}

Thanks in advance!

--
venlig hilsen / with regards
anders borum
--

推荐答案

Anders Borum< na@na.na>写道:
Anders Borum <na@na.na> wrote:
当程序员更改某个类的特定属性时,我有一个需要验证输入值的类。输入应该只接受
以下模式[a-zA-Z0-9] {1,n}(带至少一个条目的字母数字)。

我有点抵抗实现正则表达式验证,
因为涉及的开销(不是因为我不能:-)。显然,使用常规字符串操作实现模式验证是可能的。

我应该选择什么?我认为访问频率相当低,但我不想走错方向 - 也就是未来的API开发方面。
I have a class that needs to validate the input value, when a programmer
changes a specific property on a class. The input should only accept the
following pattern [a-zA-Z0-9]{1,n} (alpha-numeric with atleast one entry).

I''m a little resistant to implementing a regular expression validation,
because of the overhead involved (not because I can''t :-). Obviously, it''s
possible to implement the pattern validation using regular string ops ..

What should I choose? I imagine the access frequency is rather low, but I
don''t want to go the wrong direction - also in terms of future API
development.




如果正则表达式很容易阅读,但是手工制作的

代码并不是很简单,那么请使用正则表达式直到你

有证据证明它太慢了。如果你最终得到一个可以在实际代码中轻松表达的怪物

正则表达式,请将

作为代码留下。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



If the regular expression is simple to read, but the "hand-crafted"
code wouldn''t be very simple, go with the regular expression until you
have evidence that it''s being too slow. If you end up with a monster
regular expression that can easily be expressed in actual code, leave
it as code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Hello Jon


(感谢快速回复)


我在想RegEx方法因为代码干净(并且在RegEx模式的变化方面具有令人难以置信的灵活性),与

相比,字符串操作更加冗长(并且更难阅读)。 />

正如我所说,该物业不太可能被广泛使用,因此RegEx可能是目前最好的方法。谢谢你的想法。


-

venlig hilsen /关心

anders borum

-
Hello Jon

(and thanks for the quick reply)

I was thinking of the RegEx approach because of the clean code (and
incredible flexibility in terms of changes the RegEx pattern), compared to
the more verbose (and harder to read) string ops.

As I said, the property is unlikely to be used extensively, so the RegEx is
probably the best approach at the moment. Thanks for your thoughts.

--
venlig hilsen / with regards
anders borum
--




" Anders Borum" < na@na.na>在消息中写道

新闻:O3 ************** @ TK2MSFTNGP09.phx.gbl ...

"Anders Borum" <na@na.na> wrote in message
news:O3**************@TK2MSFTNGP09.phx.gbl...
Hello Jon

(感谢您的快速回复)

由于代码干净(以及RegEx模式更改方面具有令人难以置信的灵活性),我正在考虑使用RegEx方法,相比更详细(和更难阅读)字符串操作。
Hello Jon

(and thanks for the quick reply)

I was thinking of the RegEx approach because of the clean code (and
incredible flexibility in terms of changes the RegEx pattern), compared to
the more verbose (and harder to read) string ops.



这个很容易。


私有静态布尔IsAlphaNumeric(string s)

{

foreach(char c in s)

{

if(!( (c> =''0''&& c< ='''9'')||(c> ='''''&& c< =''z'') ||(c> ='''''&&

c< =''Z'')))

{

返回false;

}

}

返回true;

}

比正则表达式快得多,当你设计一个

库并且你现在不知道时,这很重要将执行ach方法。


David


This one is easy.

private static bool IsAlphaNumeric(string s)
{
foreach (char c in s)
{
if (!((c >= ''0'' && c <= ''9'') || (c >= ''a'' && c <= ''z'') || (c >= ''A'' &&
c <= ''Z'')))
{
return false;
}
}
return true;
}

And much faster than a regex, which can be important when you are desiging a
library and you don''t now know often each method will be executed.

David


这篇关于验证“设置”的最佳方法财产..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆