gpt4 book ai didi

c# - 避免多个 IF 语句

转载 作者:太空宇宙 更新时间:2023-11-03 18:17:43 25 4
gpt4 key购买 nike

Suggestion in C# or VB.NET are welcome

我有以下代码块。在将字符串分配给 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

最佳答案

I'm tired of typing the same IF statement to check if the string has any lenght

那就不要了。 如果 newEmployee 属性还没有一个重要的值(并且使用像“newEmployee”这样的变量名,它们不应该),您可以只调用该函数,无论如何结果都是一样的。

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

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

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

关于c# - 避免多个 IF 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3705922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com