VB.NET Language features


Published on

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • VB.NET Language features

    1. 1. Copyright ©2004 and Distribution Rights Held by DotNetTechnology, LLC. This material is exclusively used by DotNetTechnology, LLC Consultants as a teaching aid. Any unauthorized usage including teaching, copying, and redistribution is strictly prohibited. .NET Programming with VB.NET Session 2 Dutch Dasanaike November 2004
    2. 2. Section Objectives <ul><li>After completing this section, you will be able to: </li></ul><ul><ul><li>List the applications that can be built using VB.NET </li></ul></ul><ul><ul><li>List the key terms associated with VB.NET </li></ul></ul><ul><ul><li>List the features of the VB.NET language </li></ul></ul><ul><ul><li>Identify changes between VB6 and VB.NET </li></ul></ul><ul><ul><li>Compile VB.NET applications </li></ul></ul>
    3. 3. What Applications Can Be Built with VB.NET? <ul><li>Two main types of applications can be built using VB.NET. </li></ul>.NET framework VB.NET ASP.NET Web Forms Windows Forms
    4. 4. Other VB.NET Applications <ul><li>Other types of applications can also be built to run on the .NET framework using VB.NET. </li></ul>.NET framework Web Services Windows Services Networking applications Wireless applications
    5. 5. VB.NET Language Features <ul><li>VB.NET contains many important language features: </li></ul><ul><ul><li>Inheritance (object-oriented language) </li></ul></ul><ul><ul><li>Type-safe </li></ul></ul><ul><ul><li>Structured exception handling (Try…Catch) </li></ul></ul><ul><ul><li>Method overloading/overriding </li></ul></ul><ul><ul><li>Parameterized constructors </li></ul></ul><ul><ul><li>Delegates and events </li></ul></ul><ul><ul><li>Attributes </li></ul></ul><ul><ul><li>Multithreading </li></ul></ul>
    6. 6. Inheritance in VB.NET Automobile Truck Car <ul><li>VB.NET contains full support for inheritance. </li></ul><ul><li>Inheritance allows developers to extend an existing code base to add new functionality. </li></ul>Doors Color Start() Stop() LoadBed() Load Trunk()
    7. 7. Type Safety in VB.NET <ul><li>VB.NET is a type safe language: code can only access memory locations it is allowed to access. </li></ul><ul><li>Benefits of type-safe code include: </li></ul><ul><ul><li>Ability to isolate assemblies </li></ul></ul><ul><ul><li>Increased security </li></ul></ul><ul><ul><li>Unwanted side effects eliminated </li></ul></ul><ul><li>VB.NET can require explicit type casting so that types are not automatically converted to other types. </li></ul>
    8. 8. Structured-Exception Handling in VB.NET <ul><li>Structured exception handling is an integral part of VB.NET: </li></ul>Try InsertRecord(‘jdoe@hotmail.com’,‘John’,’Doe’) Catch exp as System.Exception LogError(exp.Message) End Try
    9. 9. Method Overloading in VB.NET <ul><li>VB.NET fully supports method overloading. </li></ul><ul><li>Overloading allows different signatures to be applied to methods with the same name: </li></ul>Public Overloads Function GetCustomer(id as Integer) _ as Customer ‘ Return Customer object based upon id End Function Public Overloads Function GetCustomer(fname as String, _ lname as String) as Customer ‘ Return Customer object based upon first and last name End Function
    10. 10. Method Overriding in VB.NET <ul><li>Method overriding allows child objects to override behaviors found in the parent (base). </li></ul>Automobile Truck Doors Color Start() Stop() Start() Overrides
    11. 11. Parameterized Constructors in VB.NET <ul><li>VB.NET object constructors can have multiple parameters passed in allowing for greater flexibility when creating objects: </li></ul><ul><li>‘ Default constructor </li></ul><ul><li>Public Sub New() </li></ul><ul><li>MyBase.New() ‘Call base class constructor </li></ul><ul><li>‘ Initialization of objects can occur here </li></ul><ul><li>End Sub </li></ul><ul><li>Public Sub New(ByVal fname As String, _ </li></ul><ul><li>ByVal lname As String) </li></ul><ul><li>Me.New() ‘Call default constructor </li></ul><ul><li>Fname = fname </li></ul><ul><li>Lname = lname </li></ul><ul><li>End Sub </li></ul>
    12. 12. Events in VB.NET <ul><li>VB.NET has a robust architecture in place for handling events. As a result, understanding how events work is crucial to developing successful VB.NET applications. </li></ul><ul><li>Events are defined by using the Event keyword: </li></ul><ul><li>Public Event WorkPerformed(amount as Integer, workType as String) </li></ul><ul><li>Events are raised using the RaiseEvent keyword: </li></ul><ul><li>RaiseEvent WorkPerformed(5,”Wrote Code”) </li></ul>Worker WorkPerformed Event Raised
    13. 13. <ul><li>A Delegate type provides a type-safe pointer to a function and can be used to hook an event to an event handler. </li></ul><ul><li>Public Sub WorkPerformed_Handler(amount as Integer, _ </li></ul><ul><li>workType as String) Handles Worker.WorkPerformed </li></ul><ul><li>‘ Handling of event data occurs here </li></ul><ul><li>End Sub </li></ul>Delegates in VB.NET Delegate Worker (WorkPerformed event) WorkPerformed event raised Boss WorkPerformed event handled
    14. 14. Attributes in VB.NET <ul><li>Attributes are descriptive tags that can be added into VB.NET code to provide additional information (metadata) about what the code does: </li></ul><ul><ul><li><WebMethod()> _ </li></ul></ul><ul><ul><li>Public Function GetUserPassword(email as string) as String </li></ul></ul><ul><ul><li>Dim golfer as GolferInfo = new GolferInfo(email) </li></ul></ul><ul><ul><li>Return golfer.Password </li></ul></ul><ul><ul><li>End Function </li></ul></ul>. NET attribute
    15. 15. Multithreading in VB.NET <ul><li>VB.NET developers can now create true multi-threaded applications. </li></ul><ul><li>Threading allows different processes to run separately without tying up the user interface or program. </li></ul><ul><li>Threading functionality is located in the System.Threading namespace. </li></ul>Form runs on one thread. Search runs on a separate thread.
    16. 16. VB6 vs. VB.NET <ul><li>Microsoft strived to attain the following goals with VB.NET: </li></ul><ul><ul><li>Leverage all elements of the .NET framework </li></ul></ul><ul><ul><li>Simplify the language and make it more consistent </li></ul></ul><ul><ul><li>Add new features requested by users </li></ul></ul><ul><ul><li>Make code easy to read and maintain </li></ul></ul><ul><ul><li>Help programmers avoid introducing coding errors </li></ul></ul><ul><ul><li>Make applications more robust and easier to debug </li></ul></ul><ul><li>Although VB.NET resembles VB6 in many ways, there are many differences between the two languages. </li></ul>
    17. 17. Changes Between VB6 and VB.NET <ul><li>File extensions have changed. </li></ul><ul><li>Property Let and Property Set have been simplified. </li></ul><ul><li>The Set keyword is no longer supported for assigning objects. </li></ul><ul><li>Option Private and Option Base are no longer supported. </li></ul><ul><li>Many VB6 data types have been supplanted by CTS data types. </li></ul><ul><li>Variant has been replaced with Object: Cvar has been replaced with Cobj. </li></ul><ul><li>Empty and Null keywords have been replaced with the Nothing keyword. </li></ul>
    18. 18. Changes Between VB6 and VB.NET (cont.) <ul><li>Function/Sub calls must now use parenthesis (). Call is still in VB.NET, but it is no longer recommended. </li></ul><ul><li>Variable scope has been restricted more in VB.NET. (VB6 had procedural scope.) </li></ul><ul><li>On Error Goto , syntax is still supported, but not recommended. </li></ul><ul><li>DAO or RDO Data Binding is no longer supported. </li></ul><ul><li>There is no support for VB6 WebClasses , DHTML applications, or ActiveX documents . </li></ul><ul><li>There is no more regsvr32.exe . .NET code no longer has to be added into the registry to work. </li></ul>
    19. 19. Migrating from VB6 to VB.NET Visual Studio.NET has a built-in VB6 to VB.NET migration tool.
    20. 20. Migrating from VB6 to VB.NET (cont.)
    21. 21. Migrating from VB6 to VB.NET (cont.)
    22. 22. Creating a Hello World Program <ul><li>Now that you’ve seen some of the basics of VB.NET, let’s write a simple program. </li></ul><ul><li>Instructor-led demo: </li></ul><ul><ul><li>Create a Hello World VB.NET application. </li></ul></ul><ul><ul><li>Compile the application using vbc.exe. </li></ul></ul><ul><ul><li>Run the application. </li></ul></ul>
    23. 23. Summary <ul><li>VB.NET provides many new features, including: </li></ul><ul><ul><li>Inheritance </li></ul></ul><ul><ul><li>Threading </li></ul></ul><ul><ul><li>Attributes </li></ul></ul><ul><ul><li>Many more </li></ul></ul><ul><li>Although similar in many ways, there have been many changes between VB6 and VB.NET. </li></ul><ul><li>VB6 applications can be upgraded to VB.NET using the VS.NET upgrade tool. </li></ul><ul><li>VB.NET programs are compiled using the vbc.exe command-line tool. </li></ul>
    24. 24. <ul><li>Both VB.NET and VB6 support true object-oriented programming techniques. </li></ul><ul><ul><li>True </li></ul></ul><ul><ul><li>False </li></ul></ul><ul><li>VB.NET property statements are the same as they were in VB6. </li></ul><ul><ul><li>True </li></ul></ul><ul><ul><li>False </li></ul></ul>Section Review
    25. 25. <ul><li>Individual tasks that can run simultaneously are called _____ in .NET. </li></ul><ul><ul><li>a. Tasks </li></ul></ul><ul><ul><li>b. Processes </li></ul></ul><ul><ul><li>c. Namespaces </li></ul></ul><ul><ul><li>d. Threads </li></ul></ul><ul><li>The glue between an event and an event handler is called a _____. </li></ul><ul><ul><li>a. Derivation </li></ul></ul><ul><ul><li>b. Hook </li></ul></ul><ul><ul><li>c. Event hooker-upper </li></ul></ul><ul><ul><li>d. Delegate </li></ul></ul>Section Review
    26. 26. <ul><li>Overriding allows: </li></ul><ul><ul><li>a. Parent objects to override behaviors of the child </li></ul></ul><ul><ul><li>b. Child objects to override behaviors of the parent </li></ul></ul><ul><ul><li>c. Intermediate objects to override all behaviors in an assembly </li></ul></ul><ul><ul><li>d. None of the above </li></ul></ul><ul><li>Which of the following options best represents the concept of overloading? </li></ul><ul><ul><li>a. Two methods have the same name but different signatures </li></ul></ul><ul><ul><li>b. Two methods have different names but override each other </li></ul></ul><ul><ul><li>c. None of the above </li></ul></ul>Section Review
    27. 27. <ul><li>Benefits of type-safe languages include. </li></ul><ul><ul><li>a. Isolated assemblies </li></ul></ul><ul><ul><li>b. Enhanced security </li></ul></ul><ul><ul><li>c. Elimination of unwanted side-effects </li></ul></ul><ul><ul><li>d. All of the above </li></ul></ul><ul><li>The VB.NET language is a minor upgrade from its VB6 predecessor? </li></ul><ul><ul><li>True </li></ul></ul><ul><ul><li>False </li></ul></ul>Section Review