Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development...

25
Module 3: Working with Components

Transcript of Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development...

Page 1: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Module 3: Working with Components

Page 2: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Overview

An Introduction to Key .NET Framework Development Technologies

Creating a Simple .NET Framework Component

Creating a Simple Console Client

Creating an ASP.NET Client

Page 3: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

An Introduction to Key .NET Framework Development Technologies

Windows Forms

Web Forms

XML Web Services

Page 4: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Creating a Simple .NET Framework Component

Using Namespaces and Declaring the Class

Creating the Class Implementation

Implementing Structured Exception Handling

Creating a Property

Compiling the Component

Page 5: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Using Namespaces and Declaring the Class

Create a New Namespace

Declare the Class

using System;

namespace CompCS {...}

using System;

namespace CompCS {...}

public class StringComponent {...}public class StringComponent {...}

Page 6: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Creating the Class Implementation

Declare a Private Field of Type Array of String Elements

Create a Public Default Constructor

Assign the stringSet Field to an Array of Strings

stringSet = new string[] {"C# String 0","C# String 1",

... };

stringSet = new string[] {"C# String 0","C# String 1",

... };

private string[] stringSet;private string[] stringSet;

public StringComponent() {...}public StringComponent() {...}

Page 7: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Implementing Structured Exception Handling

Implement the GetString Method

Create and Throw a New Object of Type IndexOutOfRangeException

Exceptions May Be Caught by the Caller in try, catch, finally block

Structured Exception Handling Replaces HRESULT-Based Error Handling in COM

public string GetString(int index) {...}public string GetString(int index) {...}

if((index < 0) || (index >= stringSet.Length)) {throw new IndexOutOfRangeException();

}return stringSet[index];

if((index < 0) || (index >= stringSet.Length)) {throw new IndexOutOfRangeException();

}return stringSet[index];

Page 8: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Creating a Property

Create a Read-Only Count Property to Get the Number of String Elements in the stringSet Array

public int Count { get { return stringSet.Length; }}

public int Count { get { return stringSet.Length; }}

Page 9: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Compiling the Component

Use the /target:library Switch to Create a DLL

Otherwise, an executable with a .dll file extension is created instead of a DLL library

csc /out:CompCS.dll /target:library CompCS.cscsc /out:CompCS.dll /target:library CompCS.cs

Page 10: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Lab 3.1: Creating a .NET Framework Component

Page 11: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Creating a Simple Console Client

Using the Libraries

Instantiating the Component

Calling the Component

Building the Client

Page 12: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Using the Libraries

Reference Types Without Having to Fully Qualify the Type Name

If Multiple Namespaces Contain the Same Type Name, Create a Namespace Alias to Remove Ambiguity

using CompCS;

using CompVB;

using CompCS;

using CompVB;

using CSStringComp = CompCS.StringComponent;

using VBStringComp = CompVB.StringComponent;

using CSStringComp = CompCS.StringComponent;

using VBStringComp = CompVB.StringComponent;

Page 13: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Instantiating the Component

Declare a Local Variable of Type StringComponent

Create a New Instance of the StringComponent Class

CompCS.StringComponent myCSStringComp = new CompCS.StringComponent();

CompCS.StringComponent myCSStringComp = new CompCS.StringComponent();

Page 14: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Calling the Component

Iterate over All the Members of StringComponent and Output the Strings to the Console

for (int index = 0; index < myCSStringComp.Count; index++) {

Console.WriteLine (myCSStringComp.GetString(index));

}

for (int index = 0; index < myCSStringComp.Count; index++) {

Console.WriteLine (myCSStringComp.GetString(index));

}

Page 15: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Building the Client

Use the /reference Switch to Reference the Assemblies That Contain the StringComponent Class

csc /reference:CompCS.dll,CompVB.dll /out:ClientCS.exe ClientCS.cs

csc /reference:CompCS.dll,CompVB.dll /out:ClientCS.exe ClientCS.cs

Page 16: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Lab 3.2: Creating a Simple Console-Based Client

Page 17: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Demonstration: Creating a Windows Forms Client

Page 18: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Creating an ASP.NET Client

Writing the HTML for the ASP.NET Application

Coding the Page_Load Event Handler

Generating the HTML Response

Page 19: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Multimedia: ASP.NET Execution Model

Page 20: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Writing the HTML for the ASP.NET Application

Specify Page-Specific Attributes Within a Page Directive

Import the Namespace and the Physical Assembly

Specify Code Declaration Blocks

<%@ Page Language="C#" Description="ASP.NET Client" %><%@ Page Language="C#" Description="ASP.NET Client" %>

<%@ Import Namespace="CompCS"%><%@ Import Namespace="CompVB"%>

<%@ Import Namespace="CompCS"%><%@ Import Namespace="CompVB"%>

<script language="C#" runat=server>... //client code

</script>

<script language="C#" runat=server>... //client code

</script>

Page 21: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Coding the Page_Load Event Handler

void Page_Load(Object sender, EventArgs EvArgs){ StringBuilder Out = new StringBuilder(""); int Count = 0; // Iterate over component's strings and concatenate Out.Append("Strings from C# Component<br>"); CompCS.StringComponent myCSStringComp = new CompCS.StringComponent(); for(int index = 0; index < myCSStringComp.Count; index++) { Out.Append(myCSStringComp.GetString(index)); Out.Append("<br>"); } Message.InnerHtml = Out.ToString();}

void Page_Load(Object sender, EventArgs EvArgs){ StringBuilder Out = new StringBuilder(""); int Count = 0; // Iterate over component's strings and concatenate Out.Append("Strings from C# Component<br>"); CompCS.StringComponent myCSStringComp = new CompCS.StringComponent(); for(int index = 0; index < myCSStringComp.Count; index++) { Out.Append(myCSStringComp.GetString(index)); Out.Append("<br>"); } Message.InnerHtml = Out.ToString();}

Page 22: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Generating the HTML Response

<body> <span id="Message" runat=server/></body>

<body> <span id="Message" runat=server/></body>

Specify the Body of the HTML Response

Page 23: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Demonstration: Testing the ASP.NET Client

Page 24: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Lab 3.3: Calling a Component Through an ASP.NET Page

Page 25: Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.

Review

An Introduction to Key .NET Framework Development Technologies

Creating a Simple .NET Framework Component

Creating a Simple Console Client

Creating an ASP.NET Client