programming in visual basic visual basic building blocks

18
PROGRAMMING IN VISUAL BASIC .NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5

Upload: brett-burns

Post on 30-Dec-2015

222 views

Category:

Documents


22 download

DESCRIPTION

Programming in visual basic .net Visual Basic Building Blocks. Bilal Munir Mughal. Chapter-5. In this chapter. Terminology You Must Know Understanding Namespaces Error Handling Introduction to Debugging. Objects and Classes. Objects belong to classes - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming in visual basic   Visual Basic Building Blocks

PROGRAMMING IN VISUAL BASIC .NET

VISUAL BASIC BUILDING BLOCKS

Bilal Munir Mughal

1

Chapter-5

Page 2: Programming in visual basic   Visual Basic Building Blocks

In this chapter2

Terminology You Must Know Understanding Namespaces Error Handling Introduction to Debugging

Page 3: Programming in visual basic   Visual Basic Building Blocks

Objects and Classes3

Objects belong to classes All objects with the same characteristics

(data and functions) constitute one class. A class and an object of that class has the

same relationship as a data type and a variable

Declaring a class doesn’t create any objects, just as mere existence of data type int doesn’t create any variables.

Page 4: Programming in visual basic   Visual Basic Building Blocks

Objects and Classes4

A class serves only as a plan, or a template, or sketch of a number of similar things. It merely specifies what data and what functions will be included in objects of that class.

A class is thus a description of a no. of similar objects.

For instance, HUMAN is a class, and JOHN is its instance (object)

Page 5: Programming in visual basic   Visual Basic Building Blocks

Understanding Members5

In Visual Basic, the definition of a class may include properties, methods, and events. Collectively these parts of an object are known as its members.

The modifiers (such as Public and Private) that precede the member definition are used to control the access to the member. For example, Private members cannot be

accessed from VB code outside the class definition, but Public members can be. (discussed in Chapter 8)

Page 6: Programming in visual basic   Visual Basic Building Blocks

Understanding Method Types6

Methods are units of code within a class that perform a task, but they come in several different forms.

The following are all statements that indicate execution of a method: Invoke a method Call a function Execute a procedure (or subroutine) Fire an event

Each of these statements is a variation on the same thing but has a specific connotation.

Page 7: Programming in visual basic   Visual Basic Building Blocks

Understanding Method Types7

The two most common types of methods are functions and subroutines.

A function has a value, much like a mathematical function. A declaration at the end of the function declaration indicates the type of value returned by the function.

(A function in VB also may have input parameters that control the return value.)

Unlike the function, the subroutine itself does not have a value. However, it can still send values back to the caller using output parameters.

Page 8: Programming in visual basic   Visual Basic Building Blocks

Understanding Method Types8

Private Function GetCurrentTime(Optional ByVal IncludeSeconds As Boolean = True) As String

If IncludeSeconds = False Then

Return Format(Now, "hh:mm")

Else Return Format(Now, "hh:mm:ss")

End If

End Function

---------------------------------------------------------------------------------------------------------------------

Private Sub GetCurrentTime(ByRef CurrentTime As String, Optional ByVal IncludeSeconds As Boolean = True)

If IncludeSeconds = False Then

CurrentTime = Format(Now, "hh:mm")

Else CurrentTime = Format(Now, "hh:mm:ss")

End If

End Sub

Page 9: Programming in visual basic   Visual Basic Building Blocks

Understanding Method Types9

The following lines of code show how to call the function and subroutine. In each case, the output value of the GetCurrentTime method is assigned to a string variable, ReturnValue.

ReturnValue = GetCurrentTime(False) 'Calls the function

GetCurrentTime(ReturnValue,False) 'Calls the sub

Page 10: Programming in visual basic   Visual Basic Building Blocks

Understanding Events10

An event is something external to your program e.g. when the user clicks a button he is causing an event.

Windows applications are inherently event-driven, meaning that the flow of program execution is controlled by the events that occur as the program is running.

The user or system can initiate events in any order. For example the user may click a button, scroll a list box, or close a window.

Page 11: Programming in visual basic   Visual Basic Building Blocks

Selecting an Event Handler11

The event handler is a special type of method that allows you to write code to respond to events.

When an event fires it executes the procedure that handles the event. When the user clicks a button he is causing an event within the program, so there is an associated method that handles that event.

When an object/event combination occurs at runtime and code has been written for that combination, Visual Basic executes the code. Otherwise, Visual Basic ignores the event.

Page 12: Programming in visual basic   Visual Basic Building Blocks

Understanding Assembly & Namespace12

The higher-level groupings (Above classes) :

An assembly provides a fundamental unit of physical code grouping. e.g.   Each time you create an Application, Service,

Class Library, with Visual Basic .NET, you're building a single assembly. Each assembly is stored as an .exe or .dll file.

Page 13: Programming in visual basic   Visual Basic Building Blocks

Understanding Assembly & Namespace13

A namespace provides a fundamental unit of logical code grouping.

Namespaces do not replace assemblies, but.

complement them Examples of NamespacesName Purpose

System.Math Mathematical functions

System.Drawing.Graphics

Drawing lines, and so on

System.Windows.Forms

Windows Forms

System.Net.Sockets TCP Sockets

Page 14: Programming in visual basic   Visual Basic Building Blocks

Error Handling14

Syntax errors: are generally caught while you are typing the code and can be fixed easily because Visual Studio tells you it doesn't understand what you typed.

Logic errors: can usually appear only after you compile and run your code. They can be identified by incorrect operation of the program and are fixed by examining and then correcting an erroneously designed section of code e.g. division by zero

Runtime errors: usually occur due to reasons beyond programmer's control (hence an exception to the normal process and order of code execution) e.g. File not found

Page 15: Programming in visual basic   Visual Basic Building Blocks

Structured Error Handling with Exceptions15

An exception is thrown back to the function that called it. You can write code to catch the exception and then handle the error appropriately.TryFile.Copy("myfile.txt", "yourfile.txt")MessageBox.Show("The file has been copied")

Catch FileMissingError As FileNotFoundExceptionDim strMessage As String

strMessage = FileMissingError.MessagestrMessage = strMessage & "Please make sure this file exists and try again.“strMessage = strMessage & "The program will now exit.“

MessageBox.Show(strMessage, "Error has occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)

Application.Exit()End Try

Page 16: Programming in visual basic   Visual Basic Building Blocks

Unstructured Error Handling with On Error Goto (old syntax)16

Dim Answer As DialogResult = DialogResult.Retry

On Error GoTo FileCopyErrorRetryHere: File.Copy("myfile.txt", "yourfile.txt") MessageBox.Show("The file has been copied") Answer = DialogResult.OK Exit Sub

FileCopyError: Dim strMessage As String If Err.Number = 53 Then strMessage = "Missing file: " & Err.Description Else strMessage = "Error " & Err.Number & ": " & Err.Description End If Answer = MessageBox.Show(strMessage, "Error has occurred") If Answer = DialogResult.Retry Then Resume RetryHere Else Application.Exit() End If

Page 17: Programming in visual basic   Visual Basic Building Blocks

Introduction to Debugging17

To help track down bugs in your program, Visual Basic provides several tools:

Breakpoints tell the debugger that an application should break, pause execution, at a certain point. When a break occurs, your program and the debugger are said to be in break mode. The Immediate window is used to debug and evaluate expressions, execute statements, print variable values, and so forth. It allows you to enter expressions to be evaluated or executed by the development language during debugging.The Output window can display status messages for various features in the integrated development environment (IDE). Watch You can use the Watch window to evaluate variables and expressions and keep the results. You can also use the Watch window to edit the value of a variable or register.

Page 18: Programming in visual basic   Visual Basic Building Blocks

18

Q & A?