建议使用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
Run Code Online (Sandbox Code Playgroud)
我厌倦了输入相同的IF语句来检查字符串是否有任何长度
然后不要. 如果 newEmployee属性不具有重要值(并且具有类似"newEmployee"的变量名称,则它们不应该),您可以只调用该函数,结果无论如何都是相同的.
如果它们可能已经具有您不想覆盖的重要值,则可以有多种选择.您可以隐藏简单辅助函数背后的逻辑,也可以使用反射来迭代属性.但对我来说,这主要是需要重写的东西,因为它最终会将html编码的数据存储在数据库中.那是错的.
ASP.Net标签和许多其他控件将对您为其分配的数据进行html编码.有一天,您可能希望将此数据用于您不想进行html编码的平台(例如,报表编写工具).保持数据纯净.在输出时处理演示级别的html编码.
无论哪种方式,您都应该使用显式类型(在本例中为String)声明变量,并且应该使用String.IsNullOrEmpty()函数而不是仅检查长度.