+ All Categories
Home > Documents > Michael Olivero Microsoft Student Ambassador for FIU [email protected].

Michael Olivero Microsoft Student Ambassador for FIU [email protected].

Date post: 14-Dec-2015
Category:
Upload: audrey-barrett
View: 214 times
Download: 0 times
Share this document with a friend
46
Michael Olivero Microsoft Student Ambassador for FIU [email protected]
Transcript
Page 1: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Michael OliveroMicrosoft Student Ambassador for [email protected]

Page 2: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Pre-Lecture TopicsPre-Lecture Topics

Web site resourcesWeb site resources Lecture & MaterialsLecture & Materials

http://microsoft.mike95.comhttp://microsoft.mike95.com MSDN AA programMSDN AA program

http://www.cs.fiu.edu/MSDNAA/http://www.cs.fiu.edu/MSDNAA/

Page 3: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Topics Covered TodayTopics Covered Today

Using Data TypesUsing Data Types Using Constants, Enums, Arrays, and Using Constants, Enums, Arrays, and

CollectionsCollections Implementing PropertiesImplementing Properties Implementing Delegates & EventsImplementing Delegates & Events Mobile Application DevelopmentMobile Application Development Give AwayGive Away

Page 4: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Using Data TypesUsing Data Types

After this lesson, you will be able to :After this lesson, you will be able to : Describe the data types provided by the .NET Describe the data types provided by the .NET

FrameworkFramework Explain how to perform implicit and explicit Explain how to perform implicit and explicit

conversions between typesconversions between types Describe the functionality provided by types of Describe the functionality provided by types of

the .NET Frameworkthe .NET Framework Describe string manipulation functions with the Describe string manipulation functions with the

String class methodsString class methods

Page 5: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Data TypesData Types

All .NET languages are All .NET languages are strongly typedstrongly typed Built in Value TypesBuilt in Value Types

Integer TypesInteger Types Floating-point TypesFloating-point Types Boolean TypeBoolean Type Char TypeChar Type

Built in Reference TypesBuilt in Reference Types StringString ObjectObject

Page 6: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Data TypesData Types

Mappings from .NET to C# & VB.NETMappings from .NET to C# & VB.NET

TYPETYPE C#C# VB.NETVB.NET RANGERANGESystem.ByteSystem.Byte bytebyte ByteByte 0 – 2550 – 255

System.Int16System.Int16 shortshort ShortShort -32768 – 32767-32768 – 32767

System.Int32System.Int32 intint IntegerInteger -2-23131 to 2 to 23131-1-1

System.Int64System.Int64 longlong LongLong -2-26363 to 2 to 26363-1-1

System.SByteSystem.SByte sbytesbyte (NA)(NA) -128 to 127-128 to 127

System.UInt16System.UInt16 ushortushort (NA)(NA) 0 to 655350 to 65535

System.UInt32System.UInt32 uintuint (NA)(NA) 0 to 20 to 23232-1-1

System.UInt64System.UInt64 ulongulong (NA)(NA) 0 to 20 to 26464-1-1

Page 7: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

System.ObjectSystem.Object

The Object type is the supertype of The Object type is the supertype of all types in the .NET Framework.all types in the .NET Framework.

Every type, whether value type or Every type, whether value type or reference type, derives from reference type, derives from System.Object.System.Object.

Page 8: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Converting TypesConverting Types

Implicit ConversionsImplicit Conversions are performed whenever the are performed whenever the

conversion can occur without the conversion can occur without the loss (ex. long t = intValue; )loss (ex. long t = intValue; )

Explicit ConversionExplicit Conversion Necessary when conversion where Necessary when conversion where

types cannot be implicitly convertedtypes cannot be implicitly converted Cast similar to Java & C++Cast similar to Java & C++ (ex. int t = (int)longValue; )(ex. int t = (int)longValue; )

System.Convert classSystem.Convert class

Page 9: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Common Type MethodsCommon Type Methods EqualsEquals

Determines whether two instances are Determines whether two instances are equalequal

GetHashCodeGetHashCode Serves as a hash function for a particular Serves as a hash function for a particular

typetype GetTypeGetType

Returns the type object for the current Returns the type object for the current instanceinstance

ToStringToString Returns a human-readable form of the Returns a human-readable form of the

objectobject

Page 10: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

ValueType.Parse()ValueType.Parse()

All built in value types have a All built in value types have a Parse() method.Parse() method. The Parse method can be used to The Parse method can be used to

convert a string to the type desired.convert a string to the type desired.

Ex:Ex: int Age = Int32.Parse( tbxAge.Text );int Age = Int32.Parse( tbxAge.Text ); int Age = int.Parse( tbxAge.Text );int Age = int.Parse( tbxAge.Text );

Page 11: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Common String methodsCommon String methods

String.Replace()String.Replace() Replaces all occurrences of a specified character in the string with Replaces all occurrences of a specified character in the string with

another specified characteranother specified character

String.Split()String.Split() Returns an array of substrings that are delimited by a specified Returns an array of substrings that are delimited by a specified

charactercharacter

String.Substring()String.Substring() Returns a substring from the specified instanceReturns a substring from the specified instance

String.ToUpper(), ToLower()String.ToUpper(), ToLower() Returns the string converted to all lowercase or all uppercase, Returns the string converted to all lowercase or all uppercase,

respectivelyrespectively

Page 12: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

DemoDemo

System.Convert functionsSystem.Convert functions Int.Parse()Int.Parse() Recommendation on StringBuilderRecommendation on StringBuilder

Page 13: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Topics Covered TodayTopics Covered Today

Using Data TypesUsing Data Types Using Constants, Enums, Arrays, and Using Constants, Enums, Arrays, and

CollectionsCollections

Implementing PropertiesImplementing Properties Implementing Delegates & EventsImplementing Delegates & Events Mobile Application DevelopmentMobile Application Development Give AwayGive Away

Page 14: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Const, Enum, Array, CollectionsConst, Enum, Array, Collections

After this lesson, you will be able to :After this lesson, you will be able to : Create constants and enumerations, and use Create constants and enumerations, and use

them in methodsthem in methods Describe how to create and use arraysDescribe how to create and use arrays Explain what a collection is and how to use oneExplain what a collection is and how to use one Describe how to enumerate through the Describe how to enumerate through the

members of a collection or an arraymembers of a collection or an array

Page 15: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Why use Const & EnumsWhy use Const & Enums ConstantsConstants

Can have user-friendly names to represent frequently Can have user-friendly names to represent frequently used values used values

Syntax: public const double Pi = 3.14159265; Syntax: public const double Pi = 3.14159265; Enumerations (enums)Enumerations (enums)

Allow you to organize Allow you to organize sets of related integral sets of related integral constantsconstants and use the sets to help make your and use the sets to help make your application easy to read and debug. application easy to read and debug.

Ex:Ex: Enum Section { Name, Phone, Address };Enum Section { Name, Phone, Address }; SetValue( Section.Name, “Michael” );SetValue( Section.Name, “Michael” );vs.vs. SetValue( “Name”, “Michael” );SetValue( “Name”, “Michael” );

Could have typo’s

Page 16: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

EnumerationsEnumerations Allow you to work with sets of related constants and to Allow you to work with sets of related constants and to

associate easy-to-remember names with those sets. associate easy-to-remember names with those sets. Ex:Ex:

public enum DaysOfWeekpublic enum DaysOfWeek{{

Monday,Monday,Tuesday,Tuesday,Wednesday,Wednesday,Thursday,Thursday,Friday,Friday,Saturday,Saturday,SundaySunday

}} Can refer to constant withCan refer to constant with

DaysOfWeek.MondayDaysOfWeek.Monday

Page 17: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Enumerations (cont)Enumerations (cont)

The default data type for enumerations is intThe default data type for enumerations is int It can be any of the integral numeric data types It can be any of the integral numeric data types

byte, short, int, and longbyte, short, int, and long

To define an enum as a different data type, you To define an enum as a different data type, you

must specify it in the declaration line.must specify it in the declaration line. Ex: public enum DaysOfWeek : byte { … }Ex: public enum DaysOfWeek : byte { … }

You can assign values; default starts at 0You can assign values; default starts at 0 Enum DaysOfWeek { Monday = 1, Tuesday = 2..}Enum DaysOfWeek { Monday = 1, Tuesday = 2..}

Page 18: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

DemoDemo

EnumEnum Enum ToString()Enum ToString()

Page 19: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Array BasicsArray Basics Arrays are a way to manage groups of similarly typed Arrays are a way to manage groups of similarly typed

values or objectsvalues or objects Similar to JavaSimilar to Java

Declaration Syntax:Declaration Syntax: int[ ] MyNumbers = new int[ 5 ];int[ ] MyNumbers = new int[ 5 ]; string[ ] MyStrings = new string[ 5 ];string[ ] MyStrings = new string[ 5 ];

Inline Initialization Syntax:Inline Initialization Syntax: Int[ ] MyNumbers = new int[ ] { 4, 3, 2 };Int[ ] MyNumbers = new int[ ] { 4, 3, 2 };

Page 20: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Multidimensional ArraysMultidimensional Arrays.NET Framework supports two forms of multidimensional arrays.NET Framework supports two forms of multidimensional arrays

Jagged ArraysJagged Arrays Array of Arrays (as in Java)Array of Arrays (as in Java)

Rectangular ArraysRectangular Arrays Length are the same for same Length are the same for same

dimensiondimension

Page 21: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Multidimensional Arrays (cont)Multidimensional Arrays (cont)

Jagged ArraysJagged Arrays Syntax:Syntax:

string[ ][ ] Families = new string[2] [ ]; string[ ][ ] Families = new string[2] [ ];

Families[0] = new string[ 4 ] ; //array of 4Families[0] = new string[ 4 ] ; //array of 4

Families[1] = new string[ 2 ]; //array of 2Families[1] = new string[ 2 ]; //array of 2

Page 22: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Multidimensional Arrays (cont)Multidimensional Arrays (cont)

Rectangular ArraysRectangular Arrays Syntax:Syntax:

string[ , ] stringArrays = new string[5, 3];string[ , ] stringArrays = new string[5, 3]; Think of as a table with rows and Think of as a table with rows and

columns. columns.

Page 23: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Collections Collections

Namespace: System.CollectionsNamespace: System.Collections Ex. ArrayList Ex. ArrayList

Provides same functionality as arrays Provides same functionality as arrays with additional benefits.with additional benefits.

Auto resizingAuto resizing Internal collection type is ObjectInternal collection type is Object

Page 24: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

ArrayList Collection ( cont )ArrayList Collection ( cont )

Typical Methods / PropertiesTypical Methods / Properties Add( object )Add( object )

To add an object to the collectionTo add an object to the collection Remove( object )Remove( object )

To remove the object in the collection with the To remove the object in the collection with the same referencesame reference

RemoveAt( int index )RemoveAt( int index ) To remove the object in the collection with the To remove the object in the collection with the

corresponding index.corresponding index. CountCount

Total number of element stored in the collectionTotal number of element stored in the collection

Page 25: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Some other collectionsSome other collections

ClassClass DescriptionDescriptionBitArrayBitArray Manages a compact array of bits (1 and 0)Manages a compact array of bits (1 and 0)

HashtableHashtable Represents a collection of key-and-value pairs that are organized base on Represents a collection of key-and-value pairs that are organized base on the hash code of the keythe hash code of the key

QueueQueue Manages a group of objects on a first-in, first-out basisManages a group of objects on a first-in, first-out basis

SortedListSortedList Organizes a group of objects and allows you to access those objects Organizes a group of objects and allows you to access those objects either by index or by a key value.either by index or by a key value.

StackStack Manages a grou of objects on a first-in, last-out basisManages a grou of objects on a first-in, last-out basis

Page 26: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Iteration over a collectionIteration over a collection

foreach keywordforeach keyword Can be used to iterate over the values in a Can be used to iterate over the values in a

collectioncollection Syntax:Syntax:

