Monday, October 4, 2010

Create a Variable in VB.NET

Page : 1 2 3 4 5 6 7 8 9 10 11 12 13 14


How to Create Variables in VB .NET


With Visual Basic .Net and most programming languages, what you are doing is storing things in the computer's memory, and manipulating this store. If you want to add two numbers together, you put the numbers into storage areas and "tell" Visual Basic to add them up. But you can't do this without variables. So a variable is a storage area of the computer's memory. Before using variable we have to declare that.

Variable Declaration

Syntax:

Dim <> As

Example

Dim a As Integer
Dim b As Integer
Dim c As Object
Dim str As String

Using Variable

a = 2
b = 4

Dim

Short for Dimension. It's a type of variable. You declare that you are setting up a variable with Dim Keyword.
a
This is a variable. In other words, our storage area. After the Dim word, Visual Basic is looking for the name of your variable.
As Integer
We are telling to Visual Basic that the variable is going to be a number (integer). The above example “a” is going to be a Integer.


VB NET Variables Example

We have met two variable types so far - As String and As Integer. Put a textbox and a Button on your new form.
Change the Properties of the Textbox to the following
Name: txtNumbersText: just delete the default Textbox1, and leave the textbox blank
Change the Properties of the Button to the following:
Text: Answers
Your Form should look something like this:


Double click on the Button to bring up the code window. Type the following code for your Button.

Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) _Handles Button1.Click
Dim testNumber As IntegertestNumber = txtNumbers.TextMsgBox testNumber
End Sub

Notice that there is a new Type of variable declared - As Short. This means "Short Integer". We'll see what it does.

Run your programme. While it's running, do the following:
Enter the number 1 into the textbox, and click the Answers button
The number 1 should display in the Message Box
Add the number 2 to the textbox and click the Button
The number 12 should display in the Message Box
Add the number 3 to the textbox and click the Button
The number 123 should display in the Message Box
Keeping adding numbers one at a time, then clicking the button
How many numbers did you get in the textbox before the following error message was displayed? (Click Break to get rid of it.)



You should have been able to enter 12345 quite safely. When you entered 123456 and clicked the button, that's when the error message displayed.


When you click the Break button, you are returned to the coding environment. You'll see the problem line highlighted in yellow:


testNumber=val(txtNumber.Text)


But your programme will still be running. So click Debug > Stop Debugging to return to the normal code window.


An Overflow happens when you try to put too much information into a variable that can't handle it. The reason we got an error message after just 6 numbers was because of the variable type. We had this


Dim testNumber As Short


And it's As Short that is causing us the problems. If you use As Short you're only allowed numbers up to a certain value. The range for a Short variable is -32 768 to 32 767. When we entered 6 numbers, Visual Basic decided it didn't want to know. If you run your programme again, and then enter 32768, you'll get the same Overflow error message. If you change it once more to -32769, you'll get the error message as well. So it's not just 6 numbers a Short Type can't handle - it's 5 numbers above or below the values specified.


So what's the solution? Change the variable Type, of course!
Change the variable to this


Dim testNumber As Integer


Now start the programme and try again, adding numbers one at a time to the textbox, and then clicking the Command button. How far did you get this time?
If you started at 1 and added the numbers in sequence, you should have been allowed to enter 1234567890. One more number and Visual Basic gave you the Overflow error message, right? That's because variable types with As Integer also have a limitation. The range you can use with the As Integer variable type is -2,147,483,648 to 2,147,483,647. If you want a really, really big number you can use As Long.


Dim testNumber As Long


But these will get you whole numbers. Leave your number on As Integer. Run your programme again and enter a value of 123.45 into your textbox. Press the button and see what happens.


VB will chop off the point 45 bit at the end. If you want to work with floating point numbers (the .45 bit), there are three Types you can use:


Dim testNumber As SingleDim testNumber As DoubleDim testNumber As Decimal
Single and Double mean Single-Precision and Double-Precision numbers. If you want to do scientific calculations, and you need to be really precise, then use Double rather than Single: it's more accurate.


The As Decimal Type is useful when you want a Specific number of decimal places.