Download presentation
Presentation is loading. Please wait.
Published byLindsay Harrell Modified over 9 years ago
1
.NET Data types
2
Introduction ٭ A "data type" is a class that is primarily used just to hold data. ٭ This is different from most other classes since they're primarily intended to 'do something', like access a database or format a page for example.
3
Data Types byte1 byteRange 0 to 255 Unsigned byte sbyte1 byteRange -128 to 127 Signed byte Short (sho) 2 bytesRange -32768 to 32767 Signed short ushort2 bytesRange 0 to 65535 Unsigned short
4
int (int) 4 bytesRange -2,147,483,647 to 2,147,483,647 Signed integer uint4 bytesRange 0 to 4,294,967,295 Unsigned integer long (lng) 8 bytesGreater than ±900,000 trillion Signed long int ulong8 bytesGreater than 18 million trillion Unsigned long int More Integer Data Types
5
Other Data Types single (sng) 4 bytesRange A number 6 digits past the decimal Float number double (dbl) 8 bytes Range A number 14 digits past the decimal Double precision decimal8 bytes Range A number 28 zeros long Fixed precision string (str)N/ARange N/AUnicode char2 bytes Range 0x0000 to 0xFFFF Unicode character Bool (bln)True or FalseBoolean
6
Block-Level Scope ٭ VB.NET introduces variables that only exist within blocks of code Blocks are items such as For…Next, Do…Loop, and If Then…End If ٭ Variables are only visible within the block, but their lifetime is that of the whole procedure
7
Changes in Syntax ٭ Firstly the ‘Currency’ data type is no longer used in VB 6.0 ٭ Currency has been replaced with Decimal in VB.NET ٭ The Currency data type (64 bit) does not provide sufficient accuracy to avoid rounding errors, so Decimal (96 bit) was created as its own data type. ٭ Dim x As Currency is upgraded to: ٭ Dim x As Decimal
8
Long and Integer Data Types ٭ VB 6.0 - Long variables were stored as 32-bit numbers and Integer variables as 16-bit numbers ٭ VB.NET - Long variables are stored as 64-bit numbers, Integer variables are stored as 32-bit numbers, and Short variables are stored as 16-bit numbers.
9
No More Variant Data Type ٭ Variant data types are changed to Object due to keeping all the languages more similar. This is no longer the same as a pointer to an object. ٭ Dim x As Variant is upgraded to: ٭ Dim x As Object
10
Option Explicit ٭ In VB.BET the option is turned on by default for all new projects. ٭ When Option Explicit Off is used (not a good programming style), you can use any variable without first declaring it.
11
Option Strict On ٭ New Option in VB.NET ٭ When Option Strict is turned on, the compiler/editor does not allow implicit conversions from a wider data type to a narrower one, or between String and numeric data types ٭ CInt and CDec convert ٭ Limits erroneous numbers or run-time errors ٭ Place the line Option Strict On before the first line of code
12
Value vs. reference types ٭ Value types: ٭ variable represents a value ("bits") ٭ Reference types: ٭ variable represents a reference to a heap-based object ٭ actual data resides in the object int i; i = 10; 10 string s; s = "calico"; "calico"
13
How do you know which types are which? ٭ Memorization! ٭ Though it's pretty obvious based on past experience – primitive types like bool, int and double are values – remainder are reference types int i; string s; Customer c1, c2; i = 23; s = "a message"; c1 = null; c2 = new Customer(…);
14
Boxing and Unboxing ٭ When necessary, C# will auto-convert value object – value ==> object is called "boxing" – object ==> value is called "unboxing" int i, j; object obj; string s; i = 32; obj = i; // boxed copy! i = 19; j = (int) obj; // unboxed! s = j.ToString(); // boxed! s = 99.ToString(); // boxed!
15
User-defined reference types ٭ Classes! – for example, Customer class we worked with earlier… public class Customer { public string Name; // fields public int ID; public Customer(string name, int id) // constructor { this.Name = name; this.ID = id; } public override string ToString() // method { return "Customer: " + this.Name; } }
16
Working with reference types… ٭ Creating, assigning, and comparing: Customer c1, c2, c3; string s1, s2; c1 = new Customer("joe hummel", 36259); c2 = new Customer("marybeth lore", 55298); c3 = null; // c3 references no object c3 = c1; // c3 now references same obj as c1 if (c1 == null)... // do I ref an object? if (c1 == c2)... // compares references if (c1.Equals(c2))... // compares objects if (s1 == s2)... // exception: == overloaded to // compare string data
17
The End …… Thank You …..
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.