1 / 18

Programming in visual basic Visual Basic Building Blocks

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

brett-burns
Télécharger la présentation

Programming in visual basic Visual Basic Building Blocks

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in visual basic .netVisual Basic Building Blocks Bilal Munir Mughal Chapter-5

  2. In this chapter • Terminology You Must Know • Understanding Namespaces • Error Handling • Introduction to Debugging

  3. Objects and Classes • 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.

  4. Objects and Classes • 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)

  5. Understanding Members • 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)

  6. Understanding Method Types • 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.

  7. Understanding Method Types • 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.

  8. Understanding Method Types Private Function GetCurrentTime(Optional ByValIncludeSeconds 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(ByRefCurrentTime As String, Optional ByValIncludeSeconds As Boolean = True) If IncludeSeconds = False Then CurrentTime = Format(Now, "hh:mm") Else CurrentTime = Format(Now, "hh:mm:ss") End If End Sub

  9. Understanding Method Types • 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

  10. Understanding Events • 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.

  11. Selecting an Event Handler • 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.

  12. Understanding Assembly & Namespace 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.

  13. Understanding Assembly & Namespace • A namespace provides a fundamental unit of logical code grouping. • Namespaces do not replace assemblies, but. complement them

  14. Error Handling • 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

  15. Structured Error Handling with Exceptions 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. Try File.Copy("myfile.txt", "yourfile.txt") MessageBox.Show("The file has been copied") CatchFileMissingError As FileNotFoundException Dim strMessage As String strMessage = FileMissingError.Message strMessage = 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

  16. Unstructured Error Handling with On Error Goto (old syntax) Dim Answer As DialogResult = DialogResult.Retry On Error GoTo FileCopyError RetryHere: 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

  17. Introduction to Debugging 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.

  18. Q & A ?

More Related