Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development...
-
Upload
lynne-golden -
Category
Documents
-
view
226 -
download
4
Transcript of Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development...
Module 3: Working with Components
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
An Introduction to Key .NET Framework Development Technologies
Windows Forms
Web Forms
XML Web Services
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
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 {...}
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() {...}
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];
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; }}
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
Lab 3.1: Creating a .NET Framework Component
Creating a Simple Console Client
Using the Libraries
Instantiating the Component
Calling the Component
Building the Client
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;
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();
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));
}
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
Lab 3.2: Creating a Simple Console-Based Client
Demonstration: Creating a Windows Forms Client
Creating an ASP.NET Client
Writing the HTML for the ASP.NET Application
Coding the Page_Load Event Handler
Generating the HTML Response
Multimedia: ASP.NET Execution Model
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>
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();}
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
Demonstration: Testing the ASP.NET Client
Lab 3.3: Calling a Component Through an ASP.NET Page
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