很多IF或elses [英] lots of IF or elses

查看:79
本文介绍了很多IF或elses的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它有10个属性,每个属性需要根据5种不同的验证规则进行验证。我最终写了很多

IF和elses声明使我的代码变得如此丑陋。有没有更好的方式

而不是顺序IF和elses?


非常感谢

解决方案

在您描述的情况下,可能不是。它可能很难看,但如果它有效,我就不用担心它了!如果你使用了一个好的if - elseif等逻辑

模式。


彼得

-

联合创始人,Eggheadcafe.com开发者门户网站:
http:// www .eggheadcafe.com

UnBlog:
http: //petesbloggerama.blogspot.com


" J-T"写道:

我有一个类,它有10个属性,每个属性需要根据5种不同的验证规则进行验证。我最终写了很多

非常感谢


switch语句可能非常适合这项任务。


<%= Clinton Gallagher

METROmilwaukee(sm)A区域信息服务

NET csgallagher AT metromilwaukee.com

URL http://metromilwaukee.com/

URL http://clintongallagher.metromilwaukee.com/

" J-T" <的J - * @ nospam.com>在消息中写道

news:%2 ****************** @ TK2MSFTNGP15.phx.gbl ...

我有一个类,它有10个属性,每个属性需要根据5种不同的验证规则进行验证。我最终写了很多
IF和elses语句,这使我的代码变得如此丑陋。是否有更好的方式来代替顺序IF和elses?

非常感谢



你可以写一个函数来做所有的验证。

你可以学习这门语言的最好方法是阅读一些关于它的书。


干杯,
Mark


" J-T"写道:

我有一个类,它有10个属性,每个属性需要根据5种不同的验证规则进行验证。我最终写了很多

非常感谢


I have a class which it has 10 properties, and each property needs to be
validated against 5 different validation rules.I ended up writing a lot of
IF and elses statement which has made my code so ugly.Is there a better way
to do instead of sequential IF and elses?

Thanks a lot

解决方案

In the case you describe, probably not. It may be ugly, but if it works, I
wouldn''t worry about it! Provided you''ve used a good if - elseif etc. logic
pattern.

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"J-T" wrote:

I have a class which it has 10 properties, and each property needs to be
validated against 5 different validation rules.I ended up writing a lot of
IF and elses statement which has made my code so ugly.Is there a better way
to do instead of sequential IF and elses?

Thanks a lot



The switch statement may be perfect for this task.

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
"J-T" <J-*@nospam.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...

I have a class which it has 10 properties, and each property needs to be
validated against 5 different validation rules.I ended up writing a lot of
IF and elses statement which has made my code so ugly.Is there a better way
to do instead of sequential IF and elses?

Thanks a lot



You can write a function to do all the validations.
Best way you can learn the language is to read a few books about it.

Cheers,
Mark

"J-T" wrote:

I have a class which it has 10 properties, and each property needs to be
validated against 5 different validation rules.I ended up writing a lot of
IF and elses statement which has made my code so ugly.Is there a better way
to do instead of sequential IF and elses?

Thanks a lot



这篇关于很多IF或elses的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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