More Language Features and Windows Forms

Similar documents
More Language Features and Windows Forms. Part I. Some Language Features. Inheritance. Inheritance. Inheritance. Inheritance.

Chapter 5 Object-Oriented Programming

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Introduction to Programming Using Java (98-388)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Chapter 13: Handling Events

What is Inheritance?

Programming II (CS300)

Create a memory DC for double buffering

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Introduce C# as Object Oriented programming language. Explain, tokens,

Inheritance and Polymorphism

C# and Java. C# and Java are both modern object-oriented languages

Practice for Chapter 11

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Inheritance and Polymorphism

Step 1: Start a GUI Project. Start->New Project->Visual C# ->Windows Forms Application. Name: Wack-A-Gopher. Step 2: Add Content

Object Orientated Analysis and Design. Benjamin Kenwright

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

More on Objects in JAVA TM

Responding to the Mouse

Chapter 6 Introduction to Defining Classes

C++ Inheritance and Encapsulation

Object-Oriented Concepts

Framework Fundamentals

Self-test Java Programming

Object-Oriented Programming

More On inheritance. What you can do in subclass regarding methods:

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Mobile Application Programming. Swift Classes

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Assigned Date: August 27, 2014 Due Date: September 7, 2015, 11:59 PM

You can call the project anything you like I will be calling this one project slide show.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Java Primer 1: Types, Classes and Operators

Chapter 4 Java Language Fundamentals

Lecture 27. Log into Windows/ACENET. Start MS VS and open the PointClassDemo project. Reminder: Project 1 is due on Wednesday. Questions?

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Chapter 10 Classes Continued. Fundamentals of Java

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

ITI Introduction to Computing II

Declarations and Access Control SCJP tips

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

PROGRAMMING LANGUAGE 2

Events. Event Handler Arguments 12/12/2017. EEE-425 Programming Languages (2016) 1

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Chapter 14 Abstract Classes and Interfaces

Click on the empty form and apply the following options to the properties Windows.

Class, Variable, Constructor, Object, Method Questions

Fall Problem Set 1

1 Shyam sir JAVA Notes

Fundamentals of Object Oriented Programming

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

ITI Introduction to Computing II

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Table of Contents Preface Bare Necessities... 17

What are the characteristics of Object Oriented programming language?

Inheritance and Substitution (Budd chapter 8, 10)

Array. Prepared By - Rifat Shahriyar

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Menus and Printing. Menus. A focal point of most Windows applications

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Type Hierarchy. Lecture 6: OOP, autumn 2003

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Capturing the Mouse. Dragging Example

Object Oriented Programming. Java-Lecture 11 Polymorphism

CSCE3193: Programming Paradigms

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

CS260 Intro to Java & Android 03.Java Language Basics

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

COP 3330 Final Exam Review

2. (True/False) All methods in an interface must be declared public.

Java Review Outline. basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for

Radius= 10 cm, Color= Red, Weight= 200g, X= 3m, Y= 5m, Z= 2m. Radius= 10 cm, Color= Blue, Weight= 200g, X= 3m, Y= 5m, Z= 0m

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Quick Guide for the ServoWorks.NET API 2010/7/13

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

What about Object-Oriented Languages?

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

INSTRUCTIONS TO CANDIDATES

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Building Multitier Programs with Classes

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Designing Robust Classes

CS313D: ADVANCED PROGRAMMING LANGUAGE

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

The Chain of Responsibility Pattern

The Chain of Responsibility Pattern. Design Patterns In Java Bob Tarr

Inheritance, and Polymorphism.

CS-202 Introduction to Object Oriented Programming

EL-USB-RT API Guide V1.0

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams

Method Slots: Supporting Methods, Events, and Advices by a Single Language Construct

Transcription:

More Language Features and Windows Forms C# Programming January 12

Part I Some Language Features

Inheritance To extend a class A: class B : A {... } B inherits all instance variables and methods of A Which ones it can access depends on the visibility levels Unlike in Java, method overriding is explicit To allow a method to be overridden, define the method as virtual in the base class To override the method, define the method as override in the derived class To hide a function of the base class, define the method as new in the derived class In the following examples, A a = new A(); B b = new B();

Inheritance class A {... public string m1() { return "a1"; }... } class B {... } b.m1(); // "a1" B has inherited the method m1 from A

Inheritance class A {... public virtual string m2() { return "a2"; }... } class B {... } b.m2(); // "a2" Even though m1 is defined to be virtual, since B does not override it, the definition in A gets executed

Inheritance class A {... public virtual string m3() { return "a3"; }... } class B {... public override string m3() { return "b3"; }... } b.m3(); // "b3" B has overridden m3, so its execution its dynamically dispatched

Inheritance class A {... public string m4() { return "a4"; }... } class B {... public new string m4() { return "b4"; }... } b.m4(); // "b4" B has hidden A s definition of m1 (but this is not dynamic dispatch)

m4 is statically dispatched ((A)b).m4(); // "a4" Inheritance On the other hand... ((A)b).m3(); // "b3"...since b s type at runtime is B

Inheritance To require that a subclass provide an implementation for a method, declare it as abstract An abstract method declaration does not provide an implementation, just the method signature A class with one or more abstract methods must be declared abstract and cannot be instantiated To prevent a class from being extended, mark the class as sealed (like a final class in Java)

Interfaces An interface is a collection of abstract definitions, of events, methods, properties, and indexers (we won t talk about indexers) interface IGoo { string Text { get; set; } void PrintText(); }

Interfaces An implementing class would be declared class MyGoo : IGoo {... } MyGoo would need to provide concrete definitions for all abstract members defined in IGoo A class can implement multiple interfaces

Arrays As in Java, arrays are reference types int[] arr1 = new int[5]; arr1.length is 5 Possible ways to initialize: int[] arr1 = new int[5]{ 1, 2, 3, 4, 5 }; int[] arr1 = new int[]{ 1, 2, 3, 4, 5 }; int[] arr1 = { 1, 2, 3, 4, 5 };

Multidimensional Arrays Unlike Java, there is a syntactical difference between rectangular arrays and jagged arrays

Rectangular Arrays int[,] arr2 = new int[3,2]; arr2.length is 6 arr2.getlength(0) is 3 arr2.getlength(1) is 2 Possible way to initialize: int[,] arr2 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

Jagged Arrays int[][] arr3 = int[3][]; arr3[0] = new int[1]; arr3[1] = new int[4]; arr3[2] = new int[2]; arr3.length is 3 arr3[0].length is 1 arr3[1].length is 4 arr3[2].length is 2

Jagged Arrays Possible way to initialize: int[][] arr3 = new int[][]{ new int[]{ 1 }, new int[]{ 1, 2, 3, 4 }, new int[]{ 1, 2 } };

Boxing / Unboxing Boxing is supplying a value type where a reference type is expected Boxing is implicit in C# object o = 5; Unboxing is supplying a reference type where a value type is expected Unboxing must be explicit in C# int i = (int)o;

Parsing Numbers To parse a string into an integer: int i = Int32.Parse("13"); Parsing other types of numbers is similar Int32.Parse can throw a System.FormatException

Exception Handling Example int i; try { i = Int32.Parse("1a3"); } catch (FormatException) { Console.WriteLine("Error parsing"); i = -1; }

Part II Event Arguments

Event Arguments When an object raises an event, it can send arguments along with it From looking at the event handlers that get attached to control events, we see that these events get packaged with two arguments object sender a reference to the control that fired the event EventArgs e or a subclass of EventArgs depending on the type of event

MouseDown Arguments As an example, let s look at some of the information that is contained in e when the MouseDown event is fired We notice two properties, X and Y that represent the coordinates of the cursor when the mouse was clicked void Form1_MouseDown(object sender, MouseEventArgs e) { label1.text = e.x.tostring() + "," + e.y.tostring(); }

Part III Custom Controls

Custom Controls Here is a short demonstration of how to define a custom control

Create a Windows Control Library project

Choose an existing control class to extend

Define the custom behavior

To use this from a Windows Forms application, you need to add a reference to the compiled.dll from your custom control project

Or if you want to be able to drag-and-drop your custom control from the Toolbox, add your control to it

Part IV Maintaining State

Maintaining State It is a good idea to maintain a state variable What should the type of this be? A good option is to define an enum whose values are all possible states your application can be in The state variable can be an instance of this enum Code to update the GUI can perform a switch on the state variable

A trivial example Consider a simple application to model a traffic light The empty form s background color changes from red to green to yellow according to a fixed timer We start by defining the states of the application, and an instance variable to hold the current state: public partial class Form1 : Form { public enum State { RED, GREEN, YELLOW } private State state = State.RED;... } Now we define a method to transition from state to state...

private void ChangeColor() { switch (this.state) { case State.RED: this.backcolor = Color.Green; this.state = State.GREEN; break; case State.YELLOW: this.backcolor = Color.Red; this.state = State.RED; break; case State.GREEN: this.backcolor = Color.Yellow; this.state = State.YELLOW; break; } }

And finally create a timer to drive state transitions public Form1() { InitializeComponent(); Timer timer1 = new Timer(); timer1.interval = 2000; timer1.tick += new EventHandler(timer1_Tick); timer1.start(); } void timer1_tick(object sender, EventArgs e) { ChangeColor(); }

Maintaining State Since this is such a simple example, it is hardly worth the effort to explicitly define each state in an enum For applications that can be in several complicated states, however, organizing the GUI update logic in this manner can be very useful Also note that since the logic for updating the GUI (changing from red to green to yellow) would be the same no matter how we chose to run the traffic light, we placed that logic in a separate function If we wanted to change the application so that, say, a user clicked a button to change the state of the traffic light color, we would only have to make a call to ChangeColor() in the button s event handler (instead of having to copy-and-paste the color update logic)