intro programming web

43
Microsoft Courses 2003 1 Introductory Programming with C# Judith Bishop University of Pretoria, South Africa Visiting TU-Berlin

Upload: yiweiw

Post on 29-Nov-2014

56 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro Programming Web

Microsoft Courses 2003 1

Introductory Programmingwith C#

Judith BishopUniversity of Pretoria, South Africa

Visiting TU-Berlin

Page 2: Intro Programming Web

Microsoft Courses 2003 2

Talk overview

Introduction– Who, why, when, how, with what?

Syntax and semantics– for you and your students

A tour through some lessons– Early concepts

– Our approach to GUIs

– Debugging

– Advanced concepts

Assessment– Quizzes, exercises, laboratories, exams

References

Page 3: Intro Programming Web

Microsoft Courses 2003 3

Featuring …

Syntax and semantics descriptions

Early use of libraries

Independent GUI specifications

Debugging New

Page 4: Intro Programming Web

Microsoft Courses 2003 4

Focus on C#

Designed by Anders Hejlsberg, Scott Wiltamuth and Peter Golde

To b the main development medium for future Microsoft products

Origins in C++, Java, Delphi, Modula-2, Smalltalk

Heljsberg was the chief architect behind Turbo Pascal and Delphi

Standardised by Ecma and ISO

Free (to us)

Page 5: Intro Programming Web

Microsoft Courses 2003 5

Changing languages

Major movements worldwide– To Pascal in 1970s and 1980s

– To Java in 1990s

Caused by advances in technology – Data structures, oops, internet computing

– Foundation for later courses

– Desire to be "ahead of the pack"

Inhibitors to change– Lack of teaching resources

– Computing resources required by new technology

– Investment in current language

– Uncertainty over the measure of improvement

Page 6: Intro Programming Web

Microsoft Courses 2003 6

A first programming course

Typically 40-50 lectures

10-14 laboratories

Take home assignments

Project

Questions:– where does it start?

– where does it end?

– what is the place of libraries?

– what is the order of topics?

– what should be included/left out?

– what do I need to run the language?

Page 7: Intro Programming Web

Microsoft Courses 2003 7

Where does it start and end?

Most institutions assume "no background"– most students have more than that – use of computers is almost universal now– students can interact with GUIs

Strong desire to have "objects first"– but what is second?– what do objects assume?

With or without GUIs?– huge tension between need to program realistically and the number

of concepts required to express GUIs

Advanced topics can be left to other courses– networking and databases - NetCentric Computing– generics and overloading - Data Structures

Page 8: Intro Programming Web

Microsoft Courses 2003 8

What about libraries?

Libraries cannot be ignored, – More in the libraries than in the language!– Without them, examples will be too constrained

Students can be on a "need to know basis", BUT they need to know the structure and organisation of libraries– This knowledge transcends languages

Early use of libraries introduces many fundamental concepts in a controlled manner, e.g.– variables vs properties– instance vs static– constructors– parameters

Page 9: Intro Programming Web

Microsoft Courses 2003 9

An order of topics

Introduction to computers, languages and compiling 2

Using types 5

Defining types 5

Data structures and control structures 5

Views System 4

Input and output with files 4

Debugging 3

Collections 5

Extensibility and polymorphism 5

Extra topics 2

Half way

Page 10: Intro Programming Web

Microsoft Courses 2003 10

What to include?

If a feature is covered, cover it completely, albeit over time in a spiral fashion

Include– images as data types - adds to the fun

– GUIs - for realism

– formatting and unicode - promotes internationalisation

– serialization - makes for serious programs

– exception handling - makes for robust programs

– foreach loop - so neat and powerful

– collections - enhance object-orientation

Page 11: Intro Programming Web

Microsoft Courses 2003 11

What to exclude?

What to exclude depends on– length of course– interface with other courses

A suggestion– threads - in Operating Systems– networking - for Netcentric Computing– graphics and delegates - to introduce non-Views GUIs– operator overloading, other upcoming features (e.g. generics), - in

