Search code examples
visual-studiovbscriptasp-classicinitializationvariable-types

Dim statement without type in classic vbscript / classic asp


In legacy code, I have found a "Dim" statement without type in classic vbscript / classic asp

Dim x1

Is there a default type (such as object) that x1 acquires?

Is it true that vb might 'work backwords' and if later it encounters x1 =3 or x1 = 'Name'

it will assign the relevant type (integer and string) respectively?

NOTE: The code involved really does work in the real world. Therefore, this isn't a bug, just an unfortunate (IMHO) 'feature' of classic VB ...

Thanks


Solution

  • there are no data types in classic asp, every variable is of a variant type. in fact, you explicitly cannot declare a data type when DIMing a variable, it's incorrect syntax.

    However, once a variable contains data, you can then force the data type you want to use by using specific functions such as:

    CInt( x ) convert to a integer

    CDbl( x ) convert to a decimal

    CStr( x ) convert to a string

    CBool( x ) convert to a boolean

    you can see how this is useful:

    Response.Write ( CInt( "4" ) = 4 )       true
    Response.Write ( CInt( "4" ) = "4" )     false