int [ ] MyIntegers = new int[ ] { 1, 2, 3, 4, 5 };int [ ] MyIntegers = new int[ ] { 1, 2, 3, 4, 5 };foreach( int i in MyIntegers )foreach( int i in MyIntegers ){{

MessageBox.Show( i.ToString() );MessageBox.Show( i.ToString() );}}

Page 27: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Topics Covered TodayTopics Covered Today

Using Data TypesUsing Data Types Using Constants, Enums, Arrays, Using Constants, Enums, Arrays,

and Collectionsand Collections Implementing PropertiesImplementing Properties

Implementing Delegates & EventsImplementing Delegates & Events Mobile Application DevelopmentMobile Application Development Give AwayGive Away

Page 28: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

PropertiesProperties

After this lesson, you will be able to:After this lesson, you will be able to: Explain how to implement Explain how to implement

propertiesproperties Describe how to create a read-only Describe how to create a read-only

or write-only propertyor write-only property

Page 29: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

PropertiesProperties

What are they?What are they? Properties are a specialized methods Properties are a specialized methods

that look like a field. Their values are that look like a field. Their values are set and retrieved similar to fields.set and retrieved similar to fields.

Why use properties?Why use properties? SimplicitySimplicity Extensive use in DataBinding (future Extensive use in DataBinding (future

lecture)lecture)

Page 30: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Properties (cont)Properties (cont)

Typically used to expose a private Typically used to expose a private or protected field; similar to a or protected field; similar to a method.method.

Property can be used to set the Property can be used to set the value of a field or retrieve a value of value of a field or retrieve a value of a field.a field.

Key advantage, can scrutinize value Key advantage, can scrutinize value being assigned prior to assigning it.being assigned prior to assigning it.

Page 31: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Properties - SyntaxProperties - Syntaxpublic String FirstNamepublic String FirstName{{

getget //reading //reading {{

return m_FirstName;return m_FirstName;}}

setset //writing//writing{{

m_FirstName = m_FirstName = valuevalue;;}}

}}

Value keyword is special; it is the value being passed in to the Value keyword is special; it is the value being passed in to the property.property.

Page 32: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Properties – Additional InfoProperties – Additional Info

Some additional topics of Some additional topics of significance to look intosignificance to look into Creating an Indexer propertyCreating an Indexer property Creating a Collection propertyCreating a Collection property

Page 33: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

DemoDemo

PropertiesProperties

Page 34: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Topics Covered TodayTopics Covered Today

Using Data TypesUsing Data Types Using Constants, Enums, Arrays, Using Constants, Enums, Arrays,

and Collectionsand Collections Implementing PropertiesImplementing Properties Implementing Delegates & EventsImplementing Delegates & Events

Mobile Application DevelopmentMobile Application Development Give AwayGive Away

Page 35: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Delegates And EventsDelegates And Events

After this lesson you will be able to:After this lesson you will be able to: Explain what a delegate is and how to Explain what a delegate is and how to

create onecreate one Describe how to declare and raise events Describe how to declare and raise events

from your applicationfrom your application Explain how to implement event Explain how to implement event

handlers and associate them with eventshandlers and associate them with events Describe how to dynamically add and Describe how to dynamically add and

remove event handlers at run timeremove event handlers at run time

Page 36: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

DelegatesDelegates

Are essential and central to how Are essential and central to how events work in .NETevents work in .NET

In essence, a delegate is a function In essence, a delegate is a function pointer that is also type safe.pointer that is also type safe.

Page 37: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

How to use a delegateHow to use a delegate

4 Steps4 Steps1.1. Declare the delegate type variable Declare the delegate type variable

specifying the prototype signature of the specifying the prototype signature of the methods it is allowed to call.methods it is allowed to call.

2.2. Create a method that matches the Create a method that matches the signature of the delegate.signature of the delegate.

3.3. Create an new instance of that delegate Create an new instance of that delegate type passing it the method name from #2type passing it the method name from #2

4.4. Invoke the function through the delegate Invoke the function through the delegate as many times as necessary.as many times as necessary.

Page 38: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

How to Declare a DelegateHow to Declare a Delegate

[ qualifier ] delegate [ function [ qualifier ] delegate [ function prototype signature ]prototype signature ]

