Today I will tell you about using the Option Explicit statement. The Option Explicit has two modes. On and Off mode. If Option Explicit mode in ON , you have to declare all the variable before you use it in the program . If not , it will generate a compile-time error whenever a variable that has not been declared is encountered .If the Option Explicit mode is OFF , Vb.Net automatically create a variable whenever it sees a variable without proper declaration
By default the Option Explicit Statement is always On.
Option Explicit Off
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
someVariable = "Option Explicit ON"
MsgBox(someVariable)
End Sub
End Class
Here "someVariable" is not declared , because the Option Explicit Of , without any compiler error you can continue the program.
VB Coding Tutorials
Wednesday, May 15, 2013
Saturday, May 4, 2013
Using Nothing Syntax in VB.NET
When coding in VB.NET you will sometimes want to declare a variable or a string as Nothing. There is a really way to do this and it takes about 1 line of code. Here is how you can do it.
Sub Main2() Dim sString As String = Nothing If sString = Nothing Then Msgbox("You've got nothing") End If
You can also ofcourse combine Nothing with an If-else statement which will help you test to see if the string is actually Nothing.
Saturday, April 27, 2013
How to use the ProgressBar control!
Today I am going to show you how to use the progress bar control in VB.NET.
This is fairly simple to do and really takes only 1 line of code.
ProgressBar1.Value = ProgressBar1.Value + 55
This will increment the ProgressBar's value by 55 from whatever value it is. As you can see this is very simple and will be very useful for you. :D
Wednesday, April 24, 2013
[VB.NET] Declaring Constants
Today I am going to show you how to declare constants in VB.NET.
We are going to be working with this example program today.
This declares that the variable are is PI * radius * radius.
And then at the end this outputs the area :D
We are going to be working with this example program today.
Module constantsNenum Sub Main() Const PI = 3.14149 Dim radius, area As Single radius = 7 area = PI * radius * radius Console.WriteLine("Area = " & Str(area)) Console.ReadKey() End Sub End ModuleThis program will show you basically how to declare constants.
Dim radius, area As SingleThis line of code declares that radius and area should be Single numbers.
radius = 7This basically says that radius = 7.
area = PI * radius * radius
This declares that the variable are is PI * radius * radius.
And then at the end this outputs the area :D
Sunday, April 21, 2013
Using Variables in VB.NET
Today I am going to show you an easy way to use variable in your VB.NET application.
The easiest way to do this is using the statement "Dim". This will make it so you can declare all of your variables...so for example
Dim mystring as String
This will declare the variable as a String....and you can also do something like this....
Dim mystring as String = "hello"
This will actually give the variable mystring a value which in this case is "hello"
But let's say you want to declare something other than a string...like an integer for example...this can be easily done by setting the variable to an integer instead of a String like this...
Dim mynumber as Integer = 13
When declaring Integer's you do not need to use strings as it is automatically detected...
The easiest way to do this is using the statement "Dim". This will make it so you can declare all of your variables...so for example
Dim mystring as String
This will declare the variable as a String....and you can also do something like this....
Dim mystring as String = "hello"
This will actually give the variable mystring a value which in this case is "hello"
But let's say you want to declare something other than a string...like an integer for example...this can be easily done by setting the variable to an integer instead of a String like this...
Dim mynumber as Integer = 13
When declaring Integer's you do not need to use strings as it is automatically detected...
I hope this tutorial helped you learn a little more about variables in VB.NET!
Wednesday, April 17, 2013
For Loops in VB.NET
This first type of loop we'll look at is called a For Loop. It is the most common type of loop you'll use when you program. We'll use one to add up our 4 numbers, and then discuss the code. Study the following. In fact, create a new Project. Add a button to your new Form. Double click your new button and type the following code for it:
Dim answer As Integer
Dim startNumber As Integer
Dim startNumber As Integer
answer = 0
For startNumber = 1 To 4
answer = answer + startNumber
Next startNumber
MsgBox answer
Run the program, and see what happens when you click the button. The number 10 should have been displayed in your message box.
[VB.NET] If-Then Statements
What is conditional logic? Well, it's something you use in your daily life all the time, without realising you're doing it. Suppose that there is a really delicious cream cake in front of you, just begging to be eaten. But you are on a diet. The cake is clearly asking for it. So what do you do, eat the cake and ruin your diet? Or stick to your diet and let somebody else have that delicious treat? You might even be saying this to yourself.
An example would be. If I eat the cake Then my diet will be ruined.
Here is an example of using if-then statements
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim firstname As String
firstname = "Bill"
If firstname = "Bill" Then MsgBox("firstname is Bill")
If firstname = "Bill" Then MsgBox("firstname is Bill")
End Sub
Run the program and see what happens. You should get a Message Box with the words "firstname is Bill" in it.What we did was to set up a string variable and put the name "Bill" into it. When then used conditional logic to test what was in the variable. In fact, we used an If statement.
We said: If the variable called firstname holds the value "Bill" Then display a Message Box
Subscribe to:
Posts (Atom)