0
votes

Why does this string need to be declared as an object when comparing to Nothing?

Dim BlankStr
BlankStr = "blank"
If BlankStr Is Nothing Then
End If

I also cannot compare If 1 Is 1 Then. Why can't I compare primitives?

3
are you asking why a variable has to be declared at all? - Daniel A. White
I'm asking why BlankStr can't be compared to Nothing in this instance. - Andrew

3 Answers

3
votes

VBScript has data (sub) types. Besides simple (sub) types like Strings:

>> x = "blank"
>> WScript.Echo VarType(x), TypeName(x)
>>
8 String

there are Objects:

>> Set y = New RegExp
>> WScript.Echo VarType(y), TypeName(y)
>>
9 IRegExp2

To assign an object to a variable, you need Set, to compare objects, you need Is. Simple (non-object) values have their own comparison operator.

To compare a string against another:

>> WScript.Echo CStr(x = "blank"), CStr(x = "object")
>>
True False

Trying to use a simple value 'as if it were an object', throws an "object required" error:

>> Set z = "blank"
>>
Error Number:       424
Error Description:  Object required
>> WScript.Echo CStr(x Is x)
>>
Error Number:       424
Error Description:  Object required

Read A Whole Lot Of Nothing.

1
votes

String is not an object in VBScript.

So if you compare any primitive data types, you just use = to compare if they are equal. If does not require objects.

http://www.w3schools.com/asp/vbscript_ref_functions.asp

0
votes

According a comment to this answer, VBScript doesn't have a string type. Instead it sounds like strings are primitives. Is works with object references.