下面的方法怎么改写,使得更加简洁容易理解

caicoko 2011-01-10 03:43:49
下面是web控件的部分重写代码,有些乱,用了很多if语句,如何改写使得代码更加简洁,更加容易理解。


protected override void OnPreRender(EventArgs e)
{
if (this._controlType == "DateTime" && (BB.Get("action") == "insert" || BB.Get("action") == "copy"))
{
this.Text = DateTime.Now.ToString();
}
if (this._controlType == "UpdateDate" && BB.Get("action") == "update")
{
this.Text = DateTime.Now.ToString();
}
if (this._controlType == "InputUser" && BB.Get("action") == "insert")
{
this.Text = BB.LoginKey("Name") != null ? BB.LoginKey("Name") : String.Empty;
}
if (this._controlType == "InputUserName" && BB.Get("action") == "insert")
{
this.Text = BB.LoginKey("Name") != null ? BB.LoginKey("Name") : String.Empty;
}
if (this._controlType == "InputUserID" && (BB.Get("action") == "insert" || BB.Get("action") == "copy"))
{
this.Text = BB.LoginKey("UserID") != null ? BB.LoginKey("UserID") : String.Empty;
}
if (this._controlType == "Password")
{
this.TextMode = TextBoxMode.Password;
}
if (this._controlType == "MultiLine")
{
this.TextMode = TextBoxMode.MultiLine;
}
base.OnPreRender(e);
}
...全文
106 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lee576 2011-05-12
  • 打赏
  • 举报
回复
设计模式之State(状态模式)

http://www.cnblogs.com/springyangwc/archive/2011/04/11/2013073.html
caicoko 2011-01-11
  • 打赏
  • 举报
回复
比如换个什么思想~
LQ_651119244 2011-01-10
  • 打赏
  • 举报
回复
唯有换个思想。。。
BATTLERxANGE 2011-01-10
  • 打赏
  • 举报
回复
感觉用switch更难看
ReyZhang 2011-01-10
  • 打赏
  • 举报
回复
首先,上面的代码是必定要被重构的。如果一个条件进来,现在的每一个if 语句都会被执行,这不是我们想要看到的。
可以用if ..else if 来判断,或用switch
刀枪blue 2011-01-10
  • 打赏
  • 举报
回复
还是if吧。switch没什么简洁
lilianjie0426 2011-01-10
  • 打赏
  • 举报
回复
楼主这个可变量太多了,用什么都很复杂的。。。。如果非要简单的话,还是看看有没有别的思路吧
laowang134 2011-01-10
  • 打赏
  • 举报
回复
我咋感觉用switch会更复杂。。。
baysos 2011-01-10
  • 打赏
  • 举报
回复
同意switch
fox_sky 2011-01-10
  • 打赏
  • 举报
回复
protected override void OnPreRender(EventArgs e)
{
if ((this._controlType == "DateTime" && (BB.Get("action") == "insert" || BB.Get("action") == "copy"))||
(this._controlType == "UpdateDate" && BB.Get("action") == "update"))
{
this.Text = DateTime.Now.ToString();
}

if (((this._controlType == "InputUser"||this._controlType == "InputUserName") && BB.Get("action") == "insert"))
{
this.Text = BB.LoginKey("Name") != null ? BB.LoginKey("Name") : String.Empty;
}

if (this._controlType == "InputUserID" && (BB.Get("action") == "insert" || BB.Get("action") == "copy"))
{
this.Text = BB.LoginKey("UserID") != null ? BB.LoginKey("UserID") : String.Empty;
}
if ((this._controlType == "Password")||(this._controlType == "MultiLine"))
{
this.TextMode = TextBoxMode.Password;
}

base.OnPreRender(e);
}
Fibona 2011-01-10
  • 打赏
  • 举报
回复
用switch好点

switch(this._controlType)
case ""
TimZhuFaith 2011-01-10
  • 打赏
  • 举报
回复
这么多if都要判断的。。。为什么不用elseif switch
xjfhnsd 2011-01-10
  • 打赏
  • 举报
回复
switch(this._controlType)吧,不过还是一样,只是好看点

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