sql.net consultation lesson 1 dr. gábor pauler, associate professor, private entrepeneur tax reg....

14
SQL .Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location: 1st Széchenyi str. 7666 Pogány, Hungary Tel: +36-309-015-488 E-mail: pauler @ t-online.hu

Upload: martin-dawson

Post on 12-Jan-2016

223 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

SQL .Net ConsultationLesson 1

Dr. Gábor Pauler, Associate Professor, Private Entrepeneur

Tax Reg. No.: 63673852-3-22Bank account: 50400113-11065546

Location: 1st Széchenyi str. 7666 Pogány, HungaryTel: +36-309-015-488

E-mail: [email protected]

Page 2: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

Content of LessonThe rationale behind Object-oriented programming• Objects

• Fields• Methods• Properties• Events

• Features of objects• Encapsulation• Inheritance• Polymorphism

• Classes and instances• Collections• General elements of .Net FCL object hierarchy.Net IDE• Project creation• API• Solution explorer• Properties editor• Project settings• Debugging• Help toolsHome Assignment 1-1: Describe your own object

Page 3: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

Practice 1-3: Describe your career plansShortly you will be called to present your future career plans in 3 minutes (1pts):• Which schools do you still plan to attend on which major?• What is your favourite field in IT?• What is your favourite programming language?• Which elements of the carrer are the most important for you (high salary, flexible

working time, innovative team, intelligent boss, many free time, autonomy in your decisions, level of stress, doing something useful for people, create remarkable innovations, working along very clear and understandable rules)?

…I vividly remember my

favorite teacher, Dr. Gabor, who put

me on the road becoming fucking

rich…

Page 4: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

The rationale behind Object-oriented programming 1To fully utilize capabilities of .Net, we have to be very clear about concept of Object-

oriented programming:• A modern software using graphic user interface has a code of several 10

thousands of rows. No one can really overwiev it as a whole, even it is correctly tabulated (Tabulált) and commented (Kommentezett). Even more impossible to debug (Hibakeresés) it. This phenomena is called the spaghetti-code effect (Spagetti-kód effektus).

Therefore, in 3rd generation programming languages parametered procedures (Eljárások) and functions (Függvények) are introduced. Hierachy of several 100 procedures and functions embedded (Beágyaz) into each other can break up several million lines of code in understandable sized parts.

• Local variables (Lokális változó) of functions and procedures are stored in smaller stack memory (Verem memória), and they are freed after termination of the function to save memory space

• The problem is there are also dynamic-sized, complex-structure data arrays in code (eg. Flight Diary of an airport), stored in more sizeable heap memory (Halom memória), processed by dozens of interlinked functions (eg. InsertNewFlight, DeleteFlight, etc.). As several functions communicate through this array, it can be only global variable (Globális változó)

• Most of the programming languages allow procedures and functions to use a global variable without referencing on it in their parameter list. This is called implicite reference (Implicit hivatkozás). Without this, parameter list of functions should be intolerably long.

• The real problem comes when we would like to re-utilize several pre-written functions and procedures in a new code. They should be carefully checked to declare all global variables used implicitely also in the new code. As tehre is no any explicite link among global variables and functions/procedures reusability of the code is very bad.

Therefore, the concept of object (Objektum) is introduced in 4th generation programming languages:

• It is a container of a complex data structure/record (Rekord) and procedures/functions - collectively called methods (Metódusok) - processing its data, performing a special functionality (eg. showing a window on screen, play music or video, etc.)

Page 5: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

The rationale behind Object-oriented programming 2• Further parts of an object are properties (Tulajdonságok) which are kind of mixture of data

structure and methods: they are data fields (Adatmező) of the object with given name and type, and at reading/writing them given Set/Get methods (Set/Get metódusok) are running automatically. There are read-only (Csak olvasható) properties using only Get method. Properties in programming are useful because:• They create the illusion that the object intelligently reacts on changes in its environment

(eg. if you set BackColor property of a Form object to red, a Set method paints the form background red, and you do not have to know how does it actually happen)

