Friday, 25 October 2013

More variables in VB.NET: short, integer, single, double, decimal, long etc...

More variables in VB.NET:

we have met 2 variables such as: integer and string till now.
Now we will get few more regarding the variables..

1.SHORT:

First place the TEXTBOX and BUTTON in the FORM.

change the properties of TEXTBOX as:
Name the TEXTBOX as txtNumbers by using name property.
And remove the text and empty the TEXTBOX by using text property in property window.

change the properties of BUTTON as:
Change the text of the BUTTON as result using text property in property window.

The form design is:


Then double click on the Result BUTTON.
Then type the code below.

     Dim testNumber As Short

        testNumber = Val(txtNumbers.Text)

      MsgBox(testNumber)





Then RUN the FORM.
Goto DEBUG ---->> START DEBUGGING.
Then type 1 in the TEXTBOX then click on Result.
It displays the result in the MESSAGE BOX.




In this manner type the numbers in TEXTBOX by placing a digit to next place of already existed digit in this manner.



In this manner we can print the numbers until 5 digits.ex: 12345
But we can not proceed to 6 digit numbers.
when we try to goto 6 digit numbers it will display an error message.




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




Why the error raised ??

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 we use As Short we'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 we run your program again, and then enter 32768, we'll get the same Overflow error message.


 If we change it once more to -32769, we'll get the error message as well. 
So it's not just 6 numbers.
A Short Type can't handle - it's 5 numbers range(-32 768 to 32 767 ) above or below the values specified.

The solution for this problem is::
change the Variable SHORT into INTEGER.

Integer:

Now start the program and try again with INTEGER variable, adding numbers one at a time to the TEXTBOX, and then clicking the Result button. 

How far did you get this time?
change the above code into below by introducing INTEGER instead of SHORT
Dim testNumber As Integer

        testNumber = Val(txtNumbers.Text)

        MsgBox(testNumber)

          
    If you started at 1 and added the numbers in sequence, we should have been allowed to enter 1234567890.



 One more number and Visual Basic gave us the Overflow error message.




 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 we want a really, really big number we can use As Long.

Dim testNumber As Long

        testNumber = Val(txtNumbers.Text)

        MsgBox(testNumber)



The result is:


In this result the number after 16 digits it was rounded.



later sections we will get the details about the FLOATING numbers.


<---- Previous page                                            NextPage ---->


VB.NET HOME PAGE