Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1069115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:22:17+00:00 2026-05-16T20:22:17+00:00

Suggestion in C# or VB.NET are welcome I have the following code block. I’m

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:22:18+00:00Added an answer on May 16, 2026 at 8:22 pm

    There are some bad things here, like not declaring your variables as string, but I’ll answer your question:

    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)
    

    This is safe even when there is an existing value for title that you don’t want changed –which I assume is what you want, otherwise why are you bothering to check?

    You can go even further:

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

C# or VB.NET suggestion are welcome. I have the following code to create Excel
The question, I have a web application - .net 4. The client is having
Windows 7, Visual Studio 2010, tagert framework = .NET 4 Client Profile I'm creating
I'm working on a webapplication which runs central at a company. This webapplication needs
I am writing a glassing program, similar to Glass2k (see image below) as I
I'm trying to design a tab control which will fade out the current content
As the title states, I can connect to another machine from my server via
I’ve been tasked with implementing a system for continuously receiving large amounts of data
I'm currently writing a PyGTK application and I'd like some advice as to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.