Академический Документы
Профессиональный Документы
Культура Документы
The main defect in the tripod programming system was that it did not
support a programming language to further support programmable systems
within the Windows platform. Microsoft; however, took the initiative to mix
Ruby with some basic language and the rise of Visual Basic as a platform
was started. Microsoft had abandoned one of its database program known
as Omega. The amalgamation of Ruby interface together with the
Embedded BASIC engine created for Omega provided another platform to
proceed with the programmable systems. One of the best advantages of
Ruby is to support the ability of loading dynamic link libraries. These
libraries contained GIZMOS (also known as supplementary controls) that
helped the designers to design the VBX interface later. With the
advancement in the visual basic platform and programming techniques,
different visual basic versions were released both commercially and as
freeware. Freeware versions; however, did not include complete features
that the commercial released had.
Visual Basic 1.0 was released back in 1991. The sole purpose of visual
basic 1.0 was to act as a programming platform for different programmable
systems within the Windows environment.
After the release of visual basic 1.0, another version with the name of
Visual Basic 1.0 for DOS was introduced back in 1992. This version was
actually the post-release for the DOS-based BASIC compilers, QuickBasic
(another compiler developed by Microsoft) and Basic Professional
Development System made by Microsoft. That was the only reason for the
non-compatibility of the language with the Visual Basic for Windows. The
features of this version greatly tackled the interface that used to stimulate
the look of a Graphical user interface with the help of ASCII characters.
3. Visual Basic 2.0:
Visual Basic 2.0 version of old Visual Basic was released back in late 1992.
The sole purpose of this release was to modify some of the buds and
speed up the overall performance of the platform. The environment was
also shifted towards an easier version to help people use the Visual Basic
easily.
In the mid of 1993, another version of visual basic with the name of Visual
Basic 3.0 was released. This version was further released in two different
variants namely Standard Visual Basic 3.0 and Professional Visual basic
3.0. The main addition to Visual Basic 3.0 was its provision of database
engine. The main ability of this database engine is that it could read and
write databases, normally Access Databases.
5. Visual Basic 4.0:
Visual Basic 4.0 was released in 1995. This was the first ever release of
Visual Basic that has the ability to create programs of 32-bit and 16-bit. In
addition, for the first time, this release enabled programmers to write
classes in VB.
6. Visual Basic 5.0:
In 1997, another version of visual basic was released with the name of
Visual basic 5.0. This version was enabled for 32-bit programs within the
Windows environment. One good advantage of visual basic 5.0 was that it
enabled programmers to write programs in 5.0 and import programs that
are already written in visual basic 4.0. Customer User Controls and
Compilation of codes to native windows executable codes are the two main
features of visual basic 5.0. This release also came up with speedy runtime
execution of codes.
7. Visual Basic 6.0:
In 1998, another version of visual basic hit the market with the name Visual
Basic 6.0. With this release, a number of aspects within the Visual basic
arena were improved. For the first time, programmers and designers could
mutually use Visual Basic 6.0 to create programs within the Windows
environment as well as Website applications using IE browser.
Visual Basic.NET:
Visual basic.NET is a high-language programming language with an
integrated implementation of .NET Framework. Launched in 2002, Visual
Basic.NET acted as a basic platform for programmers to program codes
within the Windows environment using .NET Framework. Visual Studio is
the next-level pack for further development of Visual Basic.NET using
integrated development environment (IDE). More important is to know
about VBC.EXE, which is a freeware command line compiler. VBC.EXE
was included in the .NET Framework SDK. A simple example to execute
Hi Buddy in a VB.NET program is given below:
?
Module Module1
1
Sub Main()
2
' The classic "Hi Buddy" demonstration program
3
Console.WriteLine("Hi, Buddy!")
4
End Sub
5
End Module
6
The code opens up with Sub Main () and ends with End Sub to execute the
main body of the program. In the above-mentioned program, the program
will execute the result as hi Buddy.
1. Managed Code: Code that directly targets the Common Language Runtime
(CLR) in .NET Framework
2. Unmanaged Code: Code that does not directly targets the Common Language
Runtime (CLR) in .NET Framework
The main synchronization is required while writing a code. A code that is written in
a CLS should synchronize with that written in another CLS-based language.
.NET Framework 4.5.2 has recently been launched in 2014. For Windows forms, high
DPI scenarios improvements have been made. However, for ASP.NET platform,
HTTP header scrutiny and adjustments procedures have been made to improve its
overall working.
By the way the Express products are slimmed-down versions of their Visual
Studio 2013 counterparts and are primarily targeted for the .NET hobbyists
and students. Though completely free this version of Visual Studio is
surprising capable; Some of the most valuable features are object browsing
tools, a Windows Forms designer, the Add References dialog box,
IntelliSense capabilities etc.
For more information about creating a new project check our other tutorials.
NOTE: For long-term use you need to register your Express Edition so
please find the "Register Product" in the Help menu to get your FREE
key and unlock it. You will not be prompted again for this info. Enjoy!
NOTE: The screenshots in this tutorial may not look exactly like the ones
on your system because of the OS and selected style. These screenshots
were taken on a computer running Windows 8.1 so they display the
Windows 8.1 look and feel. Also it may look different if you have chosen a
different configuration (we assume you have configured VS for Visual Basic
development).
3. The Properties window allows you to view and modify the properties of
the form and of the controls that it contains.
In addition please note the drop-down list at the top of the window. Well
that list holds the names of all of the controls on the form. To select a
certain control, you can either click it on the Designer or select it from this
drop-down list.
The small icons below the dropdown determine what items are displayed in
the window and how they are arranged. For example if you click the
leftmost button, the window lists properties grouped by category and if
you click the second icon that holds the letters A and Z, the window lists the
control's properties alphabetically.
4. The Solution Explorer lets you manage the files associated with the
current solution. For example, in the Figure below, you could select
LoginForm.vb in the Solution Explorer and then click the View Code button
(the third icon from the right at the top of the Solution Explorer) to open the
form's code editor.
You can also right-click an object in the Solution Explorer to get a list of
appropriate commands for that object. This window makes it easier to find
a command by right-clicking an object related to whatever you want to do
than it is to wander through the menus.
5. The Error List window shows errors and warnings in the current
project. For example, if the code contains invalid character, this list will say
so. It's extremly useful as it clearly tells you the type of the
error(s) showing a full description, file name, line and everything else that
helps you easily find and fix the error. If you don't see the Error List it is
probably hidden. You can display it by selecting the appropriate item in
the View menu.
The most obvious feature of the code editor is that it lets you type code, but
the code editor is far more than a simple text editor such as Notepad.
It provides many features to make writing correct Visual Basic code much
easier. For example you can create an event handler within the code
editor. The upper left part of the code editor displays a drop-down listing all
the controls.
If you select a control from the list, you can then pick an event for that
control from a second dropdown in the code editor's upper right. If you
select an event, the code editor generates a corresponding empty event
handler for you.
To make referring to the code lines easier, code editor may display line
numbers too.
To switch between design and code view:
Visual Studio (Express 2013 for Windows Desktop) and its Windows
Forms Designer are configured to be quite usable. As soon as you open
the Designer you can immediately use the Toolbox to place controls on
the form.
However the Windows Forms Designer allows you to add controls to a form
in a several different ways.
1. First way is, if you double-click a control on the Toolbox, Visual Studio
places an instance of the control on the form in a default location and at a
default size. Then you can use the mouse to relocate and resize the
control.
2. Second way is, if you click a control in the Toolbox, the mouse cursor
changes while the mouse is over the form. The new cursor looks like a
plus sign with a small image of the control's Toolbox icon next to it. If you
click the form, Visual Studio adds a control at that location with a default
size.
Figure 1 (with selected control in the toolbox you click the form)
Figure 2 (Visual Studio adds a control at the location you've clicked)
?
1
Dim CheckBox1 As New CheckBox
2
Me.CheckBox1.AutoSize = True
3 Me.CheckBox1.Location = New System.Drawing.Point(31, 144)
4 Me.CheckBox1.Name = "CheckBox1"
6 Me.CheckBox1.TabIndex = 4
Tip #1: If your Properties window is hidden just press F4 on your keyboard
or select Properties Windows on the View menu.
?
1 ' this code sets the checkbox's text property
As soon as you add some controls to your form and write appropriate code
for them you will want to test your application. To do that you simply start
debugging!
If there are no errors your app will run shortly after you started debugging.
Please watch the Output Windows as this window displays compilation
results and output produced by Debug and Trace statements.
The comments are one of the few typographic code elements that make a
programs structure a bit easier to understand.
They do not execute and they are ignored by the compiler but are an
important part of how you organize your code.
The comment starts with a single quotation mark (') and everything until
the end of the line is part of the comment and as mentioned ignored by
Visual Basic.
NOTE: you cannot use line continuation characters to make a multi-line
comment nor you can have block comment as they are not supported in
VB.NET
You should always use comments to make your code clear and
understandable.
' checking if the username and password are valid
If Username.Text = "admin" AndAlso Password.Text =
"mypwd" Then
Dim mForm As Form = MainForm ' instantiate the
MainForm
mForm.ShowDialog() ' display the main form
Me.Close() ' close the login form
Else
' incorrect username or password provided
Username.Text = String.Empty ' reset the
username textbox
Password.Text = String.Empty ' reset the
password textbox
' let them know that username or password is
incorrect
MessageBox.Show("Incorrect username or
password. Please try again!")
End If
1. Scope - it indicates the scope levels from where the variable can be
accessed (private, public etc.)
2. Data type - can be an Integer, String, Boolean etc.
3. Accessibility - determines what code in other modules can access the
variable
4. Lifetime - determines how long the variable value is valid
For instance, any variable declared inside a subroutine has scope equal
to the subroutine and cannot be accessed by the code outside of the
subroutine.
?
1 Private Sub DoSomething()
4 End Sub
6 Console.Write(InsideVariable)
In Visual Basic you use the keyword Dim to tell Visual Basic officially that
you want to declare a variable.
You can avoid the Dim only if you specify Private, Public, Protected etc.
however a variable delared using a Dim keyword is Private by default so
the next two declarations are identical:
?
?
1 C = A * B
+ Addition 2+2 4
- Substraction 4-2 2
- Negation 1 operand* -2 -2
* Multiplication 2*2 4
/ Division 4/2 2
As shown in the table, \ operator performs integer division and returns the
result of dividing the first operand by the second, dropping any remainder.
It's very important to understand that the result is truncated toward zero,
not rounded.
The Mod operator returns the remainder after dividing the operand by the
second one e.g. 5 Mod 2 = 1 because 5 = 2 * 2 + 1
The following example code prints all even numbers in range 1 to 10:
?
1
For i As Integer = 1 To 10
2 If i Mod 2 = 0 Then
4 Else
End If
6
Next
7
It often happens that you need to prompt the user to enter an expected
value.
?
Dim nl As String = Environment.NewLine
1
' say hi and ask them for a name
2
Console.WriteLine("Hello there. What's your name?" + nl)
3
Dim input = Console.ReadLine()
4
' say something to the user
5 Console.WriteLine(nl & "Nice to meet you " & input & "." & nl)
6 Console.Read()
?
1
2 Dim prompt As String = String.Empty
3 Dim title As String = String.Empty
4 Dim defaultResponse As String = String.Empty
5
Dim answer As Object
6 ' Set prompt.
7 prompt = "Hello there. What's your name?"
8 ' Set title.
9 title = "Getting user input"
10 ' Set default value.
defaultResponse = "Your name here"
11
12 ' Display prompt, title, and default value.
13 answer = InputBox(prompt, title, defaultResponse)
14
15 ' Say something to the user
16 Messagebox.Show("Nice to meet you " & answer)
17
When you want to choose between executing two different blocks of code
depending on the result of a Boolean expression, you usually use an if
statement.
?
1 If booleanExpression = True Then
2 DoSomething()
3 Else
4 DoSomethingElse()
5 End If
?
1
Dim IsUsernameValid As Boolean
2
If Username.Text = "user" Then
3 IsUsernameValid = False
5 IsUsernameValid = True
6 Else
IsUsernameValid = False
7
End If
8
' NOTE: an if statement is optionally followed by an ElseIf clause as well
9
Conditional and Logical Operators in
Visual Basic .NET
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013
This lesson describes the Conditional and Logical operators.
?
1 Dim IsUsernameValid As Boolean
?
1 Dim IsUserValid As Boolean
?
1 Dim IsUserValid As Boolean
2 IsUserValid = (Username.Text = "admin") AndAlso (Password.Text = "mypwd")
?
1 Dim IsPasswordValid As Boolean
?
1 Dim IsPasswordValid As Boolean
?
1 Dim IsUsernameValid As Boolean
Visual Basic provides two concatenation operators: + and &. Both join
two strings together.
Using & can also make your code faster and lead to fewer problems
because it lets Visual Basic know that the operands are strings.
?
1 ' make a full name concatenate the first and the last name
Console.WriteLine(FullName)
5
For example the string "Hello World!" is not only 10 characters long.
Rather its length is 12 because of the empty space and the exclamation
mark.
this example demonstrates the Length property:
?
1 Dim MyString As String = String.Empty
2 MessageBox.Show(MyString.Length) ' = 0
3 MyString = "Hello World!"
4 MessageBox.Show(MyString.Length) ' = 12
The Substring method allows you to grab a set of characters from any
position in the string and can be used in two ways.
?
1 Dim MyString As String = "Hello World!"
When you're providing only one parameter it tells the Substring to start at
the given position and copy everything right up to the end of the string
data.
?
1 Dim MyString As String = "Hello World!"
The index value gives the index numbered (zero based) of the parameter
that should be inserted in that placeholder's position.
?
1 Dim project As String = "World"
?
1 Dim amount As Decimal = 500
The format_string is also optional and indicates how the item will be
formatted:
?
1 Dim amount As Decimal = 500
?
1 Dim MyStringA As String = "Hello World!"
4 ' Do something
5 Else
?
1 Dim MyStringA As String = "Hello World!"
4 ' Do something
5 Else
?
1 Dim MyStringA As String = "Hello World!"
4 ' Do something
5 Else
?
1 Dim MyStringA As String = "Hello World!"
3 If MyStringA.CompareTo(MyStringB) = 0 Then
4 ' Do something
5 Else
You could also use String.Equals to find out if two strings have the same
value
I would suggest using of Select Case whenever you have more than three
else if statements.
In a Select Case statement, every case has its own block.
As you see Select Case provides more compact and more elegant code
than multiple nested if statements.
We can safely say that For Next loop is the simplest loop to understand
and it's certainly the most commontype of looping statement in Visual
Basic.
?
For variable [As data_type ] = start_value To stop_value [ Step increment ]
1
statements
2
[Exit For]
3
statements
4 [Continue For]
5 statements
Next [ variable ]
6
The variable value is the looping variable that controls the loop so when
the app reaches the For statement, it sets variable equal to start_value .
Then it compares variable to stop_value and if variable has passed
stop_value, the loop exits.
?
1 For i As Integer = 0 To 10 Step 1
2 Console.WriteLine(i)
3 Next i
Visual Basic doesn't require the variable's name in the Next statement, but
it makes the code easier to read
Note: the loop may not execute even once depending on the start and
stop values.
The For Each loop iterates over the items in a collection, array, or other
container class that supports For Each loops.
?
1
For Each variable [As object_type ] In group
2 statements
3 [Exit For]
4 statements
5 [Continue For]
statements
6
Next [ variable ]
7
In this loop, group is a collection, array, or other object that supports For
Each and it's important that control variable must be of a data
type compatible with the objects contained in the group.
If the control variable's type is not compatible with an object's type, the
program generates an error.
?
1 For Each user As User In Users
2 Console.WriteLine(user.Username)
3 Next user
Note: Visual Basic doesn't require the variable's name in the Next
statement, but it makes the code easier to read
To leave a For loop before it would normally finish you use the Exit For
statement.
For example, the following code loops through an array looking for value
10.
When it finds the element with value 10 it uses Exit For to immediately
stop looping.
?
1 Dim array() As Integer = {1, 3, 5, 7, 10, 20, 50}
5 Exit For
End If
6
Next
7
The Exit For statement exits only the For loop immediately
surrounding the statement.
If a For loop is nested within another For loop, the Exit For statement only
exits the inner loop.
When you want to start your loop over again you use the Continue
For statement which makes the loop jump back to its For statement and
increment its looping variable.
This is particularly useful if the program doesn't need to execute the rest of
the steps within the body of the loop and wants to start the next iteration
quickly.
?
1
Dim array() As Integer = {1, 3, 5, 7, 10, 20, 50}
2 For i As Integer = 0 To array.GetUpperBound(0)
5 Continue For
End If
6
Console.WriteLine(array(i))
7
Next
8
One of the Do Loop statements variations is the Do Until and it keeps going
until something happens (until a condition is true).
?
1 Do {Until} condition
2 statements
3 [Exit Do]
4 statements
5 Loop
Meaning the Until loop keeps running until the condition is satisfied. In this
type of loop, there is not a control variable in itself so you have to keep
track of the current position of the loop.
counter -= 1
4
'print the array elements
5
Console.WriteLine(array(counter))
6
Loop
7
' Visual Basic won't execute the statements because counter is already greater
8 than 0
?
1 Do {While} condition
2 statements
3 [Exit Do]
4 statements
Loop
5
This While loop keeps running as long as the expression is True. As soon
as the expression becomes False, it stops.
?
1
Dim array() As Integer = {1, 3, 5, 7, 10, 20, 50}
2 Dim counter As Integer = array.GetUpperBound(0) + 1
4 counter -= 1
6 Console.WriteLine(array(counter))
Loop
7
' this loop prints out all the array elements
8
The Exit Do statement allows the program to leave the nearest enclosing
loop before it would normally finish.
Meaning, it lets you end a loop early and can be used e.g. when testing an
endless loop.
?
1 Do {While} condition
2 statements
3 [Exit Do]
4 statements
Loop
5
In the following example, condition stops the loop if the counter is equal to
zero. The If statement in the loop, however, causes the Exit Do statement
to stops the loop when the array element value is 10:
?
1
Dim array() As Integer = {1, 3, 5, 7, 10, 20, 50}
2
Dim counter As Integer = array.GetUpperBound(0) + 1
3
Do While counter > 0
4 ' quit the do loop if element's value is 10
5 If array(counter) = 10 Then
6 Exit Do
7 End If
8 counter -= 1
When you work with loops, you can easily create an infinite loop. This is a
loop that is never finishing. This logic error is one of the most difficult
to find and troubleshoot so you should be very careful when you work
with loops to not end up with loops whose exit condition can never be
satisfied.
Tip: Whenever you suspect a program has fallen into an infinite loop, just
force the program to stop using Debug menu - Stop Debuging or use Ctrl
+ Shift + A in your Visual Studio.
This will stop the program immediately.
The following code is a classic example of an infinite loop and will not
stop:
?
1 Dim counter As Integer = 10
4 'Do something
5 Loop
' this loop just keeps going forever as counter is always greater than 0
6
MessageBox is one of these built-in dialog boxes that help you to provide
a rich user interface in your front-end applications.
As a developer you will use this dialog pretty often as it lets you to
display custom messages to your users and accept their input regarding
the choice that they have made.
It's very flexible and provide many properties and methods that enable you
to customize them to suit your needs.
You can customize it to display a variety of icons with your messages and
choose which buttons to display while still maintaining the standard
look of Windows Forms application.
MEMBER ICON
MEMBER DESCRIPTION
OK an OK button
To display the message box you call its Show method which by the way,
cannot be inherited because it's shared so the only way to change the
default MessageBox behaviour is using windows hooks.
?
1 MessageBox.Show( "The username or password you entered is incorrect", _
?
Dim msgRslt As MsgBoxResult = MsgBox("Are you 21 years old or older?.", MsgBoxStyle.Y
1
If msgRslt = MsgBoxResult.Yes Then
2
MsgBox("You are welcome to join.")
3
ElseIf msgRslt = MsgBoxResult.No Then
4
MsgBox("You must be at least 21 years old to join.")
5
End If
6
When you need to prompt the user to enter an expected value you use
InputBox.
It displays a prompt in a dialog box, waits for the user to enter value and
finally returns a string containing the entry.
?
1 Dim prompt As String = String.Empty
4
Dim answer As Object
5
' Set prompt.
6
prompt = "Hello there. What's your name?"
7
' Set title.
8
title = "Getting user input"
9
' Set default value.
10 defaultResponse = "Your name here"
11
14
17
?
1 [ accessibility ] Sub routine_name [ paramaters ]
2 statements
3 End Sub
The name of the subroutine must be a valid Visual Basic identifier and
should begin with a letter or an underscore.
If you include ByVal keyword in the declaration, the subroutine makes own
local copy of the parameter and then can modify this value without
affecting the value of the original parameter.
If you include ByRef keyword in the declaration, the subroutine does not
create a separate copy of the parameter variable.
Instead, it uses a reference to the original parameter passed into the
subroutine and any changes the subroutine makes to the value are
reflected in the calling subroutine too.
2 ' do something
3 End Sub
Most importantly the subroutines allow you to extract code that you may
need to use under more than one circumstance and place it in one
location where you can call it as needed:
Then whenever you want to warn your user you simply call the
ShowWarningMessage subroutine e.g.
?
1 ShowWarningMessage("Please provide a valid email.", "Warning")
Meaning, they are basically the same as subroutines, except that they
return some sort of value.
?
1 [ accessibility ] Function function_name [ paramaters ] [As return_type ]
2 statements
3 End Function
The name of the function must be a valid Visual Basic identifier and
should begin with a letter or an underscore.
If you include ByVal keyword in the declaration, the function makes own
local copy of the parameter and then can modify this value without
affecting the value of the original parameter.
If you include ByRef keyword in the declaration, the function does not
create a separate copy of the parameter variable.
Instead, it uses a reference to the original parameter passed into the
subroutine and any changes the function makes to the value are reflected
in the calling function as well.
To declare an optional parameter you use Optional keyword, so the code
that uses it may omit that parameter.
?
1 Private Function GetValue(Optional ByVal entry As String = "") As String
2 Return entry
3 End Function
The function can set its return value either by setting its name equal to the
value it wants to return, or by using the Return keyword followed by the
value that the function should return.
?
1
Private Function GetValue(Optional ByVal entry As String = "") As String
2
Dim result As String = String.Empty
3 If entry = "" Then
5 Else
6 result = entry
End If
7
GetValue = result
8
End Function
9
OR
?
1 Private Function GetValue(Optional ByVal entry As String = "") As String
5 Else
result = entry
6
7 End If
8 Return result
End Function
9
Most importantly the functions allow you to extract code that you may
need to use under more than one circumstance and place it in one
location where you can call it as needed:
?
1 Private Function CubeVolume(ByVal x As Double) As Double
3 result = x * x * x
4 Return result
End Function
5
Then whenever you want to calculate the cube volume you simply call
the CubeVolume with side of the cube as parameter provided e.g.
?
1 Console.WriteLine("The volume of the cube is: " & CubeVolume(5))
It makes the program stop executing the current subroutine but unlike
GoTo and such, cannot make the program jump around arbitrarily, possibly
leading to spaghetti code.
3 If i = 10 Then
Else
5
MyTextBox.Text &= "The value of i is: " & i.ToString &
6 Environment.NewLine
7 End If
8 Next
9 End Sub
A global variable is a variable with global scope, and unlike the local
variable can be accessed from anywhere.
?
1 Module GlobalVariables
4 End Module
2 ' or
3 Me.Text = GlobalVariables.global_FormText
Whenever you need to hold a list of similar or related data you use an
array. Arrays can only hold a single data type.
To declare an array you use parentheses and unlike the standard variable
it can hold more than one value.
?
1 Dim days(6) As String
To initialize an array you place the values inside curly braces separated
by commas. The following code initializes a one-dimensional array of
Strings:
?
' Like this:
1
Dim days(6) As String = _
2
{ "Sunday", "Monday", "Tuesday", "Wednesday", _
3 "Thursday", "Friday", "Saturday" }
6
' Declare the array
7
Dim days(6) As String
8
9
' Populate the array
10
days(0) = "Sunday"
11
days(1) = "Monday"
12
days(2) = "Tuesday"
13 days(3) = "Wednesday"
14 days(4) = "Thursday"
15 days(5) = "Friday"
16 days(6) = "Saturday"
17
18
?
1 Dim days(6) As String = _
This structure supports many helpful methods that you can be used for
various tasks like getting yesterday's date or the current month name.
When you declare the DateTime variable its default value is 1/1/0001,
12:00 AM midnight e.g.
?
1 Dim defaultDate As DateTime
?
1 Dim TodaysDate As DateTime = DateTime.Now
Then you can go further and for example, get the Yesterday's date:
?
1 Dim YesterdayDate As DateTime = DateTime.Now.AddDays(-1)
You can also display the result in a format defined by a specific format
specifier e.g.
?
2 Console.WriteLine(TodaysDate.ToString("g"))
You can additionally format your date using specific culture e.g.
Console.WriteLine(TodaysDate.ToString("F"), New
2
System.Globalization.CultureInfo("fr-FR"))
3 ' this code yields to: dimanche 11 Mai, 14:26:00
If you don't have an error handling in your code and an error occurs, your
users receive a message about an unhandled exception, and then the
program may terminate.
This message is not a user-friendly and does not inform the users about
the true nature of the error or how to resolve it.
The unhandled exception could also cause users to lose the data that they
were working with which can be very frustrating.
Esentially you execute the code that might throw an exception in the Try
block, and you handle the errors in the Catch block.
Usually in the Finally block you keep your dispose and close
methods like connection.Close() that unsures that your connection is
closed no matter what error you get in the Try block.
?
1
Dim connection As New SqlConnection("connection_string")
2
Try
3
connection.Open()
4
Dim command As SqlCommand = connection.CreateCommand
5
command.CommandText = "SELECT FieldName FROM Table1"
6
Dim reader As SqlDataReader = command.ExecuteReader
7 While reader.Read
9 End While
10 reader.Close()
command.Dispose()
11
Catch ex As Exception
12
' display the error
13
Console.Write(ex.Message)
14
Finally
15
connection.Close() ' this code will be always executed
16
End Try
Breakpoints in Visual Basic .NET
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013
This lesson describes the breakpoints in Visual Basic .NET
Sometimes when you try to debug only a small part of already large
program you will want your code to run up to a certain point and then stop
there.
The breakpoints can be set anywhere in the code and that code will
execute to that point and then will stop.
Note: Execution of the code stops before executing the code on which the
breakpoint is set.
The breakpoints can be set when you write your code, or you can set
them at runtime by switching to your code and setting the breakpoint at
the desired location.
To set a breakpoint you click the gray margin next to the line you want to
debug or press F9 on your keyboard.
When Visual Studio encounters a breakpoint, execution of your code
halts, and then the program gets in a break mode.
To make your application available for distribution in a local file, FTP site,
or web site you need to first publish it.