本文介绍了优化asp.net VB code背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有低于code ..
我正在寻找一种方法来优化它。

反正是有使用的FindControl和循环它来优化?也消除headofficeSelected,areaSelected,headofficeSelected只有使用storeSelected?

 如果(storeSelected = 1),然后
    tempCollector = tempCollector +< BR>中+部+:+ txt_panview3_ddinput1.SelectedItem.ToString
    tempCollector = tempCollector +< BR>中+商店+:+ txt_panview3_input2.Text +&所述峰; br>中
万一
如果(areaSelected = 1)。然后
    tempCollector = tempCollector +< BR>中+区域名称+:+ txt_panview0_ddinput1.SelectedItem.ToString
万一
如果(headofficeSelected = 1)。然后
    tempCollector = tempCollector +< BR>中+部+:+ txt_panview1_ddinput1.SelectedItem.ToString
万一
如果(regionSelected = 1)。然后
    tempCollector = tempCollector +< BR>中+区域+:+ txt_panview2_ddinput1.SelectedItem.ToString
万一

如果storeSelected()= 0则
    tempCollector = tempCollector +< P>< B>中+ lbl_viewTitle2.Text +< / B>中
    tempCollector = tempCollector +< BR>中+ lbl_view2_ManagersEmailAddress.Text +:+ txt_view2_ManagersEmailAddress.Text
万一


解决方案

您应该探索使用StringBuilder的,以取代所有的字符串concantenation - 很可能,这将是更快

对于大多数code你在重复着同样的事情,所以你可能会考虑与您反复调用一个子程序替换重复code的。如果可能看起来像这样的事情[未经测试]:

 无效AddControlText(StringBuilder的建设者,字符串标签,字符串值)
{
builder.Append(< BR>中);
builder.Append(标签);
builder.Append(值);
}

编辑:

位的时间短所以这里是一个开始。你会用上面的方法,并调用它是这样的:

 的StringBuilder _Builder =新的StringBuilder();如果(storeSelected = 1)。然后
{
    AddControlText(_Builder,处,txt_panview3_ddinput1.SelectedItem.ToString);
    AddControlText(_Builder,存储,txt_panview3_input2.Text);
}

编辑(2):
Woops刚刚意识到你正在使用VB - 和上面的是C# - !希望你能回来转化为VB

I have the below code..i am finding a way to optimise it..

is there anyway to optimise it using findcontrol and loops ?? also eliminating headofficeSelected,areaSelected ,headofficeSelected and only using storeSelected ?

If (storeSelected = 1) Then
    tempCollector = tempCollector + "<br>" + "Department" + ": " + txt_panview3_ddinput1.SelectedItem.ToString
    tempCollector = tempCollector + "<br>" + "Store" + ": " + txt_panview3_input2.Text + "<br>"
End If
If (areaSelected = 1) Then
    tempCollector = tempCollector + "<br>" + "Area Name" + ": " + txt_panview0_ddinput1.SelectedItem.ToString
End If
If (headofficeSelected = 1) Then
    tempCollector = tempCollector + "<br>" + "Department " + ": " + txt_panview1_ddinput1.SelectedItem.ToString
End If
If (regionSelected = 1) Then
    tempCollector = tempCollector + "<br>" + "Region " + ": " + txt_panview2_ddinput1.SelectedItem.ToString
End If
'
If storeSelected() = 0 Then
    tempCollector = tempCollector + "<p><b>" + lbl_viewTitle2.Text + "</b>"
    tempCollector = tempCollector + "<br>" + lbl_view2_ManagersEmailAddress.Text + ": " + txt_view2_ManagersEmailAddress.Text
End If
解决方案

You should explore using StringBuilder to replace all that string concantenation - it is very likely that it would be faster.

For most of the code you are repeating the same thing so you might consider replacing the repeated code with a subroutine which you call repeatedly. If might look like something like this [untested]:

void AddControlText(StringBuilder builder, string label, string value)
{
builder.Append("<br>");
builder.Append(label);
builder.Append(value);
}

EDIT:

Bit short on time so here is a start. You would use the above method and call it like this:

StringBuilder _Builder = new StringBuilder();

If (storeSelected = 1) Then
{
    AddControlText(_Builder, "Department: ",txt_panview3_ddinput1.SelectedItem.ToString);
    AddControlText(_Builder, "Store: ", txt_panview3_input2.Text);
}

EDIT(2):Woops just realised you are using VB - and the above is C# - hopefully you can translate it back to VB!

这篇关于优化asp.net VB code背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:50