Download presentation
Presentation is loading. Please wait.
Published byAudrey Barrett Modified over 9 years ago
1
Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com
2
Pre-Lecture Topics Web site resources Lecture & Materials http://microsoft.mike95.com MSDN AA program http://www.cs.fiu.edu/MSDNAA/
3
Topics Covered Today Using Data Types Using Constants, Enums, Arrays, and Collections Implementing Properties Implementing Delegates & Events Mobile Application Development Give Away
4
Using Data Types After this lesson, you will be able to : Describe the data types provided by the.NET Framework Explain how to perform implicit and explicit conversions between types Describe the functionality provided by types of the.NET Framework Describe string manipulation functions with the String class methods
5
Data Types All.NET languages are strongly typed Built in Value Types Integer Types Floating-point Types Boolean Type Char Type Built in Reference Types String Object
6
Data Types Mappings from.NET to C# & VB.NET TYPEC#VB.NETRANGE System.BytebyteByte 0 – 255 System.Int16shortShort -32768 – 32767 System.Int32intInteger -2 31 to 2 31 -1 System.Int64longLong -2 63 to 2 63 -1 System.SBytesbyte(NA) -128 to 127 System.UInt16ushort(NA) 0 to 65535 System.UInt32uint(NA) 0 to 2 32 -1 System.UInt64ulong(NA) 0 to 2 64 -1
7
System.Object The Object type is the supertype of all types in the.NET Framework. Every type, whether value type or reference type, derives from System.Object.
8
Converting Types Implicit Conversions are performed whenever the conversion can occur without the loss (ex. long t = intValue; ) Explicit Conversion Necessary when conversion where types cannot be implicitly converted Cast similar to Java & C++ (ex. int t = (int)longValue; ) System.Convert class
9
Common Type Methods Equals Determines whether two instances are equal GetHashCode Serves as a hash function for a particular type GetType Returns the type object for the current instance ToString Returns a human-readable form of the object
10
ValueType.Parse() All built in value types have a Parse() method. The Parse method can be used to convert a string to the type desired. Ex: int Age = Int32.Parse( tbxAge.Text ); int Age = int.Parse( tbxAge.Text );
11
Common String methods String.Replace() Replaces all occurrences of a specified character in the string with another specified character String.Split() Returns an array of substrings that are delimited by a specified character String.Substring() Returns a substring from the specified instance String.ToUpper(), ToLower() Returns the string converted to all lowercase or all uppercase, respectively
12
Demo System.Convert functions Int.Parse() Recommendation on StringBuilder
13
Topics Covered Today Using Data Types Using Data Types Using Constants, Enums, Arrays, and Collections Implementing Properties Implementing Delegates & Events Mobile Application Development Give Away
14
Const, Enum, Array, Collections After this lesson, you will be able to : Create constants and enumerations, and use them in methods Describe how to create and use arrays Explain what a collection is and how to use one Describe how to enumerate through the members of a collection or an array
15
Why use Const & Enums Constants Can have user-friendly names to represent frequently used values Syntax: public const double Pi = 3.14159265; Enumerations (enums) Allow you to organize sets of related integral constants and use the sets to help make your application easy to read and debug. Ex: Enum Section { Name, Phone, Address }; SetValue( Section.Name, “Michael” ); vs. SetValue( “Name”, “Michael” ); Could have typo’s
16
Enumerations Allow you to work with sets of related constants and to associate easy-to-remember names with those sets. Ex: public enum DaysOfWeek{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } Can refer to constant with DaysOfWeek.Monday
17
Enumerations (cont) The default data type for enumerations is int It can be any of the integral numeric data types byte, short, int, and long To define an enum as a different data type, you must specify it in the declaration line. Ex: public enum DaysOfWeek : byte { … } You can assign values; default starts at 0 Enum DaysOfWeek { Monday = 1, Tuesday = 2..}
18
Demo Enum Enum ToString()
19
Array Basics Arrays are a way to manage groups of similarly typed values or objects Similar to Java Declaration Syntax: int[ ] MyNumbers = new int[ 5 ]; string[ ] MyStrings = new string[ 5 ]; Inline Initialization Syntax: Int[ ] MyNumbers = new int[ ] { 4, 3, 2 };
20
Multidimensional Arrays.NET Framework supports two forms of multidimensional arrays Jagged Arrays Array of Arrays (as in Java) Rectangular Arrays Length are the same for same dimension
21
Multidimensional Arrays (cont) Jagged Arrays Syntax: string[ ][ ] Families = new string[2] [ ]; Families[0] = new string[ 4 ] ; //array of 4 Families[1] = new string[ 2 ]; //array of 2
22
Multidimensional Arrays (cont) Rectangular Arrays Syntax: string[, ] stringArrays = new string[5, 3]; Think of as a table with rows and columns.
23
Collections Namespace: System.Collections Ex. ArrayList Provides same functionality as arrays with additional benefits. Auto resizing Internal collection type is Object
24
ArrayList Collection ( cont ) Typical Methods / Properties Add( object ) To add an object to the collection Remove( object ) To remove the object in the collection with the same reference RemoveAt( int index ) To remove the object in the collection with the corresponding index. Count Total number of element stored in the collection
25
Some other collections ClassDescription BitArray Manages a compact array of bits (1 and 0) Hashtable Represents a collection of key-and-value pairs that are organized base on the hash code of the key Queue Manages a group of objects on a first-in, first-out basis SortedList Organizes a group of objects and allows you to access those objects either by index or by a key value. Stack Manages a grou of objects on a first-in, last-out basis
26
Iteration over a collection foreach keyword Can be used to iterate over the values in a collection Syntax: int [ ] MyIntegers = new int[ ] { 1, 2, 3, 4, 5 }; foreach( int i in MyIntegers ) { MessageBox.Show( i.ToString() ); }
27
Topics Covered Today Using Data Types Using Data Types Using Constants, Enums, Arrays, and Collections Using Constants, Enums, Arrays, and Collections Implementing Properties Implementing Delegates & Events Mobile Application Development Give Away
28
Properties After this lesson, you will be able to: Explain how to implement properties Describe how to create a read-only or write-only property
29
Properties What are they? Properties are a specialized methods that look like a field. Their values are set and retrieved similar to fields. Why use properties? Simplicity Extensive use in DataBinding (future lecture)
30
Properties (cont) Typically used to expose a private or protected field; similar to a method. Property can be used to set the value of a field or retrieve a value of a field. Key advantage, can scrutinize value being assigned prior to assigning it.
31
Properties - Syntax public String FirstName { get//reading { return m_FirstName; } set//writing { m_FirstName = value; }} Value keyword is special; it is the value being passed in to the property.
32
Properties – Additional Info Some additional topics of significance to look into Creating an Indexer property Creating a Collection property
33
Demo Properties
34
Topics Covered Today Using Data Types Using Data Types Using Constants, Enums, Arrays, and Collections Using Constants, Enums, Arrays, and Collections Implementing Properties Implementing Properties Implementing Delegates & Events Mobile Application Development Give Away
35
Delegates And Events After this lesson you will be able to: Explain what a delegate is and how to create one Describe how to declare and raise events from your application Explain how to implement event handlers and associate them with events Describe how to dynamically add and remove event handlers at run time
36
Delegates Are essential and central to how events work in.NET In essence, a delegate is a function pointer that is also type safe.
37
How to use a delegate 4 Steps 1. Declare the delegate type variable specifying the prototype signature of the methods it is allowed to call. 2. Create a method that matches the signature of the delegate. 3. Create an new instance of that delegate type passing it the method name from #2 4. Invoke the function through the delegate as many times as necessary.
38
How to Declare a Delegate [ qualifier ] delegate [ function prototype signature ] Ex: public delegate int DoubleDelegate( int a ); Signature of Delegate The type name of your delegate
39
Create a method to match public delegate int DoubleDelegate( int a); … int DoubleNumber( int aNumber ) { return aNumber * 2; }
40
Create an instance of delegate public delegate int DoubleDelegate( int a); … int DoubleNumber( int aNumber ) { return aNumber * 2; } DoubleDelegate myDelegate = new DoubleDelegate( DoubleNumber );
41
Call the delegate as you wish public delegate int DoubleDelegate( int a); … int DoubleNumber( int aNumber ) { return aNumber * 2; } DoubleDelegate myDelegate = new DoubleDelegate( DoubleNumber ); int total = myDelegate( 100 );//total = 200 int total2 = myDelegate( 15 ); //total2 = 30
42
Events Events are tied to delegates. When an event is declared, the corresponding delegate is specified. In C#, once an event is declared, it must be associated with one or more event handlers before it can be raised. An event handler is a method that is called through a delegate when an event is raised. The function prototype used to handle the event is the same prototype as the delegate associated with the event.
43
Events Syntax public delegate void calculationDelegate(double d);public event calculationDelegate CalculationComplete; CalculationComplete( 44.4 );//raises the event The delegate type The event Reference variable
44
Event Handlers Once an event is declared, it must be associated with one or more event handlers before it can be raised. Creating an event handler is as simple as Defining a method that matches the signature of the delegate corresponding to the event. creating an instance of a Delegate passing the function name that will handle the event and registering it with the event. Ex: myEvent += new DoubleDelegate( DblFunc );
45
Event Handlers in Forms Events in Forms are handled by the delegate System.EventHandler Example: Button1.OnClick += new System.EventHandler(OnClick); Instance of a Button object Event object In Button class To Handle Clicks The delegate that handles Most form events. Delegate Is located in the System namespace. The method on the Form that will handle The event.
46
Topics Covered Today Using Data Types Using Data Types Using Constants, Enums, Arrays, and Collections Using Constants, Enums, Arrays, and Collections Implementing Properties Implementing Properties Implementing Delegates & Events Implementing Delegates & Events Mobile Application Development Give Away
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.