0

My expertise is not VB, but I am having to work on a big chunk of VB at the moment.

Just about every block of code that looks at the input data uses this format:

Existing:

If textbox1.Text <> vbNullString AndAlso textbox1.Text.Trim <> vbNullString Then
  textbox1.Focus()
End If

That code just irritates me.

The way I would typically write a piece of code like this would be:

ReWrite:

Dim text as String = textbox1.Text.Trim()
If String.IsNullOrEmpty(text) Then
  textbox1.Focus()
End If

That is coming from my C# experience, where a TextBox Control's Text property is never NULL - even if I set it to NULL, it still reads back as an empty string.

Sure, I could test this, but I'm likely to not encounter some nuance of VB in my quick verification code.

Is my ReWrite still valid in this context?

Yes, I am using .NET 4, so I could (should?) replace String.IsNullOrEmpty with String.IsNullOrWhiteSpace - unless someone points out some reason not to.

Community
  • 1
  • 1
  • 2
    VB and C# use the same WIndows controls. `vbNullString` is the same as `String.Empty` or `""` – Ňɏssa Pøngjǣrdenlarp Feb 05 '15 at 19:36
  • My take on it is that the existing code was written by someone who is more procedural oriented and your code is more object oriented. From my experience with VB they'll accomplish the same task. As far as IsNullOrEmpty vs. IsNullOrWhiteSpace, there could be times when you want white space, but if you're already calling trim() then the check for whitespace is superfluous but harmless. – Duston Feb 05 '15 at 19:39

1 Answers1

2

vb.net handles the text property the same way as c# do.

I believe the real question is:

"How is the text property handled by the .net framework?"

If you look at the reference source you'll see that a null value will be replaced with an empty string.

So a .net developer (vb.net or c#) will use the IsNullOrEmpty method.

Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
  • 1
    Thanks for the Reference Source. That is what I needed to be sure there wasn't some unknown caveat of VB. –  Feb 05 '15 at 19:55