View Single Post
  #3 (permalink)  
Old December 16th, 2004, 08:45 AM
mmcdonal mmcdonal is offline
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts

VBScript is typeless, so type mismatch is always an issue.

If you need to define the variable before you pass it, or after, then format the variable like this:

stMyVariable = "My text or number value here"
WScript.Echo CStr(stMyVariable)

CStr converts the variable contents to a string. If the value is null, however, then you will get an error. Perhaps this:

If stMyVariable <> "" Then
   WScript.Echo CStr(stMyVariable)
Else stMyVariable = "Null"
   WScript.Echo CStr(stMyVariable)
End If

Likewise CInt converts to Integer, and CDate converts a valid date/time to Date, CBool converts to Boolean, CByte converts to Byte, CCur = Currency, CDbl = Double, CLng = Long, CSng = Single, Hex returns Hex value, Oct returns Octal value, and Chr converts ANSI to a character.

So with all these available types that VBScript has to guess at, it is sometimes best to force the type yourself to make sure there are no problems with type mismatch. I use these a lot in my code.

Reply With Quote