November 22, 2014
Hot Topics:

New JScript .NET Data Types

  • January 7, 2002
  • By Essam Ahmed
  • Send Email »
  • More Articles »

JScript has a limited set of data types: Number, String, Boolean, and Object. The JScript interpreter works behind the scenes to coerce, or convert, data stored in variables into one of its native types using a standard set of rules (see section nine of the ECMAScript Language Specification). For example, the listing below uses the typeof operator to demonstrate that one variable can manage several data types (you can view the sample here)

var a;
a = 5; // typeof(a) returns 'number'
a = "five"; // typeof(a) returns 'string'
a = false; // typeof(a) returns 'boolean'
a = new String("");// typeof(a) returns 'object'

The key benefit the JScript interpreter provides is that is makes code easier to write since it performs data type conversions for you. The key drawback is that the JScript interpreter can end up working against the developer in situations like the one I demonstrated last week (where the interpreter seems to unexpectedly change a data type from one type to another) - here's the code again:

a = 1;
b = "1";
a += b; // what is the value of a?

If you guessed that the value of a should be two, you're wrong. Based on the data type conversion rules in the ECMAScript standard, JScript evaluates both operands (a and b) and determines that one of them, b, is a String (based on the value that's assigned to b). Since there's a String involved in the operation (+ in this example), JScript converts a into a String and concatenates b to it. The result is that a is 11 - it's not new math, the result is the String "11".

JScript .NET addresses this type of problem in several ways:

  • JScript .NET introduces many new and useful data types
  • JScript .NET allows you to declare a variable and assign a type to it
  • JScript .NET does not perform type conversions for you when you work with variables you have previously declared
  • JScript .NET checks for type mismatch errors at compile time, making debugging easier
  • JScript .NET enforces data type constraints by throwing an exception at run time when you attempt to assign inappropriate values to certain data types

This article describes each feature and demonstrates how you can create your own data types.

Understanding JScript .NET's new data types

A key element of the .NET Framework is the Common Type System, a component of the Common Language Runtime (see Figure 1).



Click here for larger image

Figure 1 - How the Common Type System relates to the .NET Framework

The Common Type System is a set of data types that are available to all .NET programming languages, making it easy to share data across applications written in different programming languages. JScript .NET makes it easy to use Common Type System data types by transparently mapping its native data types to Common Type System data types. Table 1 lists the new JScript .NET data types, what Common Type System Type each maps to, and a brief description of each data type.

Table 1 - JScript .NET data types

Data Type CTS Type Description/Range
Boolean System.Boolean Boolean values - true and false.
byte System.Byte 0 to +255
char System.Char 0 to +65 535
decimal System.Decimal -79.2e27 to +79.2e27
double System.Double -1.7e308 to +1.7e308
float System.Single -3.4e38 to +3.4e38
int System.Int32 -2.147e9 to +2.147e9
long System.Int64 -9.223e18 to +9.223e18
sbyte System.Sbyte -128 to +127
short System.Int16 -32 768 to +32 767
String System.String String data type; represents an immutable String - use the .NET StringBuilder object for mutable strings
uint System.UInt32 Unsigned integer ranging from 0 to +4e9
ulong System.Uint64 Unsigned long integer ranging from 0 to +18e18
ushort System.UInt16 Unsigned short integer ranging from 0 to +65 535

The next section describes how to declare variables and use some of the new data types.

Declaring variables along with their data types

JScript .NET uses the familiar var statement to allow developers to declare variables before using them. Developers already familiar with JScript or JavaScript should find it easy to declare variables. Unlike the simple var statement that JScript and JavaScript support, JScript .NET allows you to define the variable's data type as shown in the following listing:

var a : int; // declare an integer variable...
var b : String; // ...a string varible
var c : ushort; // ...and an unsigned short integer

Declare a variable using the var statement, followed by a colon (:), which is followed by the data type of variable you want to declare, as shown above.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel