58
C#: INTRODUCTION FOR DEVELOPERS Neal Stublen [email protected]

Neal Stublen [email protected]. Class Objectives Develop an understanding of the.NET Framework Gain proficiency using Visual Studio Begin learning

Embed Size (px)

Citation preview

Page 1: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

C#: INTRODUCTION

FOR DEVELOPERS

Neal Stublen

[email protected]

Page 2: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Class Objectives Develop an understanding of

the .NET Framework Gain proficiency using Visual Studio Begin learning the C# programming

language Apply object-oriented concepts within the C#

language Develop basic Windows Forms applications Learn basic .NET database concepts

Page 3: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Suggestions

Install Visual StudioVisual Studio Express 2013 for Windows

Desktop Review each chapter

We won’t necessarily hit every point in classBring back questions

Work projects at end of each chapterMake changes and experiment

Page 4: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Tonight’s Agenda

Overview of .NET Using Visual Studio Designing a Form Object-Oriented Programming Walk through a simple object example Apply what we’ve learned Q&A

Page 5: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

CHAPTER 1

OVERVIEW OF .NET

Page 6: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Windows Applications

Microsoft Windows OS / Intel Platform

Windows Application

File System NetworkDisplay

Page 7: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

.NET Applications

.NET Framework

Class Libraries

Common Language Runtime (CLR)

.NET Application (or "Assembly")

Non-Microsoft OS? / Non-Intel Platform?Microsoft Windows OS / Intel Platform

File System NetworkDisplay

Page 8: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

C#, .NET, and Windows

C# Source Files

.NET "Assembly"

(MSIL)

C# Compiler

.NET "Assembly"

(MSIL)

CLR

"Native" Code

Page 9: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

How does C# compare?

VB.NET, F#, Managed VC++ are other .NET languagesThey all compile into MSIL assemblies that

run on the .NET CLRThey all have their own unique syntax

Java has many similarities.NET class library instead of the Java

support classes Might be considered a "safer" version of

C++.

Page 10: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

CHAPTER 1, PART 2

USING VISUAL STUDIO

Page 11: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Using Visual Studio

Start Visual Studio Create a project

Windows Forms for desktop applicationsWeb Forms for web-based applicationsConsole applications for the command line

The project represents all or part of an application

A solution is a container for multiple projects

Page 13: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Visual Studio Summary

ProjectA collection of files that are used to generate

an application or class library.csproj file extention

SolutionA collection of projects.sln file extension

Open/close a project/solution Projects target a specific version of

the .NET Framework

Page 14: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Visual Studio Summary

Menus and toolbars can be customized Solution Explorer manages project files Form Designer allows us to create and

modify forms Controls are added to a form using the

Toolbox Properties change the appearance

and/or function of a form or control

Page 15: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Visual Studio Summary Tabbed windows can be docked just about

anywhere Tabbed windows can be floating or docked Tabbed windows can be pinned or hidden

Code Editor allows you to edit source code Editing window can be split into two panes

Page 16: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Visual Studio Summary

Settings can be imported and exported We will work with WinForms applications

in this class Projects can be “built” and “run” from

within Visual Studio

Page 17: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

CHAPTER 2

DESIGNING A FORM

Page 18: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Form Design Add controls from the toolbox Set control properties

Name, TextEnabled, ReadOnly, TabOrder, TabStop,

TextAlignAcceptButton, CancelButton, StartPosition

Specify access keys (&) Specify tab order between controls Document Outline View Renaming and saving files

Page 19: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Form Exercise

Create a project named

"InvoiceTotal" in your S: folder Reproduce the following form:

Consider tab order, access keys, etc.

Page 20: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Form Design Summary Control Toolbox Tab Order Properties Window

Name, TextEnabled, ReadOnly, TabOrder, TabStop,

TextAlignAcceptButton, CancelButton, StartPosition

Access keys (&) Document Outline View Renaming and saving files

Page 21: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

CHAPTER 2, PART 2

OBJECT-ORIENTED PROGRAMMING

Page 22: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Object-Oriented Programming

.NET represents everything as an "object"

What objects can we identify in our InvoiceTotal application?Forms, Controls

Page 23: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Object-Oriented Programming

Objects are made up of data and a set of functions that act on that data

What data would be stored in the InvoiceTotal form and its controls?Position, Text

What functions might use that data?

Page 24: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Objects and Classes

An object is represented by a "class" A class has “member” data

Variables A class has “member” functions

Methods

Page 25: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

A class Definitionclass Counter{   

};

Page 26: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

A class Definitionclass Counter{       // “class” is a keyword that tells the // compiler we are defining a new type of        // object.

                                 };

Page 27: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

The class Name (or Type)

class Counter{       // “Counter” is the name of the new class // type.

   

                      };

Page 28: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Member Variablesclass Counter{    private int mValue;

    // We declare member variables that will // hold data for the class.

};

Page 29: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Member Visibilityclass Counter{    private int mValue;

    // “private” is a keyword that tells the // compiler the class member is not visible // to other objects.   

                  };

Page 30: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Member Typeclass Counter{    private int mValue;

    // “int” is a built-in type that tells the // compiler we are defining an integer // value.          

                      };

Page 31: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Member Nameclass Counter{    private int mValue;

    // “mValue” is the name we will use when // referring to this data member.

          

                      };

Page 32: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Member Initializerclass Counter{    private int mValue = 0;

    // (Optional) We can assign an initial value to // the data member.

          

                      };

Page 33: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

A class Constructorclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

                      };

Page 34: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Constructor Visibilityclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // “public” is a keyword that tells the // compiler the class member is visible to // other objects.

};

Page 35: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Constructor Nameclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // “Counter” repeats the class name, which // tells the compiler we are defining a // constructor for the class.           };

Page 36: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Constructor Parameterclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // “int inInitialValue” is a parameter of // the constructor. It is used to set the // initial state of the object.

};

Page 37: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Constructor Bodyclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // The body of the constructor assigns // initial values to any data members of // the class.

};

Page 38: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Assignment Operatorclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // “=” is an assignment operator that assigns // a value to a variable.

};

Page 39: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

A class Methodclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // Increment the counter by one.    public int Increment()    {        return ++mValue;    }};

Page 40: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Method Visibilityclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // Increment the counter by one.    public int Increment()    {        return ++mValue;    }};

Page 41: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Method Return Typeclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // Increment the counter by one.    public int Increment()    {        return ++mValue;    }};

Page 42: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Method Nameclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // Increment the counter by one.    public int Increment()    {        return ++mValue;    }};

Page 43: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Method Bodyclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // Increment the counter by one.    public int Increment()    {        return ++mValue;    }};

Page 44: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Prefix/Postfix Operatorsclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // Increment the counter by one.    public int Increment()    {        return ++mValue;    }};

Page 45: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Code Commentsclass Counter{    private int mValue;

    // Constructor    public Counter(int inInitialValue)    {        mValue = inInitialValue;    }

    // Increment the counter by one.    public void Increment()    {        mValue = mValue + 1;    }};

Counter myCounter = new Counter(0);

Page 46: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Instantiating a classclass Counter{ ...};

Counter myCounter = new Counter(0);Counter yourCounter = new Counter(10);

Page 47: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Instantiating a classclass Counter{ ...};

Counter myCounter = new Counter(0);Counter yourCounter = new Counter(10);

// “new” is a keyword that tells the compiler// we want to create an instance of the class.

// We have created two instances of the Counter// class.

Page 48: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Instantiating a classclass Counter{ ...};

Counter myCounter = new Counter(0);myCounter.Increment();

// We call a method by using the “.” operator on// a class instance.

// All statements are terminated by a semi-colon.

Page 49: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

A CLOSER LOOK AT OUR FORM

Page 50: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

What’s in a form?

A form is defined by a class Controls on the form are member

variables Event handlers are member functions

Page 51: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Form Summary

The Code Editor allows us to expand and collapse blocks of code.

Forms are just objects Forms are created by making changes

to the object’s properties and calling the object’s methods.

The Designer just adds code to the form’s class.

Page 52: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Style Tips

Use of braces, parentheses, etc. Use of indentation and spacing Improves readability

Use code comments Improves maintainability

Page 53: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

MAKING THE FORM DO SOMETHING

Page 54: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Controls and Events

We can perform actions in response to Click events

Control events are handled by form methods

Page 55: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Event Summary

Forms and controls dispatch events Event handlers respond to events

Page 56: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

CREATING AN INVOICECALCULATOR

OBJECT

Page 57: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

Best Practices

The calculator logic should not be part of the form

Place the calculator logic in a class that can be tested and used from many places

Page 58: Neal Stublen nstublen@jccc.edu. Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning

InvoiceCalculator

What methods would you place on an InvoiceCalculator object?

What data members would be part of the InvoiceCalculator object?

Refactoring the calculator