Ex:Ex:public delegate int DoubleDelegate( int a );public delegate int DoubleDelegate( int a );

Signature of Delegate

The type name of your delegate

Page 39: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Create a method to matchCreate a method to match

public delegate int DoubleDelegate( int a);public delegate int DoubleDelegate( int a);

……

int DoubleNumber( int aNumber )int DoubleNumber( int aNumber )

{{

return aNumber * 2;return aNumber * 2;

}}

Page 40: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Create an instance of delegateCreate an instance of delegate

public delegate int DoubleDelegate( int a);public delegate int DoubleDelegate( int a);

……

int DoubleNumber( int aNumber )int DoubleNumber( int aNumber )

{{

return aNumber * 2;return aNumber * 2;

}}

DoubleDelegate myDelegate =DoubleDelegate myDelegate =

new DoubleDelegate( DoubleNumber );new DoubleDelegate( DoubleNumber );

Page 41: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Call the delegate as you wishCall the delegate as you wish

public delegate int DoubleDelegate( int a);public delegate int DoubleDelegate( int a);……int DoubleNumber( int aNumber )int DoubleNumber( int aNumber ){{

return aNumber * 2;return aNumber * 2;}}

DoubleDelegate myDelegate =DoubleDelegate myDelegate =new DoubleDelegate( DoubleNumber );new DoubleDelegate( DoubleNumber );

int total = myDelegate( 100 );int total = myDelegate( 100 ); //total = 200//total = 200int total2 = myDelegate( 15 ); int total2 = myDelegate( 15 ); //total2 = 30//total2 = 30

Page 42: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

EventsEvents

Events are tied to delegates. When an event is Events are tied to delegates. When an event is declared, the corresponding delegate is declared, the corresponding delegate is specified.specified.

In C#, once an event is declared, it must be In C#, once an event is declared, it must be associated with one or more event handlers associated with one or more event handlers before it can be raised. An event handler is a before it can be raised. An event handler is a method that is called through a delegate when method that is called through a delegate when an event is raised. an event is raised.

The function prototype used to handle the event The function prototype used to handle the event is the same prototype as the delegate is the same prototype as the delegate associated with the event.associated with the event.

Page 43: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Events SyntaxEvents Syntax

public delegate void calculationDelegate(double d);public delegate void calculationDelegate(double d);

public event calculationDelegate CalculationComplete;public event calculationDelegate CalculationComplete;

CalculationComplete( 44.4 );CalculationComplete( 44.4 ); //raises the event//raises the event

The delegate type The eventReference variable

Page 44: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Event HandlersEvent Handlers

Once an event is declared, it must be Once an event is declared, it must be associated with one or more event handlers associated with one or more event handlers before it can be raised.before it can be raised.

Creating an event handler is as simple as Creating an event handler is as simple as Defining a method that matches the signature of the Defining a method that matches the signature of the

delegate corresponding to the event.delegate corresponding to the event. creating an instance of a Delegate passing the creating an instance of a Delegate passing the

function name that will handle the event and function name that will handle the event and registering it with the event.registering it with the event.

Ex: myEvent += new DoubleDelegate( DblFunc );Ex: myEvent += new DoubleDelegate( DblFunc );

Page 45: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Event Handlers in FormsEvent Handlers in Forms

Events in Forms are handled by the delegate Events in Forms are handled by the delegate System.EventHandlerSystem.EventHandler

Example:Example:Button1.OnClick += new System.EventHandler(OnClick);Button1.OnClick += new System.EventHandler(OnClick);

Instance of aButton object

Event objectIn Button classTo Handle Clicks

The delegate that handlesMost form events. DelegateIs located in the System namespace.

The method on theForm that will handleThe event.

Page 46: Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com.

Topics Covered TodayTopics Covered Today

Using Data TypesUsing Data Types Using Constants, Enums, Arrays, Using Constants, Enums, Arrays,

and Collectionsand Collections Implementing PropertiesImplementing Properties Implementing Delegates & EventsImplementing Delegates & Events Mobile Application DevelopmentMobile Application Development

Give AwayGive Away


Recommended