Tuesday 17 March 2015

Visual Basic Programming for Beginners: Declaring Variables and Assigning values (03)

Before proceeding, do read Visual Basic Programming for Beginners: Getting Started (02)
 Visual Basic Programming for Beginners: Declaring Variables and Assigning values (03)
In the previous post we have learned how to get started with VB in Visual Studio and we also created a "Hello World" Program. Hope that you are now familiar with Visual Studio environment and the Structure of a VB program.

So now its time to proceed forward in our journey of learning VB

Now we will learn how to declare variables and assign values to them.

Take a look at following code
x=7
y=x+3
Console.WriteLine(y)

Above code assigns "x" value "7" and "y" value "x+3", so its pretty easy to understand that Console.WriteLine(y) will display value "10"

Here "x" and "y" are variables which can be defined as A bucket in computer memory in which you can put values, overwrite existing values and also retrieve values.

Variables can be made to create different type of values like integer,character, string, decimal no.s and many more.

Declaring Variables(syntax):
Integer Type variable
Dim x As Integer
Dim y As Integer
"Dim" keyword is used to declare a variable i.e to provide dimensions to the variable in the computer memory
So now let's implement this in Visual Basic.

  • Program To add integer values

Module Module1
  Sub Main()
          Dim x As Integer
          Dim y As Integer
          x=7
          y=x+3
          Console.WriteLine(y)
          Console.ReadLine()
  End Sub
End Module

Note: To make your program more understandable, make comments in your program
How to make comments in VB?
We can make comments in VB by the use of Single quote('), and VB supports only single line comments
It goes like this:
'This is a comment
'Compiler won't detect it
'Only single line comments are possible
'Every character after single quote counts in comment 

String Type variable
Dim myname As String
myname = "MAK"
alternate way
Dim myname As String = "MAK"
or
Dim myname = "MAK"

  • Program to print String On console

Module Module1
  Sub Main()
          Dim myname As String
          myname = "MAK"
          Console.WriteLine(myname)
          Console.ReadLine()
  End Sub
End Module

Generic Type variable
Generic datatype variables are those who can store data of any datatype
It goes like this
Dim x
And that's all, you have created a generic type variable "x" which can store any type of data

  • String Concatenation in Visual Basic
"&" act as a string concatenation operator in VB and program to use this is as follows
  
  • Program to concatenate two datatype as string
Module Module1
  Sub Main()
          Dim x As Integer = 7
          Dim myname As String = "MAK"
          Dim myfirstTry As String = x & myname
          Console.WriteLine(myfirstTry)
          Console.ReadLine()
  End Sub
End Module

Compile this code and check out the output, it is interesting.

Type Casting:

  • Integer To string
           Dim x As Integer = 7
           Dim myname As String = x.ToString()
             or
          Dim myname As String = CStr(x)
   
So stay tuned and keep coding and practicing till next post.   

No comments:

Post a Comment

[How to] Connect an android device [using MTP] on Linux [Ubuntu 14.04 LTS] 100% working

I recently moved to Linux[Ubuntu 14.04 ] from windows 8.1 because it hangs a lot on my lappy. Apart from the cool features of Ubuntu , one...