WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

String Manipulation

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

IsString UDF

 Keywords: String Char StrTypeInfo VarType ObjectTypeGet ChrStringToUnicode ObjectType

Question:

How can I do an IsString(var) ?

Answer:

WinBatch variables are both weakly and dynamically typed. Some even call WinBatch variables negatively typed. So variable type does not have the same connotation as it does some other compiled and interpreted programming languages.

You can determine if a variables value is currently stored in string form by using the 'VarType' function on a variable.

if VarType(varname) == 2
   Pause("Notice", "Var is a string")
endif
If a variable's value is currently represented as a string the functions return bitmask will have the bits for decimal value 2 and/or 128 set. If the bitmask has the bit for decimal value 512 set, it is a variant. Variants can also hold strings so you would need to use the ObjectTypeGet function to determine if the variant variable holds a BSTR. BSTRs are Unicode strings with a stored character count.

The following takes advantage of the subroutine parameter copy mechanism to determine if a variable or manifest constant is a string

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Is String
;
; Deterimes if the input variable is a string.
; (Variable can have non-string types as well as
; a string type and still be considered a string.)
;
; anyVal - variable or manifest constant.
;
; Returns true if anyVal's internal type is
; set to a string type.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#DefineSubRoutine IsString( anyVal )

   nType = VarType(anyVal)
   Switch @TRUE
   Case (ntype & 130) != 0  ; 130 = 128 | 2
      bReturn = @TRUE ; ANSI or Unicode string
      Break
   Case (nType & 512) != 0
      If ObjectTypeGet(anyVal) == "BSTR"
         bReturn = @TRUE ; Variant BSTR string.
         Break
      EndIf
   Case @TRUE
      bReturn = @FALSE
      Break
   EndSwitch

   Return bReturn
#EndSubRoutine

; Test
strPlain = "Hello world"
strUni   = ChrStringToUnicode( strPlain)
strBs    = ObjectType( "BSTR", strPlain)
nX       = 1
strAnswers = ""
If IsString(strPlain) Then strAnswers = strAnswers:"Plain ":"Yes":@CRLF
Else strAnswers = strAnswers:"Plain ":"No":@CRLF
If IsString(strUni) Then strAnswers = strAnswers:"Unicode ":"Yes":@CRLF
Else strAnswers = strAnswers:"Unicode ":"No":@CRLF
If IsString(strBs) Then strAnswers = strAnswers:"BSTR ":"Yes":@CRLF
Else strAnswers = strAnswers:"BSTR ":"No":@CRLF
If IsString(nX) Then strAnswers = strAnswers:"Int ":"Yes":@CRLF
Else strAnswers = strAnswers:"Int ":"No":@CRLF

Message("IsString", strAnswers)

Article ID:   W18390
Filename:   IsString.txt
File Created: 2009:03:05:11:04:58
Last Updated: 2009:03:05:11:04:58