Dear Programmers

What are the possible alternative codes against following codes

If Len(PasswordTextBox.Text) > 0 And Len(PasswordTextBox.Text) > 0 Then
MsgBox("ok", MsgBoxStyle.Information)
else
 MsgBox("Enter User Name and Password", MsgBoxStyle.Information)
End if

I'm not quite sure what you're looking for but I'll give it a shot.

After updating some code from a co-worker, I've had to begin using defensive coding practices so this may be overkill, but it should cover all of the possibilities. My co-worker would set Textbox.Text = nothing when he wanted to clear it rather than using String.Empty. If the TextBox.Text is nothing then calling TextBox.Text.Length would result in an exception occuring, thus the first check.

If (UserNameTextBox.Text IsNot Nothing AndAlso UserNameTextBox.Text.Length > 0) And (PasswordTextBox.Text IsNot Nothing AndAlso PasswordTextBox.Text.Length > 0) Then
                MessageBox.Show("Ready to process username and password combination", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf (UserNameTextBox.Text IsNot Nothing AndAlso UserNameTextBox.Text.Length > 0) And Not (PasswordTextBox.Text IsNot Nothing AndAlso PasswordTextBox.Text.Length > 0) Then
                MessageBox.Show("Please enter a password", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf Not (UserNameTextBox.Text IsNot Nothing AndAlso UserNameTextBox.Text.Length > 0) And (PasswordTextBox.Text IsNot Nothing AndAlso PasswordTextBox.Text.Length > 0) Then
                MessageBox.Show("Please enter a username", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                MessageBox.Show("Invalid username and password combination")
            End If

Note: I haven't tested this, but it should work in theory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.