Press "Enter" to skip to content

Declaring and Using Variables in VB (Dim)

 Tip Abstraction:

       This visual basic tutorial describes how to declare and use a variable with assigning values



In our previous lesson we assigned a text to a textbox using the code  TextBox.Text = “Yourowncodes”.
Here we directly assigned the string “yourowncodes” as the textbox text.
But if you assign the text ‘yourowncodes’ to a variable, then we can use its value anywhere.

Let us learn more with examples.

Here I am going to assign a string ‘yourowncodes’ to a variable ‘sitename’:

Dim sitename As String

Dim stands for Declare in Memory. Dim sitename means declaring a variable named sitename, and As String means the storage formate of the variable sitename, which is String.
So there is a variable named ‘sitename’ and its format is String. That is the variable sitename can only store string typed values.

Note that you can use any name as the variable name but:

  • Spaces and special character like *,-, etc are not allowed, but can use underscore ( _ )
  • You can’t user reserved keywords like dim, as, integer, for, if, while, etc

Now I am going to the set a value into the variable ‘sitename’
This is simple like assigning a value into the textbox.

sitename = ‘www.yourowncodes.com’

Here the string ‘yourowncodes’ is assigned to the variable sitename. We must use quotes for ‘string’ data types.

Now the variable sitename contains a string value of ‘yourowncodes’. And now we are going to the set this variable into value of the textbox. This is similar to assigning a string to the textbox (username) that we shown in the previous article.

username.Text = sitename

Now the total code:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim sitename As String

sitename = “www.yourowncodes.com”

username.Text = sitename

End Sub

End Class


— Where ‘username’ is the Textbox name.
— We wrote above code inside the ‘Form load’ event, that we shown in previous lesson.

Declaring a string variable in VB

Let us create an Integer typed variable:

Dim MyNumber As Integer

Here is a varibale named MyNumber with the formte Integer. So this variable MyNumber can store numbers, but not string typed values.

Let us assigning a number to the vaiable number.

MyNumber = 500

Now again assign the variable to the textbox ‘username’

  username.Text = MyNumber

Output:

Assigning and using a Integer variable


If you want to declare fractional numbers, then declare the variable as ‘double’.

Dim percentage As DoublePercentage = 98.8


We can assign value directly while declaring the variable like this:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim num1 As Integer = 5

Dim num2 As Integer = 10

Dim num3 As Integer

num3 = num1 + num2

valuebox.Text = num3

End Sub

End Class

Here varaiable num1 gets a value of 5 and num2 gets a value of 10. Then the num3 is the sum of variables num1 and num2 (symbol + is used). Then the value of num3 is assigned to textbox named ‘valuebox’ using code  valuebox.Text = num3.

Output:

Add two numbers in vb output

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: