T9.NET Programming for the Business – Spring 2007 Session 3 C# February 13th - 2007.
-
Upload
rosaline-atkinson -
Category
Documents
-
view
217 -
download
0
Transcript of T9.NET Programming for the Business – Spring 2007 Session 3 C# February 13th - 2007.
T9 .NET Programming for the Business – Spring 2007
Session 3
C# • February 13th - 2007
T9 .NET Programming for the Business – Spring 2007
This week’s info
• This lecture will be a short one (13-15)• Helping teacher• Max number is raised from 50-60• Administration email (for SiteScape access) [email protected]• Project will be uploaded this afternoon on SiteScape
T9 .NET Programming for the Business – Spring 2007
Methods
• Methods have a return type (void is a legal return type meaning no return value).
• Methods have any number of parameters.• Methods may be called by supplying the object instance, the name
of the method, and any number of parameters.• At runtime, the formal parameters take the value of the actual
parameters.• Parameters may be specified as in (default), out or ref.• The params keyword may be used for an unspecified number of
parameters.
T9 .NET Programming for the Business – Spring 2007
Params
• Params may be used to pass an unspecified number of parameters.
public static void UseParams(params int[] list) { for ( int i = 0 ; i < list.Length ; i++ ) Console.WriteLine(list[i]); Console.WriteLine();}
public static void UseParams2(params object[] list) { for ( int i = 0 ; i < list.Length ; i++ ) Console.WriteLine(list[i]); Console.WriteLine();}
public static void Main() { UseParams(1, 2, 3); UseParams2(1, 'a', "test");
int[] myarray = new int[3] {10,11,12}; UseParams(myarray);}
T9 .NET Programming for the Business – Spring 2007
Properties
• Properties allow clients to access object state as if they were accessing member fields directly, while actually implementing access through methods.
• Decouples the object state from the implementation, while providing natural syntax.
• Example:– myString.Length // only getter– Sb.Capacity = 1000; // setter– Sb.Capacity // Getter
T9 .NET Programming for the Business – Spring 2007
Properties
• private int myVar; • public int MyVar• {• get • {• return myVar;• }• set• {• if (value > 0)• {• myVar = value;• }• else { myVar = 0; }• }• }
T9 .NET Programming for the Business – Spring 2007
Exercise – Find 5 errors
private int BadMethod(int integerInput,string stringInput) { if (stringInput = "test") { return 23.2 } for(Counter=0;Counter<30;Counter++) { if(integerInput == Counter) return integerInput; }
}
// should be ==
// missing ;
// missing declaration
and Wrong return type
// need return for all code paths
T9 .NET Programming for the Business – Spring 2007
Indexers
• Sometimes it is useful to access a class as if it were an array. • You can do this by creating indexers.• Useful for "lookup scenarios".• Indexers resemble properties, but introduce a type of the index (not
necessarily int).
T9 .NET Programming for the Business – Spring 2007
Indexers
public string this[int index]{ get { }
set { }}
T9 .NET Programming for the Business – Spring 2007
Indexers – example
• Listbox is a component in .NET• The listbox is an object with an array strings• An indexer can be made to access the list of strings with:• ListBox list = new ListBox()• ... Add some strings to the listbox• string firstString = list[0];• String lastString = list[list.length-1];
• It seems that “list” is an array, but with the indexer we index the array of string within the “list”
T9 .NET Programming for the Business – Spring 2007
Exceptions
• Exceptions are for abnormal situations and error conditions - ONLY.• Throw an instance of an object derived from System.Exception. This object
can contain any relevant information subject to interpretation by the handler.
• Exceptions work by unravelling the call stack searching for the first handler for the particular exception type.
• If no handler is found, the program stops.
T9 .NET Programming for the Business – Spring 2007
Throwing Exceptions
• Throwing an exception is done with the throw statement:
• throw new System.Exception("Something bad happened");
T9 .NET Programming for the Business – Spring 2007
Catching Exceptions
• Exceptions are caught with catch statements that are given in conjunction with a try block.
try{ int hhh = 7 / f();}// Any number of catch clauses...catch (DivideByZeroException e){ System.Console.WriteLine(e.Message); throw;}
T9 .NET Programming for the Business – Spring 2007
Catching Exceptions
• Specify more specialized handlers before more generic exception types.
try{ int hhh = 7 / f();}// Any number of catch clauses...catch (DivideByZeroException e){ System.Console.WriteLine(e.Message); throw;}catch (ArithmeticError e){ ... }catch (Exception e){ // Matches any exception }
T9 .NET Programming for the Business – Spring 2007
Exceptions
• Create your own exception classes (e.g MyException) and implement MyException.Message to provide a text representation of what went wrong.
• Only catch exceptions you know what to do about. • If you throw exceptions from an exception handler, be sure to
supply the inner exception, proving a chain back.• Don't use the catch all handler.
T9 .NET Programming for the Business – Spring 2007
Exceptions, examples
try{ int hhh = 7 / f();}// Any number of catch clauses...catch (DivideByZeroException e){ System.Console.WriteLine(e.Message); throw;}finally{} class SingularEquations : System.Exception
{ public override string Message { get { return "The equations have no solution"; } }
}
T9 .NET Programming for the Business – Spring 2007
Attributes.
• It is possible to assign metadata to programmatic entities in your program. This is done by specifying attributes.
• We will see how this is used mainly when we discuss web services in more depth.
• Inspect metadata in the pre generated AssemblyInfo.cs file in your project.
• [WebMethod] • public string HelloWorld() • { • return "Hello World"; • }
T9 .NET Programming for the Business – Spring 2007
Attributes
• [AttributeUsage(AttributeTargets.All)] public class ProductInfo : System.Attribute {
• private double m_version = 1.00; • private string m_authorName; • public ProductInfo(double version,string name)
{ • m_version = version;
m_authorName = name; • } • public double Version
{ • get { return m_version; } • } • public string AuthorName
{ • get { return m_authorName; } • } • }
T9 .NET Programming for the Business – Spring 2007
Setting attributes
• [ProductInfo(1.005,"CoderSource"])• public class AnyClass { }
T9 .NET Programming for the Business – Spring 2007
Reading attributes
• MemberInfo membInfo;membInfo = typeof(AnyClass);object [] attributes;
• attributes = membInfo.GetCustomAttributes(typeof(ProductInfo),true);
• if(attributes.GetLength(0)!=0) {
• ProductInfo pr = (ProductInfo) attributes[0]; Console.WriteLine("Product author: {0}",pr.AuthorName); Console.WriteLine("Product version; {0}",pr.Version);
• }
T9 .NET Programming for the Business – Spring 2007
C# 2.0 features
• The following features have been added to the C# language in the Whidbey VS package.
– Partial classes.– Anonymous methods– Generics– Iterators.
T9 .NET Programming for the Business – Spring 2007
C# 2.0: Partial classes.
• Somethimes it is convenient to have the implementation of a class span multiple source files.
• This is easily done using partial classes.• Example:
– WinForms.• This is really a macro-like feature.
T9 .NET Programming for the Business – Spring 2007
C# 2.0: Generics
• Generics allow classes, structs, interfaces, delegates and methods to be parameterized by the data they work on.
• Why?– Avoids using messy error prone System.Object references.– Moves errors from runtime to compiletime
T9 .NET Programming for the Business – Spring 2007
Collection classes.
• The collection classes (from which you may build advanced data structures) are available in both generic and non-generic versions.
– Dictionary, List etc.
T9 .NET Programming for the Business – Spring 2007
The project …
• Which components do we “normally” have in a solution ?
? ? ?
?
?
Middleware
Clients
Storage
T9 .NET Programming for the Business – Spring 2007
NETShop – A business example
NetShop
The database
T9 .NET Programming for the Business – Spring 2007
NETShop – A business example
The Webservice
NetShop
T9 .NET Programming for the Business – Spring 2007
NETShop – A business example
The WebForm
NetShop
T9 .NET Programming for the Business – Spring 2007
NETShop – A business example
The Winform
NetShop
T9 .NET Programming for the Business – Spring 2007
NETShop – A business example
The Pocket PC
NetShop
T9 .NET Programming for the Business – Spring 2007
The project model.
PocketPC Winform WebForm
Webservice
Database
.NET Webservice
End clients
SQL
T9 .NET Programming for the Business – Spring 2007
T9 Food control
T9 .NET Programming for the Business – Spring 2007
T9 Food control
• Food-borne diseases are a worldwide problem • It is estimated that almost 70% of all episodes of diarrhoea come
from food poisoning• Control is necessary• Danish government food-control office (Fødevarestyrelsen)• “Smiley”-project http://smiley.fvst.dk/Smiley.aspx?view=Simpel • T9 is more simple – yet extensive.
T9 .NET Programming for the Business – Spring 2007
T9 Food control – divisions
• The patrol: In this division we find the people that performs checkup on business handling food.
• The office: The office handles the results from lab tests and ensure that the right actions is taken if business do not comply to the government given standards.
• The Web team: To ensure that the public can safely eat food from the supermarkets restaurants etc. T9 food control has to populate the result of the checkup “the patrol” makes. This is done though the internet because it is believes to be the most flexible media and most Danish people have access to the internet. A Web application provides the functionality to display the information to the public.
T9 .NET Programming for the Business – Spring 2007
T9 Food control – The patrol
• Meet Susan.• Susan job is to check up on the
businesses on a regular basis. • Susan needs free hands• She needs to:
– Temperatures in refrigerators – Register food samples – Take general notes about the
business
T9 .NET Programming for the Business – Spring 2007
T9 Food control – The patrol
• Pocket PC application• .NET compact framework• Connects to the data base via a webservice• One of the core concepts in the control is that the checkup must be
unannounced, so every day Susan get to work she does not know which businesses she has to visit.
• A computer makes a random selection each morning
T9 .NET Programming for the Business – Spring 2007
T9 Food control – The office
• Meet Hans• Hans maintains information regarding
businesses (or as we might call “customers”)• Hans uses a Winform application where he can:
– alter existing data – and add new customers to the system – give a business an overall grade – read all the reports that the public makes
• Connects to the data base via a webservice
T9 .NET Programming for the Business – Spring 2007
T9 Food control - The Web team
• Meet the Jensens • Likes to eat out• Unfortunately the whole family got sick due
to food poisoning some weeks ago. • They use the T9 Food control to check the
state of sanitation • Would like to report the restaurant that gave
Them food poisoning.• Connects to the data base via a webservice
T9 .NET Programming for the Business – Spring 2007
The “Need to have”
• Data storage:• One database that can hold the following information:• “Customers” (The business to be checked) information such as name,
address, zip code etc. Apply a unique ID for each customer.• Tables holding information about temperatures in Refrigerators and
freezers• Tables holding information about food samples taken by “The patrol”.• Tables holding information about when the businesses have been checked.• Tables holding login information to the administration system used at the
office.• One table holding data for reports from the public.• Keep in mind that historical data also should be available, not just one (or
the latest) checkup result.
T9 .NET Programming for the Business – Spring 2007
The “Need to have”
• One webservice for transferring data between the Web application, Winform application and the pocket PC.
• All data transfers to and from the database must go through webservice(s)
• The webservice must also have a method e.g. “GetTodaysCheckUps” that randomly picks a number (e.g. 10) of business to be checked during the day.
T9 .NET Programming for the Business – Spring 2007
The “Need to have”
• “The patrol”:• One pocket PC application with the following functionality:• Start of the day:• When connected to the internet, the pocket pc should receive
names and addresses for the businesses to check.• “In the field” (When performing the checkup)• Refrigerator / freezer temperature measurements• Food samples, input fields for:
– Type of food – Unique ID– Note field for every sample
• A general note field for the business
T9 .NET Programming for the Business – Spring 2007
The “Need to have”
• The office:• One WinForm application where the employee can:• View all customers• Change main data for a specific customer (name address etc.)• View a list of all checkup preformed on the customer• View the results from a specific checkup• The ability to change all data from a check up.• View the reports that come from the public via the website.• Grade a specific checkup with a “smiley”-icon• The Winform application must be protected with login/password
T9 .NET Programming for the Business – Spring 2007
The “Need to have”
• The Web Team:• One web application where the public can:• Search for a specific business. Search parameters should include:
– Type of business (restaurants, supermarkets etc.) – Name of business (or part of name)– Address – “Smiley”-grade result
• Show the results of the latest check-up• A report module where people can report business for handling food
unsanitary. This form should include:– Name of business– Type of business– Address of business – A note field to explain the unsanitary conditions– A email input with the reporters email address.
T9 .NET Programming for the Business – Spring 2007
Groups (!)
• There is A LOT of work to be done.• Almost impossible to do all of it alone• Make groups of maximum 4 persons• Helping teacher will do the project as well