Top Banner
.NET Fundamentals Week 1 November 6, 2003
103

.NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp [email protected] (978) 779-5126.

Dec 26, 2015

Download

Documents

Claire Conley
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

.NET Fundamentals

Week 1

November 6, 2003

Page 2: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Introduction

• Chip Schopp• www.pondviewsoftware.com• [email protected]• (978) 779-5126

Page 3: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Class Schedule – November 6, 2003

• Introductions and other beginning stuff…• Review Class Syllabus / Class Goals• Class Dates • Introduction to .NET • Introduction to C#• Class Exercise(s)• Homework Assignment

Page 4: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

.Net FundamentalsCT-186

Page 5: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Text Books

• Applied .NET Framework Programming by Jeffrey Richter

• Programming C# by Jessie Liberty

Page 6: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Grades

• 5 take-home assignments (75%) • Final Exam (part of last class) (25%)

Page 7: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Prerequisites

Fundamental of Programming (CT100)

or

by permission of the instructor.

Page 8: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

8

Class Goals

Page 9: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Class Goals

• Provide an overview of the .NET Architecture with Major Components• Programming Languages• ADO.NET• ASP.NET• Web Services• XML Integration

Page 10: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Class Goals

Understand the .NET Frameworks as an Object Oriented Strongly Typed Computing

Environment

Page 11: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Class Goals

• Work with various .NET Framework components• Common Language Runtime (CLR)• .NET Framework Class Library (FCL)• Assemblies• Strong Names• The Global Assembly Cache (GAC)• Using the GACUTIL

Page 12: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Class Goals

Introduction to the C# Programming Language

Page 13: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

13

Course Schedule

Week Topics

1 Introduction to .NET Framework, C#

2 Basic C# Windows Forms Programming

3 Types, Objects, and Classes in .NET

4 .NET ADO and XML

5 Exceptions, Intro to ASP.NET, Assemblies

6 Web Services, Final Exam

Page 14: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Class Dates

• Class Dates ( 6, 7, or 8 classes ?)• November 6, 13, 20 – December 4, 11, 18• November 27, Thanksgiving, no class• Other possible dates:

• Monday November 24th

• Monday December 1st

• Monday December 8th or Tuesday December 9th

• Monday December 15th or Tuesday December 16th • Or ?

Page 15: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

15

Effective Learning

• First is Doing• Class exercises

• Coaching• Helping each other

• Homework

• Second is Discussing• Class discussions

• Asking questions• Sharing ideas and information

• Last is Listening

Page 16: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Learning Environment

• Learn something Write some code• Homework Assignments

Write some code

Programming is Fun!

Page 17: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

17

The only dumb questions are the ones not asked!

Page 18: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

18

Time to Address Open Questions

• Begin each class with a time for questions• Questions left open from previous classes• Questions emailed to me during the week• Questions triggered by the homework• Or other ???

Page 19: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Your introductions

• Programming background• Operating systems, languages• Any exposure to .NET ? Other courses ?

• What do you expect to get out of this course ?

Page 20: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

20

Please email me the following…

• Full Name, Nick Name, Student ID #• Home Phone / Work Phone

Page 21: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

21

Email Address (s) [email gives me one]

• Your background in computing/programming• Any experience with .NET, other courses, ?• Your objective or goals for this course• Any material you have a special interest in

covering?• Any issues or questions that you have?• Additional dates you can attend classes on.

Page 22: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

22

Page 23: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

23

What Is the Microsoft .NET Framework?

The Microsoft® .NET Framework is an important new component of the Microsoft Windows® family of operating systems. It is the foundation of the next generation of Windows-based applications that are easier to build, deploy, and integrate with other networked systems.

Page 24: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

The .NET Initiative

“To create rich applications…, businesses must offer a programmatic interface to their business logic services … [which] must be callable remotely using a network like the Internet. Simply stated, the .NET initiative is about connecting information, people and devices”

- Jeff Richter

Page 25: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

25

What Is the Microsoft .NET Framework?

Most consumers will never notice that the .NET Framework is running on their Pocket PC, smartphone, or desktop computer. But they may appreciate the reliability, ease of use, and ability to connect to other systems that the .NET Framework helps bring to computers.

Page 26: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

26

What Is the Microsoft .NET Framework?

The .NET Framework helps software developers and systems administrators more easily build and maintain systems with improvements toward performance, security, and reliability. Here's how.

Page 27: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

27

A New Approach to Building Windows Software

The .NET Framework simplifies Windows software development. It provides developers with a single approach to build both desktop applications—sometimes called smart client applications—and Web-based applications. It also enables developers to use the same tools and skills to develop software for a variety of systems ranging from handheld smartphones to large server installations.

Page 28: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

28

A New Approach to Building Windows Software

Software built on the .NET Framework can be easier to deploy and maintain than conventional software. Applications can be designed to automatically upgrade themselves to the latest version. The .NET Framework can also minimize conflicts between applications by helping incompatible software components coexist.

Page 29: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

29

Benefits of the .NET Framework

• Helps IT professionals better integrate existing systems with its native support for Web services.

• Assists with the deployment of software to both users and Web servers.

• Facilitates the development of software with improved reliability, scalability, performance, and security.

Page 30: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

30

Helps developers be more productive by:

• Making it easier for them to reuse existing code.

• Enabling them to more easily integrate components written in any of the more than 20 supported programming languages.

• Helping them more easily build software for a wide range of devices using same skills and tools.

Page 31: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

The Development Environment – Visual Studio.NET

• Finally , Visual Studio truly becomes an Integrated Development Environment, with multi-language development.

• Runs on Windows 2000, NT, XP and the .NET server family.

• Support for building 32 and 64 bit applications• Usual gamut of wizards, debuggers, linkers…• Plenty of good documentation• Free .NET Framework SDK – compilers,

tools, documentation

Page 32: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

The Development/Runtime platform – the .NET Framework

• The new runtime environment in .NET• Provides a set of base classes for

developers to build on• A unified type system to allow language

inter-operability• This course deals with the fundamentals of

programming this framework

Page 33: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

To summarize, .NET is…

• An OS platform• The .NET Enterprise Servers.NET building

block services• .NET Device Software• The Development Environment – Visual

Studio.NET• The Development/Runtime platform –

the .NET Framework

Page 34: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

The .NET Framework

• Think of what device drivers do in terms of abstracting access by an application

Application

Windows

Scanner

Driver

Mouse

Driver

Page 35: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

The .NET Framework• If we abstract the underlying OS in the

same way…

Application

.NET Framework

Windows

2000

Windows XP

FreeBSD

UNIX

Page 36: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

The .NET Framework

• The .NET Framework introduces a layer of abstraction (and obviously, some overhead) to the OS as we currently know it.

• Is this reason enough to move to it ? Let’s look at the state of Windows development without .NET…

Page 37: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

The current state of affairs• Win32/C programming

• Fairly low-level, not object oriented, interoperability with other languages is hard, memory leaks an issue.

• C++/MFC programming• Large language, complex idioms, prone to abuse,

interoperability with other languages and other C++ implementations still hard, memory leaks still an issue

• Visual Basic programming• Object aware, but not object oriented.• Interop with other languages possible, but ugly.

Page 38: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

The current state of affairs• COM programming

• Allows you cross language integration at the binary level.

• Interface based programming encouraged• Fairly complex to understand and set up• Deployment is registry based, fairly fragile.• Distributed COM was notoriously hard to set up

because of the security issues involved• A Microsoft-only solution

• Java programming• Great language, but no cross language integration.• Interpreted byte-code did have performance issues

Page 39: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

The current state of affairs• Into the mix just discussed, throw in

JavaScript, ASP, HTML, DHTML• Everything we just talked about has it’s

own runtime engine/environment, it’s own set of libraries and it’s own set of development tools.

• In a highly connected world, our solutions need to span languages, machines and network boundaries.

Page 40: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

What the .NET Framework gives us

• Consistent programming model• A simple OO programming model• Application component isolation

• Newer versions can be installed safely• Goes a long way in helping improve DLL Hell

• Simplified installation model• Xcopy, no registry entries• Uninstalls involve just deleting files/folders• Shared components easier to install than COM

components

Page 41: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

What the .NET Framework gives us• Multi-platform support

• The fact that the OS is abstracted away, with code compiled to an intermediate language makes this possible

• Cross language integration• This is in contrast with COM’s cross-language

interoperation. Types can be used between languages.

• This also makes cross language debugging possible.

• Automatic memory management • Reduces the incidence of memory leaks . The

Visual Basic runtime has done this for sometime, but it is now available to all languages

Page 42: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

What the .NET Framework gives us• Code verifiability

• Rich type information gives us the ability to check that code is operating safely

• e.g. Buffer overflows prevented.• Consistent error handling mechanism

• Exceptions used instead of error codes and HRESULTS

• Exceptions have to be handled – cannot ignore them

• Code security• Signing allows the runtime to verify that code has

not been tampered with.• Code access security associates permissions with

code.

Page 43: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

43

Page 44: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

44

Basic Components of the .NET Framework

The .NET Framework consists of two main parts:

•common language runtime•.NET Framework class library

Page 45: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

The heart of the Framework

• The most important elements of the franework are:• The Common Language Runtime (CLR)• The Framework Class Libraries (FCL)

OS Services

Common Language Runtime

Framework Class Library

Data XML …Base

WinFormsASP.NET

Page 46: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

46

Common Language Runtime•Provides the common services for .NET Framework applications.•Programs can be written for the common language runtime in just about every language, including C, C++, C#, and Microsoft Visual Basic®, as well as some older languages such as Fortran. •The runtime simplifies programming by assisting with many mundane tasks of writing code, including memory management—which can be a big generator of bugs—security management, and error handling.

Page 47: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Common Language Runtime (CLR)

• Locates, loads and runs code written in runtime-aware languages.

• Handles object creation, memory management, making method calls, enforces code security and provides a process abstraction.

• Code that targets the CLR is called managed code, while code that uses the native OS services directly is called unmanaged code.

• Languages that can produce managed code (or .NET programming languages)• C#, VB.NET, VC++ with managed extensions,

Fujitsu COBOL.NET, Jscript.NET, Eiffel, Python, Perl…

Page 48: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

48

Managed Module

A Managed Module is a standard windows portable executable (PE) file that requires the

CLR to execute.

Page 49: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Managed Code

Code executed and managed by the Microsoft® .NET Framework, specifically by the .NET Framework's common language runtime.

Managed code must supply the information necessary for the common language runtime to provide services such as memory management, cross-language integration, code access security, and automatic lifetime control of objects.

All code based on Microsoft Intermediate Language executes as managed code.

Page 50: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

50

.NET Framework Class Library

The library includes prepackaged sets of functionality that developers can use to more rapidly extend the capabilities of their own software. The library includes three key components:

• o  ASP.NET to help build Web applications and Web services.

• o  Windows Forms to facilitate smart client user interface development.

• o  ADO.NET to help connect applications to databases.

Page 51: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Framework Class Library (FCL)

• Provides base class libraries available to all .NET programming languages.

• Access to primitive types, file I/O, graphical rendering, crypto, GUI, XML support, etc.

Think of the CLR as abstracting the underlying OS (e.g. Windows), and the FCL as abstracting the OS libraries. (e.g. Win32)

Page 52: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Platforms the CLR runs on• Windows 98, Windows 98 SE, Windows Me• Windows NT 4.0 with SP 6.0a or greater• Windows XP family• Windows .NET Server (Whistler)• Windows PocketPC 2002 (with the .NET

Compact Framework).• FreeBSD Unix (Rotor)• Linux (Ximian’s Mono)

Page 53: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

53

Page 54: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

An introduction to C#

• C# is a language that was developed by Microsoft specifically targeted for the .NET platform.

Page 55: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

C# Features

• No pointers• Automatic memory management• Completely object-oriented• Supports interface-based programming

• Both implementation inheritance and interface inheritance supported

• Support for overloaded operators• Support for aspect-based (attribute-based)

programming• Can only produce managed-code

Page 56: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

56

Let’s try a simple program.

Page 57: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

57

• Create a Working Directory

mkdir k:\week1\hello• Copy corvars.bat

copy

c:\Program Files\Microsoft Visual Studio.NET\FrameworkSDK\bin\corvars.bat

k:\week1\hello\corvars.bat

• Set up environment, run covars.bat

Cd k:\week1\hello

Type corvars.bat <hit return>

Page 58: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Open notepad, create the following file and save it in the k:\week1\hello directory as hello.cs.

class Hello

{

static void Main()

{

System.Console.WriteLine("Hello World");

}

}

Page 59: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

59

• Compile the programk:\week1\hello>csc Hello.cs

• Run the programK:\week1\hello>Hello.exe

K:\week1\hello> Hello World

Page 60: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

60

Page 61: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

61

C#.NET Language Basics

•Types in C#

•Defining integer types

•A Bit About Strings

•Reading From and Writing To The Console

•If Then Statement

•Looping – The For Next Statement 

Page 62: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Primitive Types

C# Type .NET Framework type

bool System.Boolean

byte System.Byte

sbyte System.Sbyte

char System.Char

decimal System.Decimal

double System.Double

float System.Single

Page 63: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Primitive Types (contd.)

int System.Int32

uint System.UInt32

long System.Int64

ulong System.UInt64

object System.Object

short System.Int16

ushort System.UInt16

string System.String

Page 64: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

A word on types

• All types in .NET derive from System.Object• They are provided implementations of ToString()

and GetType()• To get a string with the type of any variable, you

can call <var>.GetType()• Whenever you call Console.WriteLine(obj) the

ToString() method on obj is implicitly called. The default ToString implementation for classes simply returns the name of the class.

Page 65: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

What Are Integers

0, 432, -5, 10000000, -10000000

Integers are whole numbers

Integer variables are stored as signed 32-bit (4-byte) integers ranging in value from -2,147,483,648 through 2,147,483,647.

Page 66: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Defining Integers

int i;

int i, j, k;

int i = 12;

j = i; j is now equal to 12

i = 15;

k = i + j; k is equal to 27

To write an Integer, convert it to a String using:

k.ToString();

Page 67: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

67

A Bit About Strings

Page 68: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

What are strings?

“abcdef” “Abcdef” “aBcdEf” “A23+-/*789” “q”

“John J. Smith”

“How do you do?”

“123 South Street, Calais, ME 04235”

“ Are we there? ”

“” an empty string

Page 69: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

How do we define strings?

string strTmp;

strTmp = “time will tell”;

string strTmp = “time will tell”;

strTmp = Console.ReadLine();

string strTmp2;

strTmp2 = strTmp;

strTmp2 “time will tell”

Page 70: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Concatenating Strings

string strCity = “Calais”;

string strState = “ME”;

string strZip = “04270”;

string strLoc;

strLoc = strCity + “, ” + strState + “ ” + strZip;

strLoc “Calais, ME 04270”

Page 71: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Some String Functions

string strTmp;

strTmp.Trim(); – removes leading and trailing spaces

strTmp.ToUpper(); – converts string to all upper case

strTmp.ToLower(); – converts string to all lower case

strTmp.Length; – returns string length as an integer

strTmp.SubString() – extracts a substring

Page 72: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

String Function Examples

string strTmp = “ Hello World ”;

strTmp.Trim();

strTmp “Hello World”

string strTmp = “Hello World”;

strTmp.ToLower(); “hello world”

strTmp.ToUpper(); “HELLO WORLD”

Page 73: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

String.Length Function

string strTmp;

strTmp = “in the beginning”;

The value of strTmp.Length is 16.

int i;

i = strTmp.Length;

The value of i is 16.

Page 74: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

String.SubString() FunctionString.Substring(startIndex , length );

Parameters (are Integers)

startIndex – Where the substring starts. startIndex is zero-based.

length – The number of characters in the substring.

Page 75: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Substring Examples

string strTmp;

strTmp = “around the world”;

strTmp.Substring(0,6); “around”

strTmp.Substring(11,5); “world”

strTmp.Substring(0,strTmp.Length);

“around the world”

Page 76: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Writing to the Console

Console.WriteLine(String); write with line return

Console.WriteLine(“Hi There”)

C:\>Hi There

C:\>

Console.Write(String); – write with no line return Console.Write(“Hi There”)

C:\>Hi There

Page 77: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Reading from the Console

Console.ReadLine(); – returns a string

string tmp;

Console.Write(“What is your name? ”);

tmp = Console.ReadLine();

Console.WriteLine(“Hi “ + tmp);

C:\>What is your name? Chip

C:\>Hi Chip

C:\>

Page 78: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

if Statement

if (some condition is true)

{

do something in here,

using one or more lines of code…

}

Page 79: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

79

What is difference between = and ==?

= is for assignment of value

String tmpString = “Hello world”;

int i = 12;

== is for equivalence

if (str1 == str2) { some code }

if (str.Length == 0) { some code }

if (str1 != “end”) { some code }

Page 80: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Sample if Statement

string strInput ;

strInput = Console.ReadLine();

if (strInput == “”)

{

Console.WriteLine(“Input required.”);

}

Page 81: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

81

The For Loop

Page 82: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

A Simple For Loop

int i;

for (i = 1; i<10; i++)

{

Console.WriteLine("The value of i is " + i.ToString());

}

The Value of i is 1

The Value of i is 2

The Value of i is 3

The Value of i is 9

The Value of i is 10

Page 83: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Or You Could Reverse It…

int i;

for (i = 10; i>0; i--)

{

Console.WriteLine("The value of i is " + i.ToString());

}

The Value of i is 10

The Value of i is 9

The Value of i is 8

The Value of i is 2

The Value of i is 1

Page 84: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

To Walk Through a String

string tmp = “hello world”;

for (int k = 0; k< tmp.Length-1;k++)

{

Console.WriteLine(tmp.Substring(k,1));

}

Page 85: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

To Walk Through a String Backward

string tmp = "hello world";

for (int k =tmp.Length-1;k>-1;k--)

{

Console.WriteLine(tmp.Substring(k,1)); }

Page 86: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

What About?

What If We Want To Enter More Data?

What If No String Is Entered?

What If The Entered String Is Too Long?

How Do We Know When We Are Done?

Page 87: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

What If We Want To Enter More Data?

Labels

A Label is Defined with a Colon ReturnHere:

goto Statements

goto Statements Direct Program Flow To A Label

goto ReturnHere;

GoTo Statements are Evil and High Risk!!!

Page 88: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

What If No String Is Entered?

Checking for a zero length string.

if (tmpStr.Length == 0)

{

Console.WriteLine(“No String Entered”);

goto ReturnHere;

}

Note: You could also check for tmpStr == “”

Page 89: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

What If The Entered String Is Too Long?

Let’s only work with strings up to 10 characters…

if (strTmp.Length > 10)

{

strTmp = strTmp.SubString(0,10);

}

Page 90: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

How Do We Know When We Are Done?

Let’s check for the string “end” to end the program…

if (strTmp == “end”)

{

return;

}

Note: return tells the program to exit the subroutine, which in this case will end the program.

Page 91: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

91

Comments in C#

• Both /* … */ and // can be used for comments.

• VS provides comment/uncomment selections.• Use the menu bar, or Ctrl-K Ctrl-C for comment

and Ctrl-K Ctrl-U for uncomment

Page 92: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

92

Page 93: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

93

Now let’s redo hello.cs as a Visual Studio project.

Page 94: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Visual Studio.NET• The newest version of Visual Studio• Multiple language development finally in one

environment.• Can program in

• Visual C#• Visual Basic.NET• Visual C++.NET

• Can build• Desktop console and GUI applications• Web services• ASP.NET Web applications• Mobile applications

Page 95: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

To get Visual Studio.NET

• You need to purchase either:• An MSDN subscription• A copy of Visual Studio.NET

• Academic editions are available (in or through the bookstore ?)

Page 96: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Visual Studio.NET• Start up Visual Studio.NET• Open a new project by either:

• Clicking on the New Project button on the Start Page OR

• File-> New-> Project from the Menu Bar

• In Visual C# Projects, create a Console Application

• Implement the Main() method• Notice you now have IntelliSense

• Add the Console.WriteLine… line of code.• Compile using the Build menu.• Run using the Debug menu

Page 97: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Your code should look like this…using System;

namespace HelloVS{

/// <summary>/// Summary description for App./// </summary>class App{

/// <summary>/// The main entry point for the application./// </summary>[STAThread]static void Main(string[] args){

System.Console.WriteLine("Hello World");}

}}

Page 98: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

98

Class Exercise (time permitting)

Using the Visual Studio.NET write an interactive console program to accept information from the keyboard and then format and display the information back. It might be a person’s name and address or a variable list of favorite pets including name and type of animal or whatever. Focus on formatting the data, looping to accept multiple entries, testing for missing information and also testing for an at end condition. A sample, somewhat simplified example is in the Class Collections zip file on the web (i.e. www.PondviewSoftware.com).

Page 99: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

99

Page 100: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

Homework Part 1Send Me An Email [email protected], include the

following: 

1. Full Name, Nick Name, Student ID #

2. Home Phone / Work Phone

3. Email Address (s) [email gives me one]

4. Your background in computing/programming

5. Any experience with .NET, other courses, ?

6. Your objective or goals for this course

7. Any material you have a special interest in covering?

8. Any issues or questions that you have?

9. Which additional dates you would attend classes on.

Page 101: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

101

Homework Part 2

C#.Net Programming

Homework Assignment – Week 1

Assignment Due: November 13, 2003 5:30 PM

 

 

Page 102: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

102

Write a C#.NET Console Application, which performs the following:

1. Accepts a first name string, a middle name string, and a last name string from the console. The first name and last name are required. The middle name is optional.

2. Concatenates the two or three fields together creating a full name string.

3. Truncates the full name to 20 characters if the length of the full name is longer than 20 characters.

4. Provides the capability of displaying the full name either vertically or horizontally and forward or backward as desired.

5. Allows the full name field to be displayed in either of these four ways as many times as desired.

6. Allows the user to go back to the top and start over, entering a new name.

7. Terminates gracefully. Note:  There was enough information discussed in today’s class to complete

this assignment. Feel free to use any additional commands, structures, or functions you wish.

Page 103: .NET Fundamentals Week 1 November 6, 2003. Introduction Chip Schopp  chipschopp@comcast.net (978) 779-5126.

103