introduction to c# with a gis focus with python examples to help with transition provide a c#...

63
Introduction to C# • With a GIS focus • With Python examples to help with transition • Provide a C# foundation for ArcGIS customization with ArcObjects

Upload: shawna-homsey

Post on 01-Apr-2015

241 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Introduction to C#

• With a GIS focus• With Python examples to help with transition• Provide a C# foundation for ArcGIS

customization with ArcObjects

Page 2: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Why customize ArcGIS ? Workflow.

ArcGIS application like ArcMap does not do EXACTLY what you want to do “out-of-the-box”.ArcObjects provides the programming framework customize ArGIS applications to do exactly what you want.

Page 3: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Extending ArcGISAdd-ins, C#, & ArcObjects

• Adding toolbars, buttons, tools, etc.• ArcGIS 10.1+ provides tools for making Python

Add-ins ArcGIS 10.0 did not (Free webinar)

• ArcObjects SDK for the Microsoft .NET Framework provides access to more than Python

Page 4: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Customization exampleCapture point features at midpoint of drawn line automatically calculatingand storing line orientation and providing easy tools for selecting type andadding dip angle.

Page 5: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

ESRI Customization Examples

• 200+ ESRI examples in Samples folder i.e. C:\Program Files\ArcGIS\DeveloperKit10.2\Samples\ArcObjectsNet

Page 6: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

From Python to C#

• IDE Solutions, Projects• Runtime environment• Code containers• Core language and framework• Types• Variables, operators, expressions, statements• Functions• Scope• Booleans• Flow control – branching and looping• Standard modules and libraries

Page 7: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

IDE ProjectsPyScripter psproj• One or more related files

referenced by psproj file

Visual Studio sln and csproj• Solution (sln) can contain

one or more Projects (csproj) and other files

• Project contains one or more related files

Project is optional Solution/Project(s) not optional

Page 8: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Visual Studio IDE

Creating .NET Solution and Projectswith Visual Studio 2010

Page 9: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Projects …

… because any application worth building usually has more than one .cs file and other files (e.g. images, cursors, etc.)

Page 10: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

10/50

Solutions …… because Projects must be part of a Solution

Projects (.csproj)*

Visual Studio Solution (.sln)

Page 11: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Folder structure for application development

Download …

Page 12: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

12/50

Creating a src folder

- Move files up one level into src - Delete solution folder

- Create a New Project … Blank Solution, in src- Save - Exit Visual Studio

Solutions can include code from other CLS languages (e.g. VB.NET)

Page 13: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

13/50

Adding a Project to a Solution

Page 14: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Project templates

Most common Project templates

EXE

EXE

DLL ESRI Add-ins and Extensionsare compiled into DLL’s

Page 15: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Sln and Project code

Page 16: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

StartUp Project• StartUp Project is the first project to run when the application is run

• Class Library Projects cannot be set as the StartUp Project

Page 17: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Runtime requirements

Python• PY file run from command

line or double click• Can be run interactively

>>> x = 1• Requires Python interpreter

(Python.exe) to run• Requires appropriate

version of Python.exe

C#• EXE run from command line

or double click• Cannot be run interactively

No >>> here.• Compiled assembly (EXE)

can be run directly• Requires .NET Framework

that can support assembly

Page 18: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Runtime – from code to CPU

Python• Python file run at command

line or double-clicked• Interpreted by Python.exe

into byte code• Byte code transformed into

machine language by Python runtime

• CPU runs machine language program

C#• Code files in Visual Studio

Solution/Project(s) compiled to Common Intermediate Language (CIL) Assembly (EXE)

• EXE run from command line or double-clicked

• Common Language Runtime (CLR) transforms CIL into machine language

• CLR and CPU run program

Page 19: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

19/50

.NET Framework OverviewCLS (Common Language Specification)

C#, VB.NET, Managed C++, J#

Framework Class Libraries

CLR (Common Language Runtime)

Windows OSWin32, IIS, MSMQ

Compilers

CIL (Common Intermediate Language)

UIWeb FormWinForm

WPF

ServicesWCF

WorkFlow

Data AccessADO.NET

Entity FramworkLINQ to SQL

Framework Base Classes & CTS (Common Type System)IO, Collections, Security, Threading, etc.

CLS specifes rules .NETlanguages must follow to create.NET code that will reference the.NET Framework Class Libraries(.NET DLL Assemblies) and.NET CTS.

.NET compilers will compile

.NET code into CIL stored in

.NET assemblies (EXE or DLL)

.NET CLR will perform JIT (Just-In-Time compilation) tonative machine code and ensure that the .NET assembly runs properly and securely.

Page 20: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Statements

• Code is interpreted or compiled one statement at a time

• A statement can be composed of one or more expressions

• Expressions are composed of one or more of the following: variables, operators, literals, function calls, and types

Page 21: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Statements

Python• No end of statement marker• Indentation is critical

msg = “Multi-line statement \ needs line continuation char”

C#• ; marks end of statement• Indentation is cosmetic

msg = “Multi-line statement does not need continuation char”;

Page 22: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

4

1

Statement context

PythonStatements can be run outside of functions

C#Statements cannot be run outside of functions Solution

Project Namespace Class Method

Statement

4 keywords4 names1 literal

1 keyword1 literal

Page 23: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Keywords

Python• ~30

C#• ~80

Page 24: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Types

Python• str

• int

• float

• bool (True or False)

C#• string, char

• sbyte, byte, short, ushort, int, uint, long, ulong

• float, double, decimal

• bool (true or false)

Page 25: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

C# Types – Value types

true, false

0 – 255

‘a’, ‘b’, ‘c’, ‘\t’, etc.

28-29 significant digits, ± 7.9 x 1028

15-16 significant digits, ± 5.0 x 10324 to ±1.7 x 10308

List of constants of same type - byte, sbyte, short, ushort, int, uint, long, or ulong

7 significant digits, ±3.4 x 1038

-2,147,483,648 to 2,147,483,647

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

-128 to 127

-32,768 to 32,767

Group of related value types

0 to 4,294,967,295

0 to 18,446,744,073,709,551,615

0 to 65,535

Page 26: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

C# Types – Reference types

Value types require a static amount of memory (e.g. byte requires 1 byte, int uses 4 bytes, etc.) and directly contain their data.

Reference types require dynamic amounts of memory (e.g. “Bob” and “Bobby”).

Stack memoryStores values of value types and memory location of reference types in Heap Memory

Heap memoryStores reference types

Page 27: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Most commonly used types

string

int

double

bool

char

Page 28: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Classes

PythonSyntax:class name([baseClass]): [properties (data)] [methods]

C#Syntax:[scope][static] class name : baseClass { [properties] [methods] [events] etc.}

Page 29: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Variables

Python• Variable is case-sensitive

name associated with a value of any type

• Type defined after assignment

C#• Variable is case-sensitive

name associated with a value of a specific type

• Type defined in variable declaration statement

Page 30: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Variable declaration/initialization

• <type> <variableName> {= initialValue};

int x; // Declare variablex = 3; // Initialize variable

int x = 3; // Declare and initialize

int x, y, z; // Not recommended // Declare on separate lines

Page 31: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Rules for variable namingSame for Python and C#

Variable name syntax:– _ or letter + any number of letters, digits, or _– i.e. cannot begin with a number

Use names that describe the data they are associated with – e.g. “recordCount” is better than “n”

Use lowercase letter for first character, upper case letter for other words in name. This is called camelCase.

Cannot be a keyword of the language

Page 32: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Operators

Python• +, -, *, /, % same as C#• 2**3 is 23

C#• +, -, *, /, % same as Python• Math.Pow(2,3)

Output:00.50.5

Page 33: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

C# Operators and Expressions<variable> = <operand> <operator> <operand><variable> <operator> <operand><variable> <operator>

// x = x + 1, z = x + 1// x = x + 1, z = x// x = x - 1, z = x – 1// x = x - 1, z = x

p. 46, 47 in Watson et al.++x add before expression evaluatedx++ add after expression evaluated

Page 34: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Operator Precedence

Same for Python and C#Parentheses/BracketsExponentiation Muliplication – DivisionAddition - Subtraction

Output: 17

Page 35: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Operator polymorphism

Python+ addition+ string concatenation* multiplication* string repetition

C#+ addition+ string concatenation

Page 36: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Comments

Python C#// Comment out single line/* Comment out multiple lines */

comments with //

Page 37: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

C# Comments Examples//, ///, /* */

“The best kind of comments are the ones you don't need. Allow me to clarify that point. You should first strive to make your code as simple as possible to understand without relying on comments as a crutch. Only at the point where the code cannot be made easier to understand should you begin to add comments.” From http://www.codinghorror.com/blog/archives/000749.html

Page 38: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Functions – Syntax for Definition

PythonSyntax:def name([params]): statements [return value]

C#Syntax:[scope][static] type name ([params]){ statements [return [value];]}

scope keywords include public, internal (default), protected, protected internal, privateIf static is included, function belongs to class instead of instance of classtype refers to type returned by functionCan be any type including user-defined types. Is “void” if it returns nothingparams include type (e.g. string param1)

Page 39: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Functions – Define and callSyntax:[scope][static] type name ([params]){ statements [return [value];]}

Define:

Call:

Page 40: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Scope

“Visibility” of variables, functions, classes, etc in a program.

Class:[scope][static] class name

Function:[scope][static] type name ([params])

Variables:[scope][static] type name [= value];

Page 41: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Scope

Python C#

“Visibility” of variables, functions, classes, etc in a program.

Page 42: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

42 of 29

Scope Reviewpublic

internal (default)

protected

privateEntire solution

Assembly only

Derived classes

Class only

More …

More …

More …

More …

Page 43: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Booleans

Python C#True value: trueFalse value: false

Page 44: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Boolean Operators

Pythonor, and

C#|, ||, &, &&

Comparison operators are the same for Python and C#

Page 45: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Flow control - Branching

Page 46: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Flow control - Branching

Pythonif condition: # Statements for true # conditionelif condition: # Statements for alternate # true conditionelse: # if all above conditions # are false

C#if (condition){ // Statements for true // condition}else if (condition){ // Statements for alternate // true conditionelse{ // if all above conditions // are false}

Page 47: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Branching with if

Page 48: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Branching with switch

Page 49: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Replacing simple if … else

• If the “if … else” is being used to simply set a boolean variable, set the variable using a boolean assignment statement

Replace this

withor

Page 50: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Ternary (conditional) Operator ?

• Used to set a non-bool type variable based on a condition

Replace this

withor

Syntax: variable = condition ? true_expression : false_expression

Page 51: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Flow control - Repetition

Previous code

Code to repeat

Is condition true?

Next code

Loop

Page 52: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Flow control - Repetition

Pythonfor target in list: # Statements executed for # each item in list.

# Number of iterations # controlled by number of # values in list

# target is assigned each # value in list

C#foreach (type target in list){ // Statements executed for // each item in list.}

for (type target = first_value; condition_to_continue; change_target_variable){ // Statements executed // while condition to // continue is true}

Page 53: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Looping with for

Page 54: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Looping with while

Page 55: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Looping with do

Page 56: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Interrupting loops

• continue– Continue with next

target value• break

– Exit from loop• return

– Exit from loop & containing function

Page 57: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Standard modules/libraries

Python• Standard modules

– sys– os– os.path– urllib– xml– shutil– glob– etc.

C#• Framework Class Libraries

(DLL) organized into Namespaces like– System– System.Collections– System.IO– System.Web– System.XML– System.Data– etc.

Page 58: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Using modules/librariesPython C#

C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Xml.dll

Page 59: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

using keyword

System namespace is part of mscorlib (Microsoft Common Object Runtime Library)

All C# programs require a reference to mscorlib

Page 60: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

60/50

Class libraries, namespaces, classesClass Libraries are stored ina .NET assembly (DLL)

Namespaces are a way ofnaming a group of relatedclasses and other types

Namespaces can be nested.

Namespaceicon

Page 61: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

61/50

.NET Framework OverviewCLS (Common Language Specification)

C#, VB.NET, Managed C++, J#

Framework Class Libraries

CLR (Common Language Runtime)

Windows OSWin32, IIS, MSMQ

Compilers

CIL (Common Intermediate Language)

UIWeb FormWinForm

WPF

ServicesWCF

WorkFlow

Data AccessADO.NET

Entity FramworkLINQ to SQL

Framework Base Classes & CTS (Common Type System)IO, Collections, Security, Threading, etc.

CLS specifes rules .NETlanguages must follow to create.NET code that will reference the.NET Framework Class Libraries(.NET DLL Assemblies) and.NET CTS.

.NET compilers will compile

.NET code into CIL stored in

.NET assemblies (EXE or DLL)

.NET CLR will perform JIT (Just-In-Time compilation) tonative machine code and ensure that the .NET assembly runs properly and securely.

Page 62: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Keys to remember

F1 – Context sensitive Help (If cursor is on keyword, help on keyword provided)

F6 – Build code (compile) without runningF5 – Build code (compile) and runF11 – Step through code, into called functionsF10 – Step through code, not into called

functionsShift-F5 – Stop running code F9 – Toggle Breakpoint at current statement

Page 63: Introduction to C# With a GIS focus With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects

Links for VS 2010 and C#

• “Quick Tour” of the VS 2010 IDE

• Beginner Developer Learning Centre

• Csharp-station.com tutorial