• Behavior of the object can be programmed setting 1 or 2 simple property values in one time, instead of calling functions with long and complex parameter list. However, one object can have several dozens of properties.

• Further parts of an object are events (Események): they are boolean (Logikai típusú) flag variables (Állapotjelző) signaling that a method of the object run/not run, one of the properties set/not set, or there was an effect in the environment influencing the object (eg. the user clicked, double-clicked, right-clicked with the mouse on the object, or performed drag&drop operation, moved the mouse over the object or pressed a key). Event handler methods (Eseménykezelők) can be attached to events responding them:• Events are really useful when we would like to insert our own code in the working

process of an existing object (eg. we would like to play a *.wav file when a form opens up). Doing this, we don’t have to know the several 1000 step process how the window is drawn on the screen. Instead of that we know some marked point of this highly complex algorithm – the events. And we can attach our own code on Open event of the form.

There are 3 important features of objects:• Encapsulation (Kívülről zártság): behavior of an object can be influenced by its environment

only setting its public (Publikus) properties or calling its public methods. There is no access to private (Privát) fields and methods from outside This is important because:• At debugging, it is easier to identify the object which is the source of error• Objects can be easily moved into another code

Page 6: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

The rationale behind Object-oriented programming 3• Inheritance (Öröklés): a more difficult, descendant/child object (Leszármazott/ Gyerek

objektum) can contain the same simple ancestor/parent object (Ős/ Szülő objektum) as an embedded (Beágyazott) part. Child can call parents methods, while parent cannot call methods of child: • Eg. in Windows both Form, Excel Worksheet, Drawing board objects contain a

simple parent object called Canvas. It performs the functionality of drawing the background color/picture resized to childs actual size. Childs add further functionalities to that, eg. drawing tools

Objects embedded into each othe in multiple levels (Szint) form object hierarchy (Objektum hierarchia). • Its function is to break up code in understandable parts and assure easy moving an

reuse objects among several program codes• Polymorphism (Polimorfizmus): child can define a different method on the same name as

parent had, which replaces parent method in child to modify functionality (eg. Drawing board can redefine Canvas object’s Refresh method to allow user to see pencil movement)

As most of the time we need objects in multiple copies in an application (eg. there are numerous Textbox objects on a form), objects are defined in two stages:

• Class (Objektum osztály) declaration: this is a definition of an object type (fields, methods, properties, events), not an actually working code, therefore, it does not consume a ny resources

• Instance (Objektum előfordulás) declaration: this is a working piece of code in a given class type consuming memory and processor time

Instances of a class may form collection (Kollekció): this is an indexed list (Indexelt lista) of objects (eg. Worksheets in an Excel Workbook: WorkSheet1, WorkSheet2, etc.)

• New object can be added to the collection with its .Add method, receiving by 1 increasing index

• It is possible to remove object from collection with its .Remove method. Indices of objects added after the deleted element decrease by 1

• Any object in collection can be accessed directly by its index• As indices of objects may change rapidly because of deletions, objects of collection can be

referenced by their .Name property also, which remains unchanged

Page 7: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

General elements of .Net FCL object hierarchy

All FCL objects have these events:• Init: check conditions of launching• Load: reserve resorces (memory,

processor time share)• Unload: release resourcesAll event handlers of FCL objects have

the following parameters:• Sender: dynamic reference in the

current object to the object, who generated the event. This is important because the event may be handled differently depending on who was the sender (eg. different code should be run depending on which button was pressed on the form)

• E: compressed collection of objects where the event caused changes. It contains both old and new version of changed data (eg. we will use E in server-side code to capture changes on client page caused by user reponse and sent to server page)

Structure of Visual Basic class module:• Place it in a given namespace• Private/public setting of elements• List of ancestor classes• Field and own event declarations• Property declaration• Method declaration• Handling events generated by

other objects

