Suggestion in C# or VB.NET are welcome
I have the following code block. I'm tired of typing the same IF statement to check if the string has any lenght before assignning them to the class properties of Employee.
Is there any better to accomplish this?
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