14
SSE693 C# and .NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Embed Size (px)

DESCRIPTION

Theory of Operation The following files are located in the TeamProject3ClaryHilliard file: ArrayLists.cs Collections.cs DeveloperAttributes.cs Dictionaries.cs Exceptions.cs Reflections.cs

Citation preview

Page 1: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

SSE693 C# and .NET

Dr. MacNeilSummer 2005

Jay Clary / Lance HilliardTeam Project 3

Page 2: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Purpose: To illustrate Topics found in Chapters 9-11 of Professional C# Chapter 9

Array Lists, Collections, Dictionaries Chapter 10

Reflection Chapter 11

Exceptions

Page 3: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Theory of Operation The following files are located in

the TeamProject3ClaryHilliard file: ArrayLists.cs Collections.cs DeveloperAttributes.cs Dictionaries.cs Exceptions.cs Reflections.cs

Page 4: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Theory of Operation ArrayLists.cs contains our example of Array

Lists from Chapter 9 page 240. First we created a sample class, Points, and then we instantiated the array list by declaring it with a capacity of 20. We used the add() method to fill the array lists. Finally, we use the arraylist.count method to get how many elements are in the array list and print their contents.

Page 5: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Theory of Operation Collections.cs contains our example of Collections

from Chapter 9 pages 241 through 244. First, we define the Parser class and implement the IEnumerable interface. Then we defined the Parser method and it receives as inputs a string and delimiters so it will know what to look for in each string. Next, we defined the ParserEnumerator class and inherited the IEnumerable class again. Finally, we set up our MoveNext, Current and Reset methods to work with our example (describing how to track the parsers position in the file).

Page 6: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Developer Attributes and Reflection - Theory of Operation Developer Attributes and Reflection are illustrated in the files

DeveloperAttributes.cs and Reflections.cs.

To demonstrate the user of Attributes we begin by using the special atribute syntax to define the appropriate AttributeUsage. Then we define a class that is derived from System.Attribute. This class contains a number of member properties to extract the information from the class. We finish the class by defining the private storage to hold the data.

At the bottom of DeveloperAttributes.cs, we use the special Attribute syntax to add 3 DeveloperAttribute entries that will exists as part of the myAttributeTest class.

To illustrate how this information is retrieved from a class, we instantiate the class Reflections which will bring into scope an instance of myAttributeTest. Once instantiated, myAttributeTest is called upon to present its MemberInfo. Using this, we can invoke GetCustomAttributes(). Once obtained, we can iterate through each object and access the internal Attributes we stored earlier as part of myAttributeTest.

Page 7: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Directories - Theory of Operation We will illustrate the use of Dictionaries in the file Dictionaries.cs.

We demonstrate Dictionaries by created a class used to hold a UserID. This is the data that we will use as the Hash key for access into the Dictionary. The UserID class overrides the GetHash(), ToString() and Equals() methods so that the class will be properly supported by the underlying .NET framework. To make the example more realistic, we also create another class called UserData that stores an instance of the UserID class and some additional data about the user. To make use of dictionaries, we then instantiate a HashTable class along with several instances of both UserID and UserData. This newly created data is inserted into the HashTable.

We can now search the Dictionary for any instance of data that we desire. A loop is provided that allows the user to input UserIDs that will be hashed and located in the Dictionary. If the data is found, it will be presented, if not and explanatory message will be shown. The user may enter ‘Q’ at any time to quit the Dictionary demonstration.

Page 8: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Theory of Operation Exceptions.cs contains our example of

exceptions from Chapter 11. We created an exception class and inherited ApplicationException. Then we created our own custom exception, “Attempt was made to divide by zero…” Then we implemented a sample use of try, catch and throw.

Page 9: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Compilation and Execution To build and execute the program, follow the steps

outlined below. First create a directory on Drive C called CSharp i.e. C:\

CSharp Unzip the file TeamProject3ClaryHilliard.zip into the

CSharp directory. One sub-directory will be created that contains the eight

files that demonstrate the important topics from the assigned reading.

Navigate to the directory of choice and Double click on the .sln file. This will launch the Microsoft C# development environment and load the project.

Click the Run Icon and observe the results.

Page 10: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Run Results Array Lists

Page 11: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Run Results Collections:

Page 12: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Run Results Attributes and Reflection:

Page 13: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Run Results Dictionary:

Page 14: SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3

Run Results Exceptions: