Neal stublen nstublen@jccc edu
This presentation is the property of its rightful owner.
Sponsored Links
1 / 43

C#: Introduction for Developers PowerPoint PPT Presentation


  • 66 Views
  • Uploaded on
  • Presentation posted in: General

Neal Stublen [email protected] C#: Introduction for Developers. Inheritance Walkthrough. Interfaces. What’s an interface?. It’s like a class with… No member data All methods are public All methods are abstract An interface implies nothing about the implementation behind it

Download Presentation

C#: Introduction for Developers

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Neal stublen nstublen@jccc edu

Neal Stublen

[email protected]

C#: Introduction for Developers


Inheritance walkthrough

Inheritance Walkthrough


Interfaces

Interfaces


What s an interface

What’s an interface?

  • It’s like a class with…

    • No member data

    • All methods are public

    • All methods are abstract

  • An interface implies nothing about the implementation behind it

  • A class can only inherit from one class

  • A class can implement multiple interfaces

    • Interfaces often represent only a portion of a class’ functionality


Example interface

Example Interface

public interface IStreamable

{

bool Read(FileStreaminInput);

bool Write(FileStreaminOutput);

}

public interface IHTMLDisplayable

{

void Render(OutputStreaminStream);

}


Implementing interfaces

Implementing Interfaces


Interface walkthrough

Interface Walkthrough


Generics

Generics


What s a generic class

What’s a generic class?

  • A class definition that doesn’t explicitly declare all of the types it uses internally

  • Allows creation of new types by specifying those internal types later


Generics example average a series of integer values

Generics ExampleAverage a Series of Integer Values


Common generics

Common Generics

  • Collection classes

  • Math classes

  • Enumeration (foreach)

    • IEnumerable<T>


Generic constraints

Generic Constraints

public class MyGeneric<T> where T: class

{

// T is a class (can be assigned null)

}

public class MyGeneric<T> where T: class, IComparable<T>

{

// T implements IComparable interface

}

public class MyGeneric<T> where T: struct

{

// T is a struct

}

public class MyGeneric<T> where T: new()

{

// T has a default constructor

}


Generics example

Generics Example

class Average<T>

{

public void Include(T inValue);

public T Average

{

get { ... }

}

}

Average<int> integerAverage;

Average<double> doubleAverage;


Code organization

Code Organization

  • Multiple classes in a single file

    • Closely related classes (e.g. EventArgs)

  • Nested classes

    • Objects that only exist within the context of another type of object

  • Split a single class over multiple files

    • Forms split Designer code into separate file

    • Possible to split interface implementations into separate files


Namespaces

Namespaces

  • Organize classes into logical groupings

  • Avoid name collisions

  • using <namespace>

  • <namespace>.<ClassName>

    using ns1

    {

    using ns2

    {

    }

    }

    using ns1.ns2

    {

    }


Documenting classes

Documenting Classes


Class libraries

Class Libraries

  • Share class across multiple projects

  • Projects “reference” other projects

  • Move Product, Book, Movie into Product class library


Database programming

Database Programming


Databases

Databases

  • Client-server architecture

    • One server, many clients

    • Server runs Microsoft SQL Server

    • Clients use ADO.NET 4

  • Relational databases

    • SQL (Structured Query Language)


Tables

Tables

  • Tables store data

  • One or more records (rows)

  • A primary key uniquely identifies each row

  • Indexes provide an efficient way to access data based on values in specific columns


Relations among tables

“Relations” Among Tables

  • Key columns are used to relate tables

  • Foreign keys correspond to primary keys in another table

    • One-to-many

    • One-to-one

    • Many-to-many


Table columns

Table Columns

  • Columns are defined by a name and data type

    • bit

    • char, varchar, text

    • datetime, smalldatetime

    • decimal, numeric

    • float, real

    • bigint, int, smallint, tinyint

    • money, smallmoney


Column values

Column Values

  • null – maybe, maybe not

  • Default value

  • Identity column (auto-increment)

  • Constraints


Select

SELECT

  • Select data from a database

    • SELECT column_name,column_nameFROM table_name;

    • SELECT * FROM table_name;


Inner join

INNER JOIN

  • Select all rows from two table where specified columns have matching values

    • SELECT column_name(s)FROM table1INNER JOIN table2ON table1.column_name=table2.column_name;

    • SELECT column_name(s)FROM table1JOIN table2ON table1.column_name=table2.column_name;

  • INNER JOIN is the same as JOIN


Add update delete

ADD, UPDATE, DELETE

  • Add a row to a table

    • INSERT INTO table_nameVALUES (value1,value2,value3,...);

    • INSERT INTO table_name (column1,column2,column3,...)VALUES (value1,value2,value3,...);

  • Update a row in a table

    • UPDATE table_nameSET column1=value1,column2=value2,...WHERE some_column=some_value;

  • Delete a row from a table

    • DELETE FROM table_nameWHERE some_column=some_value;


Online reference

Online Reference

  • Tutorials and references

    • http://w3schools.com/sql


Mmabooks sql command file sqlexpress linqpad

MMABooks .sql Command FileSQLExpressLINQPad


Ado net

ADO.NET


Ado net1

ADO.NET

  • Data providers

    • SQL Server

    • OLE DB

    • ODBC

    • Oracle

  • Third party

    • MySQL

    • SQLite


Components

Components

  • Database server

  • .NET data provider

    • Connection

    • Command

    • Data adapter

  • Dataset


Database concurrency

Database Concurrency

  • Multiple clients accessing data

  • ADO.NET datasets are “disconnected”

  • Optimistic concurrency

    • Check for data changes before writing

    • Throw an exception if data has changed

  • “Last in wins”

    • Data is written by last operation

    • Data may be lost


Avoid concurrency issues

Avoid Concurrency Issues

  • Update and refresh datasets frequently

  • Avoid updating large tables in datasets

  • Only reduces risk!

    • You still must handle the exceptions


Datasets

Datasets

  • Dataset contains a collection of tables

    • Not necessarily the same table from the database

    • May be a subset of columns and rows

    • May be joined to another table

  • Tables contain a collection of columns

  • Tables contain a collection of rows

  • Tables contain a collection of constraints

  • Dataset contains a collection of relations

  • Everything is accessed through object properties


Alternatives to datasets

Alternatives to Datasets

  • You can use Command and Connection objects directly

  • Select, Insert, Update, Delete


Creating datasets

Creating Datasets


Using the data sources window

Using the Data Sources Window


Dataset summary

Dataset Summary

  • Instead of a database, we can pull data from:

    • WCF Data Services

    • Custom objects

    • SharePoint

  • Entity Framework

    • Object-relational mapping framework

    • Maps database table data to C# objects

  • View > Server Explorer


Dataset summary1

Dataset Summary

  • Connection strings can be stored in app.config

  • Allows reuse of the connection string

  • A dataset can be modified in Visual Studio to add tables, columns, etc.

  • Visual Studio generates .xsd schema files for the dataset


Using datasets

Using Datasets


Datagridview control

DataGridView Control

  • Bound control

  • Table view of the dataset

  • Multiple objects are added to the form


Other bound controls

Other Bound Controls

  • Change default data control for table in Data Sources

  • Change default data control for columns in Data Sources


Behind the scenes

Behind the Scenes…

  • Load event is updated for the form

  • Save click event is updated for the binding navigator


  • Login