VB.NET Language Fundamentals


Published on

Published in: Technology, Education
1 Comment
  • For Siebel CRM Online Training Register at http://www.todaycourses.com
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • VB.NET Language Fundamentals

    1. 1. Section 4: VB.NET Language Fundamentals <ul><li>VB.NET Statements </li></ul><ul><li>Data Types </li></ul><ul><li>Operators in .NET </li></ul><ul><li>Declaring and Assigning Variables </li></ul><ul><li>Declaring Constants </li></ul><ul><li>Static Variable Types </li></ul><ul><li>Arrays in VB.NET </li></ul><ul><li>Conditional Statements </li></ul><ul><li>Looping Statements </li></ul>
    2. 2. Section Objectives <ul><li>After completing this section, you will be able to: </li></ul><ul><ul><li>Explain VB.NET statements </li></ul></ul><ul><ul><li>List the fundamental language features of VB.NET </li></ul></ul><ul><ul><li>Identify supported .NET data types </li></ul></ul><ul><ul><li>Explain how to create variables, constants, and arrays </li></ul></ul><ul><ul><li>Explain conditional statements </li></ul></ul><ul><ul><li>Explain looping statements </li></ul></ul>
    3. 3. VB.NET Statements <ul><li>VB.NET programs are written using statements such those shown here: </li></ul><ul><li>Dim custCount as Integer = 3 </li></ul><ul><li>custCount += 1 </li></ul><ul><li>Statements represent a single VB.NET operation and can be composed of several elements including types, VB.NET keywords, etc. </li></ul><ul><li>Statements that span more than one line require the _ continuation character: </li></ul><ul><li>Dim custCount as _ </li></ul><ul><li>Integer = 3 </li></ul>VB.NET statement VB.NET statement
    4. 4. Commenting VB.NET Statements <ul><li>Comments allow documentation to be added into code and/or statements to be commented out. </li></ul><ul><li>VB.NET statements can be commented by using the apostrophe (‘) character: </li></ul><ul><li>‘ Dim custCount as Integer = 3 </li></ul>
    5. 5. Data Types <ul><li>Data types in .NET are based upon the CTS, which is part of the CLR. </li></ul><ul><li>CTS types are either value or reference types. </li></ul>Intermediate language runtime compilers CTS Execution support, runtime functions Security functionality Boolean Byte Char Double Int16 Int32 Etc. Garbage collection, stack walker, Code manager Class loader, memory layout
    6. 6. Value Types <ul><li>They hold data within their own memory allocation. (They have their own copy of data.) </li></ul><ul><li>Any assignment of a value type creates a new copy of the data. </li></ul><ul><li>Examples of value types: </li></ul><ul><ul><li>Integer, Boolean, Char, Date, Enum, Structure </li></ul></ul>i = 4 Dim i as Integer = 4
    7. 7. Reference Types <ul><li>They point to another memory location where the data is held. (They do not have their own copy of data.) </li></ul><ul><li>Any assignment of a reference type creates a reference to the data. (A new copy is not made.) </li></ul><ul><li>Examples of value types: </li></ul><ul><ul><li>String, Array, Class </li></ul></ul>name “ Hello” Dim name as String = “Hello”
    8. 8. Data Types in VB.NET Reference types Value types Object String Array Foo(4) Class Foo Short Char Float Long Integer Byte Boolean Double Enum Enum Foo Structure Foo
    9. 9. Operators in .NET <ul><li>Operators allow tasks such as arithmetic, comparisons, and concatenation to be performed in a statement. </li></ul><ul><li>VB.NET contains several new operators. For example: Dim name as String = “John” name += “ Doe” </li></ul><ul><li>Dim i as Integer = 40 i -= 3 </li></ul>
    10. 10. Arithmetic and Assignment Operators Exponentiation assignment ^= Equality = Modulo division Mod Exponentiation ^ Integer division Floating-point division / Multiplication * Subtraction - Addition + Operation Operator Concatenation and assignment &= Integer division assignment = Floating-point division assignment /= Subtraction assignment -= Multiplication assignment *= Addition assignment += Operation Operator
    11. 11. Comparison Operators And And String concatenation + Concatenation & Tests to see if obj1 and obj2 refer to same object Is Compares string to pattern Like Greater than or equal to >= Less than or equal to <= Greater than > Operation Operator Or Or Inequality <> Negative for booleans Not Returns the type of an object GetType Returns address of a given procedure AddressOf Xor (bitwise operations) Xor Less Than < Operation Operator
    12. 12. Demonstration <ul><li>Value vs. reference types </li></ul><ul><li>Operators </li></ul>
    13. 13. Declaring and Assigning Variables <ul><li>VB.NET types can be declared and assigned with one statement: </li></ul><ul><li>Dim x as Integer = 45 </li></ul><ul><li>Dim name as String = “John Doe” </li></ul><ul><li>Dim currentDateTime as DateTime = DateTime.Now </li></ul><ul><li>Multiple types can be declared on the same line in VB.NET. In the following code, all types are assigned an Integer type: </li></ul><ul><li>Dim x,y,z as Integer </li></ul>
    14. 14. Variable Scope and Visibility <ul><li>Scope refers to how long a variable is accessible. </li></ul><ul><li>Types of scope: </li></ul><ul><ul><li>Class and structure level </li></ul></ul><ul><ul><li>Function and subroutine level </li></ul></ul><ul><ul><li>Block level </li></ul></ul><ul><li>Variables can have different types of visibility, including public and private. </li></ul>
    15. 15. Understanding Scope <ul><li>Public Class Customer </li></ul><ul><li>Dim CustomerName as String </li></ul><ul><li>End Class </li></ul><ul><li>Public Class Customer </li></ul><ul><li>Public Function GetCustomer() as String </li></ul><ul><li>Dim CustomerName as String = “John” </li></ul><ul><li>Return CustomerName </li></ul><ul><li>End Function </li></ul><ul><li>End Class </li></ul>Scope global to class Scope local to function
    16. 16. Function vs. Block Scope <ul><li>Public Function GetCount() as Integer </li></ul><ul><li>Dim totalCount as Integer = 0 </li></ul><ul><li>Dim i as Integer </li></ul><ul><li>For i = 1 to 50 </li></ul><ul><li>Dim x as Integer = i + 5 </li></ul><ul><li>totalCount += x </li></ul><ul><li>Next </li></ul><ul><li>Return totalCount </li></ul><ul><li>End Function </li></ul>Scope local to function Scope local to looping block
    17. 17. Declaring Constants <ul><li>Constants are used to hold data that does not change. They can help make code easier to read and maintain. </li></ul><ul><li>Constants can be assigned an initial value, but cannot be changed like a variable can. </li></ul><ul><li>VB.NET constants use the Const keyword: </li></ul><ul><li>Public Const DepartmentID as Integer = 158945699 </li></ul><ul><li>The scoping rules that apply to variables also apply to constants. </li></ul>
    18. 18. Static Variable Types <ul><li>Variables declared using the Static keyword stay in memory until the program finishes executing: </li></ul><ul><li>Function Total() as Integer </li></ul><ul><ul><li>Static CustomerTotal as Integer = 0 </li></ul></ul><ul><ul><li>CustomerTotal += 1 </li></ul></ul><ul><ul><li>Return CustomerTotal </li></ul></ul><ul><li>End Function </li></ul>CustomerTotal stays in memory even after the Total() function is exited.
    19. 19. Converting Between Types <ul><li>VB.NET relies upon the CType function to convert between different types: </li></ul><ul><li>Dim age as Integer = 45 </li></ul><ul><li>Dim stringAge as String = CType (age,String) </li></ul><ul><li>There are several other ways to convert between types: </li></ul><ul><ul><li>System.Convert class </li></ul></ul><ul><ul><li>Dim stringAge as String = System.Convert.ToString(age) </li></ul></ul><ul><ul><li>Type methods </li></ul></ul><ul><ul><li>Dim stringAge as String = age.ToString() </li></ul></ul>
    20. 20. Preventing Implicit Conversions: Option Strict <ul><li>VB.NET allows implicit conversions by default: </li></ul><ul><li>Dim x as Integer = 25 </li></ul><ul><li>Dim y as Long = 52 </li></ul><ul><li>x = y </li></ul><ul><li>When Option Strict is turned on, the compiler will not allow implicit type conversions. You must handle conversions in the code. </li></ul><ul><li>Option Strict On </li></ul><ul><li>Dim x as Integer = 25 </li></ul><ul><li>Dim y as Long = 52 </li></ul><ul><li>x = y </li></ul>With Option Strict on, the compiler will catch this potential problem. This may cause loss of data.
    21. 21. Arrays in VB.NET <ul><li>Arrays are reference types used to hold a collection of identically typed objects. </li></ul><ul><li>Arrays can be used to hold value and reference types (integers, strings, custom objects, etc.). </li></ul>Widget Widget Widget Array of widgets
    22. 22. Declaring Arrays <ul><li>Reference and value type objects can be stored in arrays. </li></ul><ul><li>Dim A() as Integer = {1,2,3} </li></ul>A(0) 1 A(1) 2 A(2) 3 A Dim S() as String = {“Hi”,”There”,”Class”} S(0) S(1) S(2) S “ Hi” “ There” “ Class”
    23. 23. Redimensioning Arrays <ul><li>Arrays can be redimensioned using the ReDim keyword in VB.NET: </li></ul><ul><li>Dim Years(2) as Integer </li></ul><ul><li>Years(0) = 1970 </li></ul><ul><li>Years(1) = 1980 </li></ul><ul><li>Years(2) = 1990 </li></ul><ul><li>ReDim Preserve Years(6) </li></ul><ul><li>The Preserve keyword can be used when the original contents of the array need to be copied to the newly dimensioned array. </li></ul>
    24. 24. Conditional Statements <ul><li>Conditional statements play an important role in applications. </li></ul><ul><li>VB.NET supports the following types of conditional statements: </li></ul><ul><ul><li>If…Then…Else </li></ul></ul><ul><ul><li>Select…Case </li></ul></ul>
    25. 25. If Statements <ul><li>If statements provide a way to check if one or more conditions are met: </li></ul><ul><li>Dim total as Integer = GetTotalSale() ‘Call Function </li></ul><ul><li>Dim shipping as Integer </li></ul><ul><li>If total > 100 And total < 1000 Then </li></ul><ul><li>shipping = total * 0.02 </li></ul><ul><li>ElseIf total > 1000 Then </li></ul><ul><li>shipping = 0 ‘shipping is free! </li></ul><ul><li>Else </li></ul><ul><li>shipping = total * 0.9 </li></ul><ul><li>End If </li></ul>
    26. 26. Select Case Statements <ul><li>Select Case statements provide a clean way to evaluate multiple conditions: </li></ul><ul><li>Dim total As Double = GetTotalSale() 'Call Function </li></ul><ul><li>Dim phrase As String = &quot;Shipping is $&quot; </li></ul><ul><li>Select Case total </li></ul><ul><li>Case 100 To 1000 </li></ul><ul><li>Console.WriteLine(phrase & (total * 0.02)) </li></ul><ul><li>Case Is >= 1000 </li></ul><ul><li>Console.WriteLine(phrase & &quot; free!&quot;) </li></ul><ul><li>Case Is < 100 </li></ul><ul><li>Console.WriteLine(phrase & (total * 0.9)) </li></ul><ul><li>Case Else </li></ul><ul><li>Console.WriteLine(&quot;We don't ship to your address.&quot;) </li></ul><ul><li>End Select </li></ul>
    27. 27. Looping Statements <ul><li>Looping statements can be used to iterate through collections of objects or perform a task a specific number of times. </li></ul><ul><li>VB.NET supports the following types of looping statements: </li></ul><ul><ul><li>For…Next </li></ul></ul><ul><ul><li>For…Each </li></ul></ul><ul><ul><li>Do…While/Until </li></ul></ul><ul><ul><li>While </li></ul></ul>
    28. 28. For…Next Loops <ul><li>The For…Next keywords provide a way to perform looping a specific number of times: </li></ul><ul><li>Dim names() as String = {“John”,”Jane”,”Thomas”,”Dan”} </li></ul><ul><li>Dim i as Integer </li></ul><ul><li>For i=0 To names.GetUpperBound(0) Step 1 </li></ul><ul><li>Dim name as String = names(i) </li></ul><ul><li>Console.WriteLine(name) </li></ul><ul><li>Next </li></ul>Step 1 is the default.
    29. 29. For…Each Loops <ul><li>Object collections can be iterated through using the For…Each keywords: </li></ul><ul><li>Dim Exists As Boolean = False </li></ul><ul><li>Dim fname as String = “John” </li></ul><ul><li>Dim customers As Customers = GetCustomers() </li></ul><ul><li>Dim customer as Customer </li></ul><ul><li>For Each customer In customers </li></ul><ul><li>If customer.FirstName = fname Then </li></ul><ul><li>Exists = True </li></ul><ul><li>Exit For </li></ul><ul><li>End If </li></ul><ul><li>Next </li></ul>
    30. 30. Do…While and Do…Until Loops <ul><li>The Do…While and Do…Until keywords can be used to perform a looping routine until a specific condition is met: </li></ul><ul><li>Dim i as Integer = 0 </li></ul><ul><li>Do Until i = 4 </li></ul><ul><li>i += 1 </li></ul><ul><li>Loop </li></ul><ul><li>Do While i > 0 </li></ul><ul><li>i -= 1 </li></ul><ul><li>Loop </li></ul>Loop while the condition is true. Loop until the condition is true.
    31. 31. While Loops <ul><li>The VB6 While…Wend has been replaced with While…End While in VB.NET: </li></ul><ul><li>Dim i as Integer = 0 </li></ul><ul><li>While i < 6 </li></ul><ul><li>i += 1 </li></ul><ul><li>Console.WriteLine(“Value is: “ & i.ToString()) </li></ul><ul><li>End While </li></ul>
    32. 32. Demonstration <ul><li>Looping and arrays </li></ul>
    33. 33. Summary <ul><li>VB.NET programs are composed of statements. </li></ul><ul><li>Types found in the .NET framework can be classified as value or reference types. </li></ul><ul><li>Type conversions can be accomplished using the CType() function. </li></ul><ul><li>Conditional and looping statements can be used to control an application’s flow. </li></ul>
    34. 34. <ul><li>The VB6 While…Wend statement has been replaced by which statement in VB.NET? </li></ul><ul><ul><li>a. While…Loop </li></ul></ul><ul><ul><li>b. While…End While </li></ul></ul><ul><ul><li>c. Do…Loop </li></ul></ul><ul><ul><li>d. None of the above </li></ul></ul><ul><li>Which of the following characters can be used to comment a line of code in VB.NET? </li></ul><ul><ul><li>a. // </li></ul></ul><ul><ul><li>b. */ </li></ul></ul><ul><ul><li>c. /* </li></ul></ul><ul><ul><li>d. ‘ </li></ul></ul>Section Review
    35. 35. <ul><li>The following types exist in the .NET platform: </li></ul><ul><ul><li>a. Value types </li></ul></ul><ul><ul><li>b. Object types </li></ul></ul><ul><ul><li>c. Conversion types </li></ul></ul><ul><ul><li>d. Reference types </li></ul></ul><ul><ul><li>e. Both a and d </li></ul></ul>Section Review
    36. 36. <ul><li>Which of the following lines of code represents a valid code statement? </li></ul><ul><ul><li>a. Dim x as Integer = 45 </li></ul></ul><ul><ul><li>b. Dim x as _ Integer = 45 </li></ul></ul><ul><ul><li>c. Dim x as Integer = 45 </li></ul></ul><ul><ul><li>d. a and b only </li></ul></ul><ul><ul><li>e. b and c only </li></ul></ul>Section Review
    37. 37. <ul><li>Value types hold their own copy of data. </li></ul><ul><ul><li>True </li></ul></ul><ul><ul><li>False </li></ul></ul><ul><li>In the following line of VB.NET code, y and z are declared as variants: Dim x,y,z as Integer </li></ul><ul><ul><li>True </li></ul></ul><ul><ul><li>False </li></ul></ul><ul><li>Reference types hold their own copy of data. </li></ul><ul><ul><li>True </li></ul></ul><ul><ul><li>False </li></ul></ul>Section Review