Imports NameOfUpperLevelNameSpaces ‘Which namespace to putPrivate[/Public] Class ClassName ‘Class headerInherits AnciestorClass ‘List of ancestor/parent object ‘Data field declarations--------------------------------- Private[/Public] FieldName As FieldType ‘Simple field Private PropStoreField As PropType ‘Private field to … ‘store property value ‘Own event declarations---------------------------------- Private[/Public] Event EventName … ‘Property declarations----------------------------------- Private[/Public] Property PropName As PropType Get ‘PropName Get method declaration Dim LocalVar As LocalVarType ‘Auxiliary/cycle variable … Return PropStoreField ‘Return property value End Get ‘PropName Set(ByVal NewValue As PropType) ‘PropName set method Dim LocalVar As LocalVarType ‘Auxiliary/cycle variable … PropStoreField = NewValue ‘Set property value to new End Set ‘PropName End Property ‘PropName … ‘Method declarations------------------------------------- Private[/Public] Function MethodName(_ ByVal JustInput As JustInputType,_ ByRef IOParam As IOParamType) As ReturnType Dim LocalVar As LocalVarType ‘Auxiliary/cycle variable RaiseEvent EventName ‘Raise event to signal method run Return … ‘Computed function value End Function ‘MethodName … ‘Handlers for events generated by other classes---------- Private[/Public] Sub SomeClassSomeEventHandler( ByVal Sender As Object,_ ByRef e As System.EventArgs)_ Handles Object1.Event1, Object2.Event2’Can handle events ‘of multiple objects in 1class ’Define a specific type of object on sender Dim SpecificObject As SpecificObjectType = Sender Select Case SpecificObject.ID ‘Who was the sender? Case „Object1ID”: ‘If sender is Object1 … ’Do event handling for Object1 here End Select ‘Who was the sender? End Sub ‘SomeClassSomeEventHandlerEnd Class ‘ClassName

Page 8: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

Practice 1-4: Creating classes verbally(--------------Practice 1-3. Should be completed here---------------)

To practice object-oriented way of thinking you will be shortly called to describe in 3 minutes common, simple utensils (Használati tárgyak) as they were computerized and controlled by class modules (1pts):

• Which data fields, properties and events they have?• What are their methods? (only name, parameter list and return value, no need

for algorithm)You do not have to stick any programming language syntax (Programnyelvi

szintaxis), try to use your own words!Example solution (partial):Class Toaster

Private ActualTemp As SinglePublic Event EjectBreadFunction HeatingOnOff(ByVal ProgrammedTem) As Boolean

End Class ‘ToasterIndividual topics:• Student 1: Lawn-mover• Student 2: Hair dryer• Student 3: Refridgerator• Student 4: Vacum cleaner• Student 5: Microwave oven• Student 6: Gas stowe• Student 7: Coffe machine• Student 8: Mixer machine• Student 9: Bread baker machine• Student 10: Washing machine• Student 11: Dishwasher• Student 12: Chainsaw

Page 9: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

Content of LessonThe rationale behind Object-oriented programming• Objects

• Fields• Methods• Properties• Events

• Features of objects• Encapsulation• Inheritance• Polymorphism

• Classes and instances• Collections• General elements of .Net FCL object hierarchy.Net IDE• Project creation• API• Solution explorer• Properties editor• Project settings• Debugging• Help toolsHome Assignment 1-1: Describe your own object

Page 10: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

Integrated Development Environment (IDE) of .Net 1Creating new project (Projekt):• As a novelty, it can be temporary project

without root directory in temp file • But more frequently it is regular project

stored permanently in a given root directory:• Creating new web application:

File|New|Website|Asp.Net Website| • Location=File system/Server, • Path=MyRootPath, • Language=Visal Basic

• Creating new windows application:File|New|Project|VB|Windows App|

• Name=ProjectName, • Location=RootPath, • SolutionName=ProjectName

Advanced Programming Interface (API):• ASPX/HTML Code Editor: for designing a

HTML page containing ASPX form and its controls. It has two views:• Controls Toolbox: contains list of

• Control Categories: inside that• Control Tools: eg. TextBox

• Design View: for design ASPX form, control tools are pulled from toolbox

