VarType: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
JwellsNB (talk | contribs)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
''This function is for BASIC compatibility. It is not available in pure JavaScript projects.''
VarType(''variable'')
VarType(''variable'')


Line 56: Line 58:
nSingle = CSNG(99.44)
nSingle = CSNG(99.44)
Print 99.44 & "isVarType" & VarType(nSingle)
Print 99.44 & "isVarType" & VarType(nSingle)
</pre>
== Example (JavsScript) ==
<pre>
// VarType Example
/* VarType returns variable type as an integer */
function VarType(o) {
  var varTypes= {
    'undefined'        : 0,
    '[object null]'    : 1,
    'number'          : 2,
    '[object Date]'    : 7,
    'string'          : 8,
    '[object Error]'  : 10,
    'boolean'          : 11,
    '[object Array]'  : 8192
  },
  TOSTRING = Object.prototype.toString;
  if (varTypes[typeof o] || varTypes[TOSTRING.call(o)] || (o ? 'object' : 'null')==2) {
    if (o.toString().indexOf('.')>-1) { return 5; } //JavaScript only deals with doubles, not singles
  }
  if (o==null) { return 1; }
  return varTypes[typeof o] || varTypes[TOSTRING.call(o)] || (o ? 'object' : 'null');
}
var nInteger, nSingle;
nInteger = parseInt(44);
NSB.Print nInteger.toString() + " is VarType " + VarType(nInteger);
nSingle = parseFloat(99.44);
NSB.Print nSingle.toString() + " is VarType " + VarType(nSingle);
</pre>
</pre>


Line 104: Line 74:


[[Category:Variable Handling]]
[[Category:Variable Handling]]
[[Category:BASIC Functions]]

Latest revision as of 15:39, 25 March 2019

This function is for BASIC compatibility. It is not available in pure JavaScript projects.

VarType(variable)

Description

VarType returns an integer that indicates the type of a variable. The required parameter, variable, is any variable that doesn't contain a user-defined type.

When variable is an array, the value returned is equal to the array constant plus the constant that specifies the element-type.

Table 25: VarType return values

Constant Value Description
vbEmpty 0 Empty
vbNull 1 Null
vbInteger 2 Integer
vbLong 3 Long
vbSingle 4 Single-precision floating-point
vbDouble 5 Double-precision floating-point
vbCurrency 6 Currency
vbDate 7 Date
vbString 8 String
vbObject 9 Object
vbError 10 Error
vbBoolean 11 Boolean
vbVariant 12 Variant
vbDataObject 13 Data-access Object
vbByte 17 Byte
vbArray 8192 Array

Example (Basic)

Rem VarType Example
'VarType returns variable type as an integer
Dim nInteger, nSingle
nInteger = CInt(44)
Print 44 & " is VarType " & VarType(nInteger)
nSingle = CSNG(99.44)
Print 99.44 & "isVarType" & VarType(nSingle)

Output

44 is VarType 2
99.44 is VarType 4

Related Items

Is, TypeName