Visual Basic .NET
Notes for Professionals
Visual Basic®
.NET
Notes for Professionals
100+ pages
of professional hints and tricks
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial Visual Basic® .NET group(s) or company(s).
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
Section 1.2: Hello World on a Textbox upon Clicking of a Button
Section 1.3: Region
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
Section 2.2: Levels of declaration – Local and Member variables
Section 2.3: Example of Access Modifiers
...................................................................
8
.........................................................................
.................................................................................................................
Chapter 3: Introduction to Syntax
.....................................................................................................................
14
Section 3.1: Intellisense Helper
Section 3.2: Declaring a Variable
Section 3.3: Comments
Section 3.4: Modifiers
Section 3.5: Object Initializers
Section 3.6: Collection Initializer
Section 3.7: Writing a function
...................................................................................................................................
14
...............................................................................................................................
14
...............................................................................................................................................
..................................................................................................................................................
.....................................................................................................................................
.................................................................................................................................
...................................................................................................................................
Chapter 4: Operators
...............................................................................................................................................
21
Section 4.1: String Concatenation
Section 4.2: Math
Section 4.3: Assignment
Section 4.4: Comparison
Section 4.5: Bitwise
Chapter 5: Conditions
Section 5.1: If operator
Section 5.2: IF...Then...Else
..............................................................................................................................
21
.........................................................................................................................................................
..............................................................................................................................................
.............................................................................................................................................
......................................................................................................................................................
...............................................................................................................................................
................................................................................................................................................
...........................................................................................................................................
Chapter 6: Short-Circuiting Operators (AndAlso - OrElse)
....................................................................
27
Section 6.1: OrElse Usage
Section 6.2: AndAlso Usage
Section 6.3: Avoiding NullReferenceException
...........................................................................................................................................
........................................................................................................................................
.........................................................................................................
27
Chapter 7: Date
...........................................................................................................................................................
30
Section 7.1: Converting (Parsing) a String to a Date
Section 7.2: Converting a Date To A String
...............................................................................................
30
..............................................................................................................
30
Chapter 8: Array
.........................................................................................................................................................
31
........................................................................................................................................
...............................................................................................................................
...................................................................................................................................
Section 8.1: Array definition
Section 8.2: Null Array Variables
Section 8.3: Array initialization
Section 8.4: Declare a single-dimension array and set array element values
Section 8.5: Jagged Array Initialization
Section 8.6: Non-zero lower bounds
Section 8.7: Referencing Same Array from Two Variables
Section 8.8: Multidimensional Array initialization
....................................................................................................................
.........................................................................................................................
....................................................................................
.....................................................................................................
.....................................................
32
2
2
3
10
11
15
15
16
17
19
21
22
23
23
25
25
25
Publicité
27
27
31
31
32
32
32
33
33
Chapter 9: Lists
............................................................................................................................................................
34
Section 9.1: Add items to a List
Section 9.2: Check if item exists in a List
...................................................................................................................................
34
...................................................................................................................
34
34
35
38
38
39
39
39
40
40
41
42
42
43
45
45
46
47
47
48
50
50
51
53
53
53
54
54
54
Section 9.3: Loop through items in list
Section 9.4: Create a List
Section 9.5: Remove items from a List
Section 9.6: Retrieve items from a List
......................................................................................................................
............................................................................................................................................
......................................................................................................................
36
......................................................................................................................
36
Chapter 10: Enum
........................................................................................................................................................
38
............................................................................................................................................
...............................................................................................................................................
......................................................................................................................................
Section 10.1: GetNames()
Section 10.2: HasFlag()
Section 10.3: Enum definition
Section 10.4: Member initialization
Section 10.5: The Flags attribute
Section 10.6: GetValues()
Section 10.7: String parsing
Section 10.8: ToString()
Section 10.9: Determine whether a Enum has FlagsAttribute specified or not
Section 10.10: For-each flag (flag iteration)
Section 10.11: Determine the amount of flags in a flag combination
Section 10.12: Find the nearest value in a Enum
.............................................................................................................................
................................................................................................................................
............................................................................................................................................
........................................................................................................................................
...............................................................................................................................................
..............................................................................................................
.....................................................................
.......................................................................................................
.....................................................
41
Chapter 11: Dictionaries
...........................................................................................................................................
45
Section 11.1: Create a dictionary filled with values
Section 11.2: Loop through a dictionary and print all entries
Section 11.3: Checking for key already in dictionary - data reduction
Section 11.4: Getting a dictionary value
....................................................................................................
..................................................................................
...................................................................
45
.....................................................................................................................
...................................................................................................................................................
..................................................................................................................................................
Chapter 12: Looping
Section 12.1: For...Next
Section 12.2: For Each...Next loop for looping through collection of items
Section 12.3: Short Circuiting
Section 12.4: While loop to iterate while some condition is true
Section 12.5: Nested Loop
Section 12.6: Do...Loop
Chapter 13: File Handling
...........................................................................................................................................
.................................................................................................................................................
........................................................................................................................................
......................................................................................................................................
.............................................................................
............................................................
48
Section 13.1: Write Data to a File
Section 13.2: Read All Contents of a File
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
Section 14.2: Creating zip archive from directory
Section 14.3: Extracting zip archive to directory
Section 14.4: Create zip archive dynamicaly
....................................................................................................
.......................................................................................................
............................................................................................................
............................................................................................
54
Chapter 15: Connection Handling
.......................................................................................................................
55
Publicité
Section 15.1: Public connection property
...................................................................................................................
55
Chapter 16: Data Access
..........................................................................................................................................
56
Section 16.1: Read field from Database
Section 16.2: Simple Function to read from Database and return as DataTable
.....................................................................................................................
56
.................................................
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
Section 18.2: ByVal keyword
.......................................................................................................................................
59
......................................................................................................................................
59
Chapter 19: Console
...................................................................................................................................................
61
Section 19.1: Console.ReadLine()
Section 19.2: Console.Read()
Section 19.3: Console.ReadKey()
Section 19.4: Prototype of command line prompt
Section 19.5: Console.WriteLine()
................................................................................................................................
......................................................................................................................................
................................................................................................................................
61
61
61
...................................................................................................
61
...............................................................................................................................
62
Chapter 20: Functions
..............................................................................................................................................
63
Section 20.1: Defining a Function
Section 20.2: Defining a Function #2
...............................................................................................................................
63
........................................................................................................................
63
Chapter 21: Recursion
...............................................................................................................................................
64
Section 21.1: Compute nth Fibonacci number
...........................................................................................................
64
Chapter 22: Random
.................................................................................................................................................
65
Section 22.1: Declaring an instance
Section 22.2: Generate a random number from an instance of Random
...........................................................................................................................
65
............................................................
65
Chapter 23: Classes
....................................................................................................................................................
67
Section 23.1: Abstract Classes
Section 23.2: Creating classes
....................................................................................................................................
67
....................................................................................................................................
67
Chapter 24: Generics
................................................................................................................................................
69
Section 24.1: Create a generic class
Section 24.2: Instance of a Generic Class
Section 24.3: Define a 'generic' class
Section 24.4: Use a generic class
Section 24.5: Limit the possible types given
Section 24.6: Create a new instance of the given type
..........................................................................................................................
.................................................................................................................
........................................................................................................................
..............................................................................................................................
.............................................................................................................
...........................................................................................
70
Chapter 25: Disposable objects
...........................................................................................................................
71
Section 25.1: Basic concept of IDisposable
Section 25.2: Declaring more objects in one Using
...............................................................................................................
71
.................................................................................................
71
Chapter 26: NullReferenceException
................................................................................................................
73
Section 26.1: Empty Return
Section 26.2: Uninitialized variable
.........................................................................................................................................
73
............................................................................................................................
73
Chapter 27: Using Statement
...............................................................................................................................
74
Section 27.1: See examples under Disposable objects
............................................................................................
74
Chapter 28: Option Strict
Section 28.1: Why Use It?
Section 28.2: How to Switch It On
Chapter 29: Option Explicit
Section 29.1: What is it?
Section 29.2: How to switch it on?
........................................................................................................................................
............................................................................................................................................
..............................................................................................................................
75
.....................................................................................................................................
...............................................................................................................................................
.............................................................................................................................
77
Chapter 30: Option Infer
.........................................................................................................................................
78
Publicité
Section 30.1: How to enable/disable it
Section 30.2: What is it?
Section 30.3: When to use type inference
......................................................................................................................
..............................................................................................................................................
................................................................................................................
79
Chapter 31: Error Handling
.....................................................................................................................................
81
Section 31.1: Try...Catch...Finally Statement
Section 31.2: Creating custom exception and throwing
Section 31.3: Try Catch in Database Operation
Section 31.4: The Un-catchable Exception
Section 31.5: Critical Exceptions
Chapter 32: OOP Keywords
...............................................................................................................
81
..........................................................................................
81
........................................................................................................
................................................................................................................
.................................................................................................................................
...................................................................................................................................
69
69
69
69
70
75
75
77
77
78
78
82
82
82
84
......................................................................................................
.....................................................................................................................................
Section 32.1: Defining a class
Section 32.2: Inheritance Modifiers (on classes)
Section 32.3: Inheritance Modifiers (on properties and methods)
Section 32.4: MyBase
Section 32.5: Me vs MyClass
Section 32.6: Overloading
Section 32.7: Shadows
Section 32.8: Interfaces
.......................................................................................................................................
...........................................................................................................................................
................................................................................................................................................
...............................................................................................................................................
..................................................................................................................................................
.........................................................................
85
Chapter 33: Extension methods
...........................................................................................................................
91
Section 33.1: Creating an extension method
Section 33.2: Making the language more functional with extension methods
Section 33.3: Getting Assembly Version From Strong Name
Section 33.4: Padding Numerics
.....................................................
91
.................................................................................
................................................................................................................................
.............................................................................................................
91
Chapter 34: Reflection
.............................................................................................................................................
94
...................................................................................
94
.................................................................................................................
.................................................................................................
.................................................................................................................
...........................................................................................................
Section 34.1: Retrieve Properties for an Instance of a Class
Section 34.2: Get a method and invoke it
Section 34.3: Create an instance of a generic type
Section 34.4: Get the members of a type
Chapter 35: Visual Basic 14.0 Features
Section 35.1: Null conditional operator
Section 35.2: String interpolation
Section 35.3: Read-Only Auto-Properties
Section 35.4: NameOf operator
Section 35.5: Multiline string literals
Section 35.6: Partial Modules and Interfaces
Section 35.7: Comments after implicit line continuation
Section 35.8: #Region directive improvements
......................................................................................................................
...............................................................................................................................
.................................................................................................................
.................................................................................................................................
...........................................................................................................................
...........................................................................................................
.........................................................................................
........................................................................................................
Chapter 36: LINQ
.......................................................................................................................................................
101
Section 36.1: Selecting from array with simple condition
Section 36.2: Mapping array by Select clause
Section 36.3: Ordering output
Section 36.4: Generating Dictionary From IEnumerable
Section 36.5: Projection
Section 36.6: Getting distinct values (using the Distinct method)
..................................................................................................................................
.......................................................................................
.............................................................................................................................................
......................................................................................
.......................................................................................................
........................................................................
102
Chapter 37: FTP server
..........................................................................................................................................
103
Section 37.1: Download file from FTP server
Section 37.2: Download file from FTP server when login required
Section 37.3: Upload file to FTP server
Section 37.4: Upload file to FTP server when login required
..........................................................................................................
103
......................................................................
103
...................................................................................................................
................................................................................
Chapter 38: Working with Windows Forms
Section 38.1: Using the default Form instance
Section 38.2: Passing Data From One Form To Another
.................................................................................................
.......................................................................................................
......................................................................................
104
Publicité
Chapter 39: Google Maps in a Windows Form
...........................................................................................
106
Section 39.1: How to use a Google Map in a Windows Form
................................................................................
106
Chapter 40: GDI+
......................................................................................................................................................
115
Section 40.1: Draw Shapes
Section 40.2: Fill Shapes
Section 40.3: Text
Section 40.4: Create Graphic Object
.......................................................................................................................................
...........................................................................................................................................
......................................................................................................................................................
.......................................................................................................................
116
84
84
86
87
88
88
90
91
92
94
94
94
96
96
96
97
97
98
98
99
99
101
101
101
101
102
103
103
104
104
115
115
116
Chapter 41: WinForms SpellCheckBox
Section 41.1: ElementHost WPF TextBox
...........................................................................................................
118
.................................................................................................................
118
Chapter 42: Using axWindowsMediaPlayer in VB.Net
...........................................................................
122
Section 42.1: Adding the axWindowsMediaPlayer
Section 42.2: Play a Multimedia File
Chapter 43: WPF XAML Data Binding
.................................................................................................
122
........................................................................................................................
............................................................................................................
123
124
Section 43.1: Binding a String in the ViewModel to a TextBox in the View
..........................................................
124
Chapter 44: Reading compressed textfile on-the-fly
...........................................................................
126
Section 44.1: Reading .gz textfile line after line
......................................................................................................
126
Chapter 45: Threading
..........................................................................................................................................
127
Section 45.1: Performing thread-safe calls using Control.Invoke()
Section 45.2: Performing thread-safe calls using Async/Await
......................................................................
127
...........................................................................
127
Chapter 46: Multithreading
.................................................................................................................................
129
Section 46.1: Multithreading using Thread Class
....................................................................................................
129
Chapter 47: BackgroundWorker
......................................................................................................................
131
Section 47.1: Using BackgroundWorker
Section 47.2: Accessing GUI components in BackgroundWorker
..................................................................................................................
131
........................................................................
132
Chapter 48: Using BackgroundWorker
.........................................................................................................
133
Section 48.1: Basic implementation of Background worker class
.......................................................................
133
Chapter 49: Task-based asynchronous pattern
......................................................................................
134
Section 49.1: Basic usage of Async/Await
Section 49.2: Using TAP with LINQ
..............................................................................................................
134
..........................................................................................................................
134
Chapter 50: Debugging your application
....................................................................................................
135
Section 50.1: Debug in the console
Section 50.2: Indenting your debug output
Section 50.3: Debug in a text file
Chapter 51: Unit Testing in VB.NET
..........................................................................................................................
135
............................................................................................................
135
.............................................................................................................................
..................................................................................................................
Section 51.1: Unit Testing for Tax Calculation
Section 51.2: Testing Employee Class assigned and derived Properties
......................................................