Visual Basic
Visual Basic .NET
Notes for Professionals
®
.NET Notes for Professionals
100+ pages
of professional hints and tricks
Disclaimer
GoalKicker.com This is an unocial free book created for educational purposes and is
not aliated with ocial Visual Basic® .NET group(s) or company(s).
Free Programming Books All trademarks and registered trademarks are
the property of their respective owners
,Contents
About ................................................................................................................................................................................... 1
Chapter 1: Getting started with Visual Basic .NET Language ................................................................ 2
Section 1.1: Hello World ................................................................................................................................................. 2
Section 1.2: Hello World on a Textbox upon Clicking of a Button ............................................................................ 2
Section 1.3: Region ......................................................................................................................................................... 3
Section 1.4: Creating a simple Calculator to get familiar with the interface and code .......................................... 4
Chapter 2: Declaring variables .............................................................................................................................. 8
Section 2.1: Declaring and assigning a variable using a primitive type ................................................................... 8
Section 2.2: Levels of declaration – Local and Member variables ......................................................................... 10
Section 2.3: Example of Access Modifiers ................................................................................................................. 11
Chapter 3: Introduction to Syntax ..................................................................................................................... 14
Section 3.1: Intellisense Helper ................................................................................................................................... 14
Section 3.2: Declaring a Variable ............................................................................................................................... 14
Section 3.3: Comments ............................................................................................................................................... 15
Section 3.4: Modifiers .................................................................................................................................................. 15
Section 3.5: Object Initializers ..................................................................................................................................... 16
Section 3.6: Collection Initializer ................................................................................................................................. 17
Section 3.7: Writing a function ................................................................................................................................... 19
Chapter 4: Operators ............................................................................................................................................... 21
Section 4.1: String Concatenation .............................................................................................................................. 21
Section 4.2: Math ......................................................................................................................................................... 21
Section 4.3: Assignment .............................................................................................................................................. 22
Section 4.4: Comparison ............................................................................................................................................. 23
Section 4.5: Bitwise ...................................................................................................................................................... 23
Chapter 5: Conditions ............................................................................................................................................... 25
Section 5.1: If operator ................................................................................................................................................ 25
Section 5.2: IF...Then...Else ........................................................................................................................................... 25
Chapter 6: Short-Circuiting Operators (AndAlso - OrElse) .................................................................... 27
Section 6.1: OrElse Usage ........................................................................................................................................... 27
Section 6.2: AndAlso Usage ........................................................................................................................................ 27
Section 6.3: Avoiding NullReferenceException ......................................................................................................... 27
Chapter 7: Date ........................................................................................................................................................... 30
Section 7.1: Converting (Parsing) a String to a Date ............................................................................................... 30
Section 7.2: Converting a Date To A String .............................................................................................................. 30
Chapter 8: Array ......................................................................................................................................................... 31
Section 8.1: Array definition ........................................................................................................................................ 31
Section 8.2: Null Array Variables ............................................................................................................................... 31
Section 8.3: Array initialization ................................................................................................................................... 32
Section 8.4: Declare a single-dimension array and set array element values ..................................................... 32
Section 8.5: Jagged Array Initialization .................................................................................................................... 32
Section 8.6: Non-zero lower bounds ......................................................................................................................... 32
Section 8.7: Referencing Same Array from Two Variables .................................................................................... 33
Section 8.8: Multidimensional Array initialization ..................................................................................................... 33
Chapter 9: Lists ............................................................................................................................................................ 34
Section 9.1: Add items to a List ................................................................................................................................... 34
Section 9.2: Check if item exists in a List ................................................................................................................... 34
, Section 9.3: Loop through items in list ...................................................................................................................... 34
Section 9.4: Create a List ............................................................................................................................................ 35
Section 9.5: Remove items from a List ...................................................................................................................... 36
Section 9.6: Retrieve items from a List ...................................................................................................................... 36
Chapter 10: Enum ........................................................................................................................................................ 38
Section 10.1: GetNames() ............................................................................................................................................ 38
Section 10.2: HasFlag() ............................................................................................................................................... 38
Section 10.3: Enum definition ...................................................................................................................................... 39
Section 10.4: Member initialization ............................................................................................................................. 39
Section 10.5: The Flags attribute ................................................................................................................................ 39
Section 10.6: GetValues() ............................................................................................................................................ 40
Section 10.7: String parsing ........................................................................................................................................ 40
Section 10.8: ToString() ............................................................................................................................................... 41
Section 10.9: Determine whether a Enum has FlagsAttribute specified or not ..................................................... 41
Section 10.10: For-each flag (flag iteration) .............................................................................................................. 42
Section 10.11: Determine the amount of flags in a flag combination ..................................................................... 42
Section 10.12: Find the nearest value in a Enum ....................................................................................................... 43
Chapter 11: Dictionaries ........................................................................................................................................... 45
Section 11.1: Create a dictionary filled with values .................................................................................................... 45
Section 11.2: Loop through a dictionary and print all entries .................................................................................. 45
Section 11.3: Checking for key already in dictionary - data reduction ................................................................... 45
Section 11.4: Getting a dictionary value ..................................................................................................................... 46
Chapter 12: Looping ................................................................................................................................................... 47
Section 12.1: For...Next .................................................................................................................................................. 47
Section 12.2: For Each...Next loop for looping through collection of items ............................................................ 48
Section 12.3: Short Circuiting ...................................................................................................................................... 48
Section 12.4: While loop to iterate while some condition is true ............................................................................. 50
Section 12.5: Nested Loop ........................................................................................................................................... 50
Section 12.6: Do...Loop ................................................................................................................................................. 51
Chapter 13: File Handling ........................................................................................................................................ 53
Section 13.1: Write Data to a File ................................................................................................................................ 53
Section 13.2: Read All Contents of a File ................................................................................................................... 53
Section 13.3: Write Lines Individually to a Text File using StreamWriter ............................................................... 53
Chapter 14: File/Folder Compression .............................................................................................................. 54
Section 14.1: Adding File Compression to your project ............................................................................................ 54
Section 14.2: Creating zip archive from directory .................................................................................................... 54
Section 14.3: Extracting zip archive to directory ....................................................................................................... 54
Section 14.4: Create zip archive dynamicaly ............................................................................................................ 54
Chapter 15: Connection Handling ....................................................................................................................... 55
Section 15.1: Public connection property ................................................................................................................... 55
Chapter 16: Data Access .......................................................................................................................................... 56
Section 16.1: Read field from Database ..................................................................................................................... 56
Section 16.2: Simple Function to read from Database and return as DataTable ................................................. 57
Chapter 17: Type conversion ................................................................................................................................ 58
Section 17.1: Converting Text of The Textbox to an Integer .................................................................................... 58
Chapter 18: ByVal and ByRef keywords ......................................................................................................... 59
Section 18.1: ByRef keyword ....................................................................................................................................... 59
Section 18.2: ByVal keyword ...................................................................................................................................... 59
Chapter 19: Console ................................................................................................................................................... 61
, Section 19.1: Console.ReadLine() ................................................................................................................................ 61
Section 19.2: Console.Read() ...................................................................................................................................... 61
Section 19.3: Console.ReadKey() ................................................................................................................................ 61
Section 19.4: Prototype of command line prompt ................................................................................................... 61
Section 19.5: Console.WriteLine() ............................................................................................................................... 62
Chapter 20: Functions .............................................................................................................................................. 63
Section 20.1: Defining a Function ............................................................................................................................... 63
Section 20.2: Defining a Function #2 ........................................................................................................................ 63
Chapter 21: Recursion ............................................................................................................................................... 64
Section 21.1: Compute nth Fibonacci number ........................................................................................................... 64
Chapter 22: Random ................................................................................................................................................. 65
Section 22.1: Declaring an instance ........................................................................................................................... 65
Section 22.2: Generate a random number from an instance of Random ............................................................ 65
Chapter 23: Classes .................................................................................................................................................... 67
Section 23.1: Abstract Classes .................................................................................................................................... 67
Section 23.2: Creating classes .................................................................................................................................... 67
Chapter 24: Generics ................................................................................................................................................ 69
Section 24.1: Create a generic class .......................................................................................................................... 69
Section 24.2: Instance of a Generic Class ................................................................................................................. 69
Section 24.3: Define a 'generic' class ........................................................................................................................ 69
Section 24.4: Use a generic class .............................................................................................................................. 69
Section 24.5: Limit the possible types given ............................................................................................................. 70
Section 24.6: Create a new instance of the given type ........................................................................................... 70
Chapter 25: Disposable objects ........................................................................................................................... 71
Section 25.1: Basic concept of IDisposable ............................................................................................................... 71
Section 25.2: Declaring more objects in one Using ................................................................................................. 71
Chapter 26: NullReferenceException ................................................................................................................ 73
Section 26.1: Empty Return ......................................................................................................................................... 73
Section 26.2: Uninitialized variable ............................................................................................................................ 73
Chapter 27: Using Statement ............................................................................................................................... 74
Section 27.1: See examples under Disposable objects ............................................................................................ 74
Chapter 28: Option Strict ........................................................................................................................................ 75
Section 28.1: Why Use It? ............................................................................................................................................ 75
Section 28.2: How to Switch It On .............................................................................................................................. 75
Chapter 29: Option Explicit ..................................................................................................................................... 77
Section 29.1: What is it? ............................................................................................................................................... 77
Section 29.2: How to switch it on? ............................................................................................................................. 77
Chapter 30: Option Infer ......................................................................................................................................... 78
Section 30.1: How to enable/disable it ...................................................................................................................... 78
Section 30.2: What is it? .............................................................................................................................................. 78
Section 30.3: When to use type inference ................................................................................................................ 79
Chapter 31: Error Handling ..................................................................................................................................... 81
Section 31.1: Try...Catch...Finally Statement ............................................................................................................... 81
Section 31.2: Creating custom exception and throwing .......................................................................................... 81
Section 31.3: Try Catch in Database Operation ........................................................................................................ 82
Section 31.4: The Un-catchable Exception ................................................................................................................ 82
Section 31.5: Critical Exceptions ................................................................................................................................. 82
Chapter 32: OOP Keywords ................................................................................................................................... 84
Visual Basic .NET
Notes for Professionals
®
.NET Notes for Professionals
100+ pages
of professional hints and tricks
Disclaimer
GoalKicker.com This is an unocial free book created for educational purposes and is
not aliated with ocial Visual Basic® .NET group(s) or company(s).
Free Programming Books All trademarks and registered trademarks are
the property of their respective owners
,Contents
About ................................................................................................................................................................................... 1
Chapter 1: Getting started with Visual Basic .NET Language ................................................................ 2
Section 1.1: Hello World ................................................................................................................................................. 2
Section 1.2: Hello World on a Textbox upon Clicking of a Button ............................................................................ 2
Section 1.3: Region ......................................................................................................................................................... 3
Section 1.4: Creating a simple Calculator to get familiar with the interface and code .......................................... 4
Chapter 2: Declaring variables .............................................................................................................................. 8
Section 2.1: Declaring and assigning a variable using a primitive type ................................................................... 8
Section 2.2: Levels of declaration – Local and Member variables ......................................................................... 10
Section 2.3: Example of Access Modifiers ................................................................................................................. 11
Chapter 3: Introduction to Syntax ..................................................................................................................... 14
Section 3.1: Intellisense Helper ................................................................................................................................... 14
Section 3.2: Declaring a Variable ............................................................................................................................... 14
Section 3.3: Comments ............................................................................................................................................... 15
Section 3.4: Modifiers .................................................................................................................................................. 15
Section 3.5: Object Initializers ..................................................................................................................................... 16
Section 3.6: Collection Initializer ................................................................................................................................. 17
Section 3.7: Writing a function ................................................................................................................................... 19
Chapter 4: Operators ............................................................................................................................................... 21
Section 4.1: String Concatenation .............................................................................................................................. 21
Section 4.2: Math ......................................................................................................................................................... 21
Section 4.3: Assignment .............................................................................................................................................. 22
Section 4.4: Comparison ............................................................................................................................................. 23
Section 4.5: Bitwise ...................................................................................................................................................... 23
Chapter 5: Conditions ............................................................................................................................................... 25
Section 5.1: If operator ................................................................................................................................................ 25
Section 5.2: IF...Then...Else ........................................................................................................................................... 25
Chapter 6: Short-Circuiting Operators (AndAlso - OrElse) .................................................................... 27
Section 6.1: OrElse Usage ........................................................................................................................................... 27
Section 6.2: AndAlso Usage ........................................................................................................................................ 27
Section 6.3: Avoiding NullReferenceException ......................................................................................................... 27
Chapter 7: Date ........................................................................................................................................................... 30
Section 7.1: Converting (Parsing) a String to a Date ............................................................................................... 30
Section 7.2: Converting a Date To A String .............................................................................................................. 30
Chapter 8: Array ......................................................................................................................................................... 31
Section 8.1: Array definition ........................................................................................................................................ 31
Section 8.2: Null Array Variables ............................................................................................................................... 31
Section 8.3: Array initialization ................................................................................................................................... 32
Section 8.4: Declare a single-dimension array and set array element values ..................................................... 32
Section 8.5: Jagged Array Initialization .................................................................................................................... 32
Section 8.6: Non-zero lower bounds ......................................................................................................................... 32
Section 8.7: Referencing Same Array from Two Variables .................................................................................... 33
Section 8.8: Multidimensional Array initialization ..................................................................................................... 33
Chapter 9: Lists ............................................................................................................................................................ 34
Section 9.1: Add items to a List ................................................................................................................................... 34
Section 9.2: Check if item exists in a List ................................................................................................................... 34
, Section 9.3: Loop through items in list ...................................................................................................................... 34
Section 9.4: Create a List ............................................................................................................................................ 35
Section 9.5: Remove items from a List ...................................................................................................................... 36
Section 9.6: Retrieve items from a List ...................................................................................................................... 36
Chapter 10: Enum ........................................................................................................................................................ 38
Section 10.1: GetNames() ............................................................................................................................................ 38
Section 10.2: HasFlag() ............................................................................................................................................... 38
Section 10.3: Enum definition ...................................................................................................................................... 39
Section 10.4: Member initialization ............................................................................................................................. 39
Section 10.5: The Flags attribute ................................................................................................................................ 39
Section 10.6: GetValues() ............................................................................................................................................ 40
Section 10.7: String parsing ........................................................................................................................................ 40
Section 10.8: ToString() ............................................................................................................................................... 41
Section 10.9: Determine whether a Enum has FlagsAttribute specified or not ..................................................... 41
Section 10.10: For-each flag (flag iteration) .............................................................................................................. 42
Section 10.11: Determine the amount of flags in a flag combination ..................................................................... 42
Section 10.12: Find the nearest value in a Enum ....................................................................................................... 43
Chapter 11: Dictionaries ........................................................................................................................................... 45
Section 11.1: Create a dictionary filled with values .................................................................................................... 45
Section 11.2: Loop through a dictionary and print all entries .................................................................................. 45
Section 11.3: Checking for key already in dictionary - data reduction ................................................................... 45
Section 11.4: Getting a dictionary value ..................................................................................................................... 46
Chapter 12: Looping ................................................................................................................................................... 47
Section 12.1: For...Next .................................................................................................................................................. 47
Section 12.2: For Each...Next loop for looping through collection of items ............................................................ 48
Section 12.3: Short Circuiting ...................................................................................................................................... 48
Section 12.4: While loop to iterate while some condition is true ............................................................................. 50
Section 12.5: Nested Loop ........................................................................................................................................... 50
Section 12.6: Do...Loop ................................................................................................................................................. 51
Chapter 13: File Handling ........................................................................................................................................ 53
Section 13.1: Write Data to a File ................................................................................................................................ 53
Section 13.2: Read All Contents of a File ................................................................................................................... 53
Section 13.3: Write Lines Individually to a Text File using StreamWriter ............................................................... 53
Chapter 14: File/Folder Compression .............................................................................................................. 54
Section 14.1: Adding File Compression to your project ............................................................................................ 54
Section 14.2: Creating zip archive from directory .................................................................................................... 54
Section 14.3: Extracting zip archive to directory ....................................................................................................... 54
Section 14.4: Create zip archive dynamicaly ............................................................................................................ 54
Chapter 15: Connection Handling ....................................................................................................................... 55
Section 15.1: Public connection property ................................................................................................................... 55
Chapter 16: Data Access .......................................................................................................................................... 56
Section 16.1: Read field from Database ..................................................................................................................... 56
Section 16.2: Simple Function to read from Database and return as DataTable ................................................. 57
Chapter 17: Type conversion ................................................................................................................................ 58
Section 17.1: Converting Text of The Textbox to an Integer .................................................................................... 58
Chapter 18: ByVal and ByRef keywords ......................................................................................................... 59
Section 18.1: ByRef keyword ....................................................................................................................................... 59
Section 18.2: ByVal keyword ...................................................................................................................................... 59
Chapter 19: Console ................................................................................................................................................... 61
, Section 19.1: Console.ReadLine() ................................................................................................................................ 61
Section 19.2: Console.Read() ...................................................................................................................................... 61
Section 19.3: Console.ReadKey() ................................................................................................................................ 61
Section 19.4: Prototype of command line prompt ................................................................................................... 61
Section 19.5: Console.WriteLine() ............................................................................................................................... 62
Chapter 20: Functions .............................................................................................................................................. 63
Section 20.1: Defining a Function ............................................................................................................................... 63
Section 20.2: Defining a Function #2 ........................................................................................................................ 63
Chapter 21: Recursion ............................................................................................................................................... 64
Section 21.1: Compute nth Fibonacci number ........................................................................................................... 64
Chapter 22: Random ................................................................................................................................................. 65
Section 22.1: Declaring an instance ........................................................................................................................... 65
Section 22.2: Generate a random number from an instance of Random ............................................................ 65
Chapter 23: Classes .................................................................................................................................................... 67
Section 23.1: Abstract Classes .................................................................................................................................... 67
Section 23.2: Creating classes .................................................................................................................................... 67
Chapter 24: Generics ................................................................................................................................................ 69
Section 24.1: Create a generic class .......................................................................................................................... 69
Section 24.2: Instance of a Generic Class ................................................................................................................. 69
Section 24.3: Define a 'generic' class ........................................................................................................................ 69
Section 24.4: Use a generic class .............................................................................................................................. 69
Section 24.5: Limit the possible types given ............................................................................................................. 70
Section 24.6: Create a new instance of the given type ........................................................................................... 70
Chapter 25: Disposable objects ........................................................................................................................... 71
Section 25.1: Basic concept of IDisposable ............................................................................................................... 71
Section 25.2: Declaring more objects in one Using ................................................................................................. 71
Chapter 26: NullReferenceException ................................................................................................................ 73
Section 26.1: Empty Return ......................................................................................................................................... 73
Section 26.2: Uninitialized variable ............................................................................................................................ 73
Chapter 27: Using Statement ............................................................................................................................... 74
Section 27.1: See examples under Disposable objects ............................................................................................ 74
Chapter 28: Option Strict ........................................................................................................................................ 75
Section 28.1: Why Use It? ............................................................................................................................................ 75
Section 28.2: How to Switch It On .............................................................................................................................. 75
Chapter 29: Option Explicit ..................................................................................................................................... 77
Section 29.1: What is it? ............................................................................................................................................... 77
Section 29.2: How to switch it on? ............................................................................................................................. 77
Chapter 30: Option Infer ......................................................................................................................................... 78
Section 30.1: How to enable/disable it ...................................................................................................................... 78
Section 30.2: What is it? .............................................................................................................................................. 78
Section 30.3: When to use type inference ................................................................................................................ 79
Chapter 31: Error Handling ..................................................................................................................................... 81
Section 31.1: Try...Catch...Finally Statement ............................................................................................................... 81
Section 31.2: Creating custom exception and throwing .......................................................................................... 81
Section 31.3: Try Catch in Database Operation ........................................................................................................ 82
Section 31.4: The Un-catchable Exception ................................................................................................................ 82
Section 31.5: Critical Exceptions ................................................................................................................................. 82
Chapter 32: OOP Keywords ................................................................................................................................... 84