Data Structures

Notes:– Topics that were in a Java introductory course might not be in a C#

version (applets)– NOTE: some institutions will start with Netcentric Computing -

interesting approach

Page 12: Intro Programming Web

Microsoft Courses 2003 12

What resources do I need?

Microsoft Academic Alliance, plus

Option 1 (Student):– a PC

– C# compiler

– Any simple editor

Option 2 (Lecturer)– a PC with lots of memory

– Visual Studio

Option 3 (Researcher)– PC or Mac, Windows or Linux

– Rotor

– Any simple editor

Page 13: Intro Programming Web

Microsoft Courses 2003 13

C# Concisely

First year programming text book, Oct 2003

Pearson, 2004

Incorporates Views

Reviewed by Microsoft

Contents on the Views website

http://csharp.cs.uvic.ca

Page 14: Intro Programming Web

Microsoft Courses 2003 14

Volunteers on a C# course in Africa

Do it in C# Naturally!

Page 15: Intro Programming Web

Microsoft Courses 2003 15

From the ECMA C# Specification

8.7.4 Properties

A property is a member that provides access to a characteristic of an object or a class. Examples of properties include the length of a string, the size of a font, the caption of a window, the name of a customer,and so on. Properties are a natural extension of fields. Both are named members with associated types, and the syntax for accessing fields and properties is the same. However, unlike fields, properties do not denote storage locations. Instead, properties have accessors that specify the statements to be executed when their values are read or written.

Properties are defined with property declarations. The first part of a property declaration looks quite similar to a field declaration. The second part includes a get accessor and/or a set accessor. In the example below, the Button class defines a Caption property.

public class Button { private string caption; public string Caption { get { return caption; } set { caption = value; Repaint(); }}}

Page 16: Intro Programming Web

Microsoft Courses 2003 16

Syntax forms in C#CFixed wordsand symbols

Items tofill in

public string Course { get {return course;}}

Page 17: Intro Programming Web

Microsoft Courses 2003 17

Also for libraries

Page 18: Intro Programming Web

Microsoft Courses 2003 18

Visual Studio Help

Page 19: Intro Programming Web

Microsoft Courses 2003 19

Concepts for simple oops

Page 20: Intro Programming Web

Microsoft Courses 2003 20

Example sequence from early lessons

Example 2.5 (page 45) - Meeting times– Creates objects of type DateTime and accesses their properties

and methods

Example 2.6 (page 47) - Dates in different formats– Further example of DateTime methods, customising output

Example 2.7 (page 49) - Time with reading– Introduces input using Console and the Parse methods of a type

Example 3.4 (page 83) - Table of meeting times– Using a loop to create different times

Examples 3.2 and 3.3 (page 75) - The shuttle bus– Defining a type from scratch and using it in a program

Page 21: Intro Programming Web

Microsoft Courses 2003 21

GUIs

Current approaches do not emphasise independent principles

OPTIONS

Create GUIs by hand– error prone

– takes too much time

Use a GUI builder– dumps code in the program

– hides principles

Page 22: Intro Programming Web

Microsoft Courses 2003 22

The reality of a

single cross-language,

cross-platform

GUI interface programming model

is in sight, based on an

XML description language

supported by

fast native runtimes. [Russel Jones, DevX, Nov 2002]

Where GUIs are going

Views

Page 23: Intro Programming Web

Microsoft Courses 2003 23

… and more recently

Supporting many GUIs

isn't just a simple process

of including one set of libraries or another;

it's often a frustrating and error-prone exercise

in writing GUI-specific code.

[Russel Jones, DevX, Aug 2003]

Page 24: Intro Programming Web

Microsoft Courses 2003 24

VS.NET

Common Language Runtime

System

System.Data(ADO.NET)

System.Xml

System.Drawing

System.Web(ASP.NET)

System.WinForms

SDK Tools

Rotor CLI Implementation

C#

JScript

Platform Abstraction

System.WinForms