• Source View: to edit code of HTML Page, ASPX Form and Event handlers

• VB Code editor:edits event handler’s code• Object selector:jumps into code of an

object/ creates code if it does not exist• Event selector: the same for events• Code window: auto-compiles and auto

aligns/tabulates VB code row by row• Different parts marked with clouring• Bad code is red-wavy underlined

clickclickclickclick

clickclick

clickclick

clickclick

clickclick

clickclick

clickclick clickclick

pullpull

Page 11: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

Integrated Development Environment (IDE) of .Net 2Solution explorer: it shows project file structureProject root directory: its content can be

copied to a web-server to launch site (but this is the primitive method of launching)

• FormName.aspx: form’s HTML/ASPX code- FormName.aspx.vb:VB class module

of ASPX form (every form is a class)• At class header, it always inherits

System.Web.UI.Page object: it references to objects of ASPX form

• Class definition• Event handler codes of ASPX Form

• Bin directory: source codes compiled and external binary codes

• App_Code directory: non page-related source codes (eg. classes for computing)

• App_GlobalResources: files used globally in the project (pictures, sounds, movies)

• App_LocalResorces directory: files used locally on ASPX page

• App-WebReferences directory: references on external web service

• App-Theme directory: appearance property settings of visual objects

• Web.config directory: web-server settingsRunning settings of windows application:Solultions explorer|Select appliaction|Right-

Click|Properties|Application|• AssemblyName: name of runable/portable

packaging• Icon: icon of the application• StartupForm: setting the starting form• SplashScreen: form to show at startup• Assembly: copyrigth, versioning settings

Property editor:• Selecting any object

on the ASPX form, its property values can be set in property editor

• Properties can be shown in alphabetic or categorized order

clickclick

clickclick

clickclick

Page 12: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

Integrated Development Environment (IDE) of .Net 3Debug (Hibakereső) Tools of IDE:• Designtime (Tervezés alatti) debugging:

• Bad syntax code marked with red-wavy underlining ( )

• Intellisense: setting the cursor in code after Dim keyword or object-embedded object marker „.” will result a context-dependent popup list of possible variables/ types/ constants can be included there

• Some of the errors are because of incorrect naming a variable. Global renaming a variable can be done automatically with RightClick on variable name|Rename

• Runtime (Futtatás alatti) error handling: in case of error after pressing Run( )IDE runs on Debug mode (Hiba-kereső üzemmód):• Clicking on right margin of VB class

module code editor,we can toggle bre-akpoints (Töréspont) where run stops

• Moving the mouse over variables/ objects in code, a DataTip comes up:

• Shows actual value of a variable• Shows members of a complex

object type, and their values• We can edit its values or execute

commands on that in Autoswindow• We can Resume running with( )button • We can Step over the current function/

procedure call with ( ) button• We can Stop running and close debug

mode with ( ) button• We can Restart run from beginning ( )

clic

kcl

ick

clic

kcl

ick

clic

kcl

ick

clickclick

clickclickclickclick

clickclick

pullpull

clickclick

Page 13: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

Integrated Development Environment (IDE) of .Net 4Help tools of IDE:• Help button anywhere: gives samples for correction you can insert in code by clicking• Help|How do I… Developer task oriented tematic help with example codes• Snipplets: we can ask for context dependent example codes from a tematic list• Pulling mouse over bad code gives tips to correct tSaving IDE confinguration settings in portable format:• Tools|Options|Environment|Help|Import/Export|Autosave file = FileName.vsSettings

Page 14: SQL.Net Consultation Lesson 1 Dr. Gábor Pauler, Associate Professor, Private Entrepeneur Tax Reg. No.: 63673852-3-22 Bank account: 50400113-11065546 Location:

Home Assignment 1-1: Describe your own object(--------------Practice 1-4. Should be completed here---------------)

Describe your own object designed in Practice 1-4 as Visual Basic class module in Visual Studio. Submit it as word file (3pts)

• Solution: 1-1HomeAssignSolution.doc