避免多个IF语句

时间:2010-09-14 03:29:16

标签: c# vb.net

  

欢迎使用C#或VB.NET建议

我有以下代码块。在将它们分配给Employee的类属性之前,我已经厌倦了键入相同的IF语句来检查字符串是否有任何长度。

有没有比这更好的了?

        Dim title = txtTitle.Text.Trim
        Dim firstName = txtFirstName.Text.Trim
        Dim lastName = txtLastName.Text.Trim
        Dim middleName = txtMiddleName.Text.Trim
        Dim nativeName = txtNativeName.Text.Trim
        Dim mobile = txtMobile.Text.Trim
        Dim workEmail = txtWorkEmail.Text.Trim
        Dim peronsalEmail = txtPersonalEmail.Text.Trim
        Dim otherContact = txtOtherContact.Text.Trim
        Dim home = txtHome.Text.Trim
        Dim homeDir = txtHomeDir.Text.Trim
        Dim homeExt = txtHomeExt.Text.Trim
        Dim personalAddress = txtAddress.Text.Trim
        Dim office = txtOffice.Text.Trim
        Dim officeDir = txtOfficeDir.Text.Trim
        Dim officeExt = txtOfficeExt.Text.Trim

                   If title IsNot Nothing AndAlso title.Length > 0 Then
            newEmployee.Title = HttpUtility.HtmlEncode(title)
        End If

        If firstName IsNot Nothing AndAlso firstName.Length > 0 Then
            newEmployee.FirstName = HttpUtility.HtmlEncode(firstName)
        End If

        If lastName IsNot Nothing AndAlso lastName.Length > 0 Then
            newEmployee.LastName = HttpUtility.HtmlEncode(lastName)
        End If

        If middleName IsNot Nothing AndAlso middleName.Length > 0 Then
            newEmployee.MiddleName = HttpUtility.HtmlEncode(middleName)
        Else
            newEmployee.MiddleName = Nothing
        End If

        If nativeName IsNot Nothing AndAlso nativeName.Length > 0 Then
            newEmployee.NativeName = HttpUtility.HtmlEncode(nativeName)
        Else
            newEmployee.NativeName = Nothing
        End If

        If mobile IsNot Nothing AndAlso mobile.Length > 0 Then
            newEmployee.MobilePhone = HttpUtility.HtmlEncode(mobile)
        Else
            newEmployee.MobilePhone = Nothing
        End If

        If workEmail IsNot Nothing AndAlso workEmail.Length > 0 Then
            newEmployee.Email = HttpUtility.HtmlEncode(workEmail)
        Else
            newEmployee.Email = Nothing
        End If

        If peronsalEmail IsNot Nothing AndAlso peronsalEmail.Length > 0 Then
            newEmployee.PersonalEmail = HttpUtility.HtmlEncode(peronsalEmail)
        Else
            newEmployee.PersonalEmail = Nothing

        End If

        If otherContact IsNot Nothing AndAlso otherContact.Length > 0 Then
            newEmployee.OtherContact = HttpUtility.HtmlEncode(otherContact)
        Else
            newEmployee.OtherContact = Nothing
        End If

        If home IsNot Nothing AndAlso home.Length > 0 Then
            newEmployee.Home = HttpUtility.HtmlEncode(home)
        Else
            newEmployee.Home = Nothing
        End If

        If homeDir IsNot Nothing AndAlso homeDir.Length > 0 Then
            newEmployee.HomeDir = HttpUtility.HtmlEncode(homeDir)
        Else
            newEmployee.HomeDir = Nothing

        End If

        If homeExt IsNot Nothing AndAlso homeExt.Length > 0 Then
            newEmployee.HomeExtension = HttpUtility.HtmlEncode(homeExt)
        Else
            newEmployee.HomeExtension = Nothing
        End If

        If office IsNot Nothing AndAlso office.Length > 0 Then
            newEmployee.Office = HttpUtility.HtmlEncode(office)
        Else
            newEmployee.Office = Nothing
        End If

        If officeDir IsNot Nothing AndAlso officeDir.Length > 0 Then
            newEmployee.OfficeDir = HttpUtility.HtmlEncode(officeDir)
        Else
            newEmployee.OfficeDir = Nothing
        End If

        If officeExt IsNot Nothing AndAlso officeExt.Length > 0 Then
            newEmployee.OfficeExtension = HttpUtility.HtmlEncode(officeExt)
        Else
            newEmployee.OfficeExtension = Nothing
        End If

        If personalAddress IsNot Nothing AndAlso personalAddress.Length > 0 Then
            newEmployee.Address = HttpUtility.HtmlEncode(personalAddress)
        Else
            newEmployee.Address = Nothing
        End If

