IIF and isNothing

I have 3 textboxes that i want to concatenate. fname, mname, lname

For example:

fname = Nat    ----     mname = Arnido    ----   lname = Congreso  

then i want them to concatenate in other textbox called fullname using a command button.

fullname = Congreso, Nat A.

But in the other case where middle name is null the fullname should look like this.

fullname = Congreso, Nat

and i find it difficult to remove the period (.) in the middle name.

This is my code so far.

Me.fullname.Text = Me.lname.Text & ", " & Me.fname.Text & " " & IIf(IsNothing(Microsoft.VisualBasic.Left(Me.mname.Text, 1) & ""), "", (Microsoft.VisualBasic.Left(Me.mname.Text, 1)) & ". ")

Upvotes: 0

Views: 534

Answers (3)

Nico Schertler
Nico Schertler

Reputation: 32597

Textbox data is usually not Nothing. It is at most an empty string:

Me.fullname.Text = Me.lname.Text & ", " & _
                   Me.fname.Text &  _
                   If(Me.mname.Text = String.Empty, _
                       "", _
                       " " & Left(Me.mname.Text, 1) & ".")

Upvotes: 0

dbasnett
dbasnett

Reputation: 11773

I wouldn't use IIf or Left.

    fullname.Text = lname.Text.Trim & ", " & fname.Text.Trim
    If Not String.IsNullOrWhiteSpace(mname.Text) Then
        fullname.Text &= " " & mname.Text.Trim.Substring(0, 1) & "."
    End If

If I were enamored of writing this as one long statement I would use the If operator.

    fullname.Text = lname.Text.Trim & ", " & fname.Text.Trim & _
        If(Not String.IsNullOrWhiteSpace(mname.Text.Trim), " " & mname.Text.Substring(0, 1) & ".", "")

Upvotes: 2

Anthony Sherratt
Anthony Sherratt

Reputation: 475

You could also use the String Replace method to remove/change any unwanted characters/patterns.

mname.Replace(".", "")

Upvotes: 0

Related Questions