VB tracking empty strings

tops

Member
Sep 13, 2001
90
0
0
Is there an inbuilt function in VB to track an empty string? something that does the equivalent of...

dim str as string

If trim(str) = "" Then
MsgBox("String is Empty!")
End If
 

UThomas

Senior member
Apr 18, 2000
251
0
0
Not that I know of. But you're code is very concise so I don't think its an issue.

Thomas
 

Theslowone

Golden Member
Jul 30, 2000
1,779
0
0
Why are you declaring it as a string?

If you are taking it as a user input then wouldn't it already be a string?

coudn't you do

If Trim(txtName.Text) = "" Then
MsgBox ("Need a Name")
txtName.SetFocus
End If
 

Boogak

Diamond Member
Feb 2, 2000
3,302
0
0
I don't think there's one either. You can always just convert that piece of code to a function to make it a bit more streamlined in your main routine.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Do you mean empty, or not initialized. Empty means no characters are present in the string. Not initialized would mean it's not yet a valid reference. Here's a sample...

Dim sFoo As Variant
MsgBox IsEmpty(sFoo) ' returns true

Now, if we did...

Dim sFoo as String
MsgBox IsEmpty(sFoo) ' returns false

Because IsEmpty() is only meaningful for variants. Now, to answer your question, there is a symbolic constant for an "empty string." I use it in place of "" simply because I find it more readable. e.g...

Dim sFoo As String
MsgBox (sFoo = "") ' returns true
MsgBox (sFoo = vbNullString) ' returns true

As you can see, vbNullString can be used in place of "". I find it more readable, maybe you will too...
 

tops

Member
Sep 13, 2001
90
0
0
thank u guys...
got what i wanted.
still havent understood what IsEmpty does though. What does it mean when something is not a 'valid reference'?
yes vbnullstring is much more readable... thanx for that.
 

BuckleDownBen

Banned
Jun 11, 2001
519
0
0
if you want to nitpick then:

dim s as string

s = ""

if len(s) = 0 then
dosomething
end if

is the best way to go, because string comparison is slower than numeric comparison
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0


<< if you want to nitpick then:

dim s as string

s = ""

if len(s) = 0 then
dosomething
end if

is the best way to go, because string comparison is slower than numeric comparison
>>



I'm going to have to say, no. String comparison is indeed slower than a numeric comparison, but what you've shown isn't just a numeric comparison. The line...

If Len(s) = 0

Would be much slower than....

If somestring = vbNullString

In the former, you're calling the routine Len(), which has to commence a "procedure prolog", which creates a stack for the called routine. This isn't very computationally expensive, but it's more expensive than doing a simple string comparison. Lets look...

Dim sFoo As String
s = "foo"

Dim sNullString As String
sNullString = vbNullString

If sFoo = sNullString Then

Ok, now what happens? In Visual Basic, a string's length is not determined in the same way a "string" (character array) is in say, C or C++. Visual Basic uses a BSTR (basic string) for it's string data type. The length of the string is at a 4-byte positive offset from the string's address. So, the entire string doesn't have to be compared. If one character is different (or the length is different), they're not equal.

Anyway, the point is, in programming, always (at least initially) write for readability. You may have a tiny possibility of shaving off a nanosecond in the execution time, but it's not worth the hassle as the compiler will optimize the code much better than you can. Write for readability first, speed second.
 

MGMorden

Diamond Member
Jul 4, 2000
3,348
0
76


<< Write for readability first, speed second. >>



Good advice. This is what people mean when they talk about "elegant code".