6 个答案:

答案 0 :(得分:6)

  

我厌倦了输入相同的IF语句来检查字符串是否有任何长度

然后不要。 IF newEmployee属性不具有重要值(并且具有变量名称,如“newEmployee”,它们不应该),您可以只调用该函数,结果无论如何都是相同的。

如果它们可能已经具有重要值而您不想覆盖,则可以有多种选择。您可以隐藏简单辅助函数背后的逻辑,也可以使用反射来迭代属性。但对我来说,这主要是需要重写的东西,因为它最终会将html编码的数据存储在数据库中。那是错的。

ASP.Net标签和许多其他控件将对您为其分配的数据进行html编码。有一天,您可能希望将此数据用于您不想进行html编码的平台(例如,报表编写工具)。保持数据纯净。在输出时处理演示级别的html编码。

无论哪种方式,你应该使用显式类型(在本例中为String)声明变量,并且应该使用String.IsNullOrEmpty()函数而不仅仅是检查长度。

答案 1 :(得分:2)

这里有一些不好的事情,比如没有将变量声明为string,但我会回答你的问题:

private sub mycopy(value as string, ref target as string)
    value = value.trim
    if value.length > 0 then
        target = HttpUtility.HtmlEncode(value)
    end if 
end sub

mycopy(txtTitle.Text, ref title)
mycopy(txtFirstName.Text, ref firstName)

即使title存在您不想更改的现有值,这也是安全的 - 我认为这是您想要的,否则为什么还要检查?

你可以走得更远:

private sub mycopy(ctrl as TextBox, ref target as string)
    dim value as string = ctrl.Text.trim ' note declaration as string
    if value.length > 0 then
        target = HttpUtility.HtmlEncode(value)
    end if 
end sub

' use it this way:
dim title as string
dim firstName as string

mycopy(txtTitle, ref title)
mycopy(txtFirstName, ref firstName)

答案 2 :(得分:0)

你可以使用一个函数..

newEmployee.title = ifNotNull(title);
....
....
newEmployee.Office = ifNotNull(office);

public string ifNotNull(string)
{
if(string.Length >0)
return HttpUtility.HtmlEncode(title);
else 
return "";
}

答案 3 :(得分:0)

一种可能性是使用辅助函数,伪代码:

def setField (oldField, possibleVal):
    if possibleVal.Length > 0:
        return HttpUtility.HtmlEncode (possibleVal)
    return oldField

newEmployee.Title = setField (newEmployee.Title, title)
newEmployee.FirstName= setField (newEmployee.FirstName, firstName)
:
... and so on.

答案 4 :(得分:0)

循环遍历表单上的所有控件,确定每个控件是否为文本框并检查length是否为空。您可以使用String.IsNullOrEmpty()函数。这样,您可能需要使用最多1或2个if语句。

答案 5 :(得分:0)

编写函数(或vb中的sub)仍然需要输入大量函数调用。

尝试类似下面的内容(代码未经测试,仅限伪编码):

foreach (Control c in form/containerObject.Controls) {
    if ("TextBox" == c.GetType()) {
        TextBox tb = c as TextBox;
        if (tb.Length > 0) {
            newEmployee/targetObject.Title = HttpUtility.HtmlEncode(tb.Text);
        }
    }
}

或类似的东西:)

祝你好运。