Jim Miller
Make the next three slides fade transition so it's easy to see how they differ?
Page 25: Intro Programming Web

Microsoft Courses 2003 25

Views

Views is a Vendor Independent Extensible Windowing System

Developed by Nigel Horspool and Judith Bishop with help from students in 2002-2003

Provides an XML-based specification notation for defining GUIs, and an execution engine for handling event listening and dispatching back to the program

It was supported under the Microsoft Rotor RFP Program

It is distributed from the C# Concisely book website

Page 26: Intro Programming Web

Microsoft Courses 2003 26

Page 27: Intro Programming Web

Microsoft Courses 2003 27

Example in WinFormsshow.Click += new EventHandler(ActionPerformed);hide.Click += new EventHandler(ActionPerformed);}

public void ActionPerformed(Object src, EventArgs args) { if (src == show) { pic.Show(); } else if (src == hide) { pic.Hide(); }}i Embedded in 115 lines of generated code - “do not touch”

Unexplained classes and unused objects here

Page 28: Intro Programming Web

Microsoft Courses 2003 28

GUI building today

GUI BuilderGUI Builder

Add ListenersAdd Listeners

Handlers

widget rendering in the OS

widget rendering in the OS

Visual Studio

C#

Windowswidget calls in a language

Application

Page 29: Intro Programming Web

Microsoft Courses 2003 29

A GUI using XML

Application

Handlers

widget rendering in the OS

widget rendering in the OS

Control Engine

Add ListenersAdd Listeners

GUI

XML

Spec

GUI

XML

Spec

Page 30: Intro Programming Web

Microsoft Courses 2003 30

Example in ViewsViews.Form f = new Views.Form(@"<Form> <vertical> <horizontal> <Button Name=Show/> <Button Name=Hide/> </horizontal> <PictureBox Name=pic Image='C:Jacarandas.jpg' Height=175/> </vertical></Form>" );

string c;for (;;) { c = f.GetControl(); PictureBox pb = f["pic"]; switch (c) { case ”Show" : pb.Show(); break; } case ”Hide" : pb.Hide(); break; } } }

No pixel positioning

No generated code

Separation of concerns

XML

C#

Page 31: Intro Programming Web

Microsoft Courses 2003 31

Views.Form v = new Form (@"<form Text= Lucky> <vertical> <TextBox name =Number Text = '13'/> <Button name = Start/> <ListBox name = Day Width = 270/> </vertical> </form>");

int luckyNumber = int.Parse(v.GetText("Number"));

Random r = new Random (luckyNumber); for( ; ; ) { string s = v.GetControl( ); if (s==null) break; DateTime luckyDate =

new DateTime(DateTime.Now.Year, r.Next(3,12);, r.Next(1,30);); v.PutText("Day", "Your lucky day will be " + luckyDate.DayOfWeek + " " + luckyDate.ToString("M")); }

Example 2

Page 32: Intro Programming Web

Microsoft Courses 2003 32

Other Views examples

Calculator– Compare with text version

– Separation of concerns

– Internationalization

PhotoAlbum– Fun with pictures

Page 33: Intro Programming Web

Microsoft Courses 2003 33

Debugging

Principles - types of errors:– syntactic

– semantic

– runtime

Exception handling

Robust code– simple logic

– validity checks - also with Assert

– tracing statements

Debugger programs– Text based, or

– GUI, with or without Visual Studio

Page 34: Intro Programming Web

Microsoft Courses 2003 34

Oops in C#

Structs and classes

Well defined collection library– Array class

– Sorted lists

– BitArray

– Queue, Stack, Hashtable

Polymorphism and extensibility– Interfaces and inheritance

Page 35: Intro Programming Web

Microsoft Courses 2003 35

Example - Access control

Page 318

Students, staff, posgrads and tutors have different rules for access to a building. The rules are implemented at the start of each year.

Polymorphic collection over IAccess

Classic simple data update example

Can be much extended e.g. for– serialisation

– images

Page 36: Intro Programming Web

Microsoft Courses 2003 36

Assessment

Quizzes– available online on the website

Exercises– at the end of each chapter - answers will be provided to lecturers

Practicals– worksheets are being devised based on the book

Exam questions– samples will also be provided

Watch for the CD

Page 37: Intro Programming Web

Microsoft Courses 2003 37

References

Peter Drayton, Ben Albahari, Ted Neward, C# in a Nutshell, O’Reilly, 2002

Troelsen, Andrew “C# and the .NET platform” A! press 2001

Damien Watkins, Mark Hammond and Brad Abrams, Programming in the .NET environment, Microsoft .NET Development Series, Addison Wesley, 2002

Not many text books yet, but many trade books

Visual Studio help files

DevHood tutorials -- see http://www.devhood.com

http://www.cs.up.ac.za/rotor -- for the Views project

Page 38: Intro Programming Web

Microsoft Courses 2003 38

Motivation for a different approach

Forward looking– Move to platform independent GUI systems– Integration of XML into languages (cf XEN)

Technical– Rotor does not have a GUI capability– Interesting challenges in Reflection, RegEx etc

Educational– Dissatisfaction with method-oriented or drag and drop GUIs– Separation of concerns

Page 39: Intro Programming Web

Microsoft Courses 2003 39

The Views Notationform: <form> controlGroup </form>controlGroup: <vertical> controlList </vertical>

| <horizontal> controlList </horizontal>controlList: { control }

textItemList: { <item> text </item> }control: controlGroup

| <Button/> | <CheckBox/>| <CheckedListBox> textItemList </CheckedListBox>| <DomainUpDown> textItemList </DomainUpDown>| <GroupBox> radioButtonList </GroupBox>| <Label/> | <ListBox/>| <OpenFileDialog/> | <SaveFileDialog/>| <PictureBox/> | <TextBox/>| <ProgressBar/> | <TrackBar/>

radioButtonList: { <RadioButton/> }

Page 40: Intro Programming Web

Microsoft Courses 2003 40

Handler methodsForm(string spec,params)

The constructor.

void CloseGUI( )

Terminates the execution thread

string GetControl( )

Waits for the user to perform an action

string GetText(string name)

Returns the value of the Text attribute

int GetValue(string name)

Returns the Value attribute from TrackBar, ProgressBar and CheckBox

int GetValue(string name, int index) Returns the status of CheckBox at position index

void PutText(string name, string s)Displays the string in a TextBox or ListBox control.

void PutValue(string name, int v)Sets an integer value associated with a ProgressBar or CheckBox

Essentially five kinds of methods:

construct

close

getControl

get

put

PLUS … direct access

Page 41: Intro Programming Web

Microsoft Courses 2003 41

Handler methodsForm(string spec,params)

The constructor.

void CloseGUI( )

Terminates the execution thread

string GetControl( )

Waits for the user to perform an action

string GetText(string name)

Returns the value of the Text attribute

int GetValue(string name)

Returns the Value attribute from TrackBar, ProgressBar and CheckBox

int GetValue(string name, int index) Returns the status of CheckBox at position index

void PutText(string name, string s)Displays the string in a TextBox or ListBox control.

void PutValue(string name, int v)Sets an integer value associated with a ProgressBar or CheckBox

Essentially five kinds of methods:

construct

close

getControl

get

put

PLUS … direct access

Page 42: Intro Programming Web

Microsoft Courses 2003 42

Object orientation

Extension, polymorphism

Delegation

MM

M

A

B C

F() -- calls M

M

MMM

I

A B C

F() -- calls M

MD

D1(P)D2(Q)D3(R)

F(D1) -- calls P via M

P Q R

A B

Interfaces Inheritance Delegates

C

Page 43: Intro Programming Web

Microsoft Courses 2003 43

GUI building today

GUI BuilderGUI Builder

Add ListenersAdd Listeners

Handlers

widget rendering in the OS

widget rendering in the OS

Visual Studio

C#

Windowswidget calls in a language

Application