Tuesday, October 5, 2010

Operators in VB.NET

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

Operators Comes with many built-in operators that allow us to manipulate data. An operator performs a function on one or more operands.

For example, we add two variables with the "+" addition operator and store the result in a third variable with the "=" assignment operator like this: a + b = c. The two variables (a ,b) are called operands.

There are different types of operators in VB.NET


  • Arithmetic Operators

  • Concatenation Operators

  • Comparison Operators

  • Logical / Bitwise Operators

Arithmetic Operators

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



Arithmetic operators are used to perform arithmetic operations that involve calculation of numeric values. The table below summarizes them:



Example for Arithmetic Operators

Dim a,b,c As Integer
a=10
b=7

Addition - c = a + b 17
Multiplication - c = a * b 70
Division - c = a / b 1.42857142
Modulus - c = a Mod b 3
Subtraction - c = a - b 3
Exponentation - c = a ^ b 10000000
Integer Division - c = a \ b 1

Concatenation Operators

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

Concatenation operator is used to join multiple strings into a single string.
There are two concatenation operators, + (Plus) and & (Ampersand)








Example
Dim str As String
Dim s1,s2 As String
S1= “MCA”
S2= “Department”
Str=s1+s2
Result will be : MCA Department
Str=s1&s2
Result will be: MCA Department

Comparison Operators

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

A comparison operator is used to compare two operands and returns a logical value based on whether the comparison is true or not.


Example

Dim a,b,c As Integer


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
a = 10
b = 7

If a = b Then
MsgBox("A is equal to B")

ElseIf a <> b Then
MsgBox("A Not equal to B")

ElseIf a > b Then

MsgBox("A is Grager than B")

ElseIf a <>= b Then
MsgBox("A is Grager than or equal to B")

ElseIf a <= b Then MsgBox("A is Less than or equal to B") End If End Sub Output will be display in MessageBox


Logical and Bitwise Operators

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


The logical operators compare Boolean expressions and return a Boolean result. In short, logical operators are expressions which return a true or false result over a conditional expression.

The table is given below.

Example


n = Not 10 > 7
MsgBox("N is True")

n = Not 10 <>

Example for Operators

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

Form Design

Place the 3 Lebel, 3 TextBox and 7 Button in your Form using ToolBox
Change the Properties of the Label to the following
Text: Number1
Text: Number2
Text: Result
Change the Properties of the Textbox to the following
Name: TxtNumber1Text: just delete the default Textbox1, and leave the textbox blank
Name: TxtNumber2Text: just delete the default Textbox2, and leave the textbox blank
Name: TxtResultText: just delete the default Textbox3, and leave the textbox blank
Change the Properties of the Button to the following:
Name: AddButtText: just delete the default Button1, and Type Add
Name: AddSubText: just delete the default Button2, and Type Sub
Name: MulButtText: just delete the default Button3, and Type Mul
Name: DivButtText: just delete the default Button4, and Type Div
Name: IntDivButtText: just delete the default Button5, and Type IntDivision
Name: ModButtText: just delete the default Button6, and Type Mod
Your Form should look like this:





Source Code

Private Sub AddButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButt.Click


TxtResult.Text = Val(TxtNumber1.Text) + Val(TxtNumber2.Text)


End Sub

Private Sub MulButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MulButt.Click


TxtResult.Text = Val(TxtNumber1.Text) * Val(TxtNumber2.Text)


End Sub

Private Sub DivButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DivButt.Click


TxtResult.Text = Val(TxtNumber1.Text) / Val(TxtNumber2.Text)

End Sub

Private Sub ModButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModButt.Click
TxtResult.Text = Val(TxtNumber1.Text) Mod Val(TxtNumber2.Text)
End Sub
1


Private Sub SubButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubButt.Click

TxtResult.Text = Val(TxtNumber1.Text) - Val(TxtNumber2.Text)

End Sub

Private Sub ExpoButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExpoButt.Click


TxtResult.Text = Val(TxtNumber1.Text) ^ Val(TxtNumber2.Text)

End Sub

Private Sub IntDivButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IntDivButt.Click


TxtResult.Text = Val(TxtNumber1.Text) \ Val(TxtNumber2.Text)

End Sub

End Class

Output






Monday, October 4, 2010

Data Types in VB .NET

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



Data Types

The Data types available in VB .NET, their size, type, description are summarized in the table below.

Data Type are



Numeric Data

Integer and Long are the most commonly used data types. The conventional Integer found in C, C++ and java is a 16-bit data type but the Integer in VB.Net is now 32 bit, while Long is now 64 bit.

Decimal Data


VB.Net has a 128-bit Decimal data type, which provides support for very large values that can be scaled by powers of 10.
It is divided into 2 parts
96-bit integer for storing the Integer part
32-bit integer for storing the decimal part
Decimal can have values anywhere between 0 to 28 digits to the right of the decimal point. More the digits to the right, higher is the precision we get.

Character Data


Char contains Unicode value ranging from 0-65535 and consumes 2 bytes of space. It directly supports international character sets. The Char data type is intended for use in manipulating single character values or for creating arrays of character values.

Strings


String data type comes form the System.String Class. Any attempt to change the value of the String. Results in a new String being created to store the changed value, while the original is destroyed.

Objects

VB.NET has the Object Data type. The Object data type can hold any type of data. It automatically adjusts its internal data type to accommodate the value so that it coule be stored.

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.