developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




Vote for the Developer.com Product of the Year Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
The Key to Rapid App Dev? Process.    December 1, 2008
PartyGaming Bets on Open Source    December 1, 2008
Google's Underdog Eyes iPhone's Lead in Apps    November 28, 2008
A Networking Idea That's Out of This World    November 26, 2008
Free Tech Newsletter -

New JScript .NET Data Types
By Essam Ahmed

Go to page: 1  2  3  Next  

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.

Go to page: 1  2  3  Next  

Previous article: What's New in JScript .NET?
Next article: Understanding New JScript .NET Statements


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Languages & Tools Archives






internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES