The data type of variable or constant indicates what type of information will be stored in the allocated memory space: perhaps a name, a dollar amount, a data, or a total.
All information stored or used by a Visual Basic.NET program has a type. A type describes a piece of information to the .NET framework, saying how it is used and stored. By assigning every piece of information a type, the language can enforce type-safe programming, which ensures that information is always interpreted in a correct manner, helping to reduce programming errors.
Numeric Data type
A data type that accepts only numerical value from the user is known as Numeric data type.
Full Number
Data Type | Capacity |
Single | 2 bytes |
Integer | 4 bytes |
Long | 8 bytes |
Precision Number
Data Type | Capacity |
Single | 4 bytes |
Double | 8 bytes |
Decimal | 16 bytes |
String
A special data type that accepts alphabets, symbols or alphanumerical values as input is called string.
Character Data type
Character variable store a single Unicode character in two bytes. In effect, characters are unsigned short integers (UInt16); you can use the CChar() function to convert integers to characters, and the CInt() function to convert characters to their equivalent integer values.
Example:
Dim x as Char = ‘A’
Dim s as Char = ‘Amit”
From the above statements, the output will be “A” in both situations because char accepts only first character. So in second statement, though “Amit” is assigned, char will take first character only.
String Data type
The string data type stores only text, and string variables are declared with the String Type:
Example: Dim y as string = “Amit Chaudhary”
You can store nearly 2 GB of text in a string variable (that is 2 billion characters in much more text that you care to read on a computer screen).
The following assignments are all valid:
Dim aString as String
Dim number as integer
aString = “Be happy and Help others . . .”
aString = “”
aString = “25000”
number = 25000
The difference between last two variables is that they hold different values. The aString variable holds the characters “2”,”5”,”0”,”0”,”0” and a number holds a single numeric value. However you can use the variable aString in numeric calculation and the variable number in string operations. VB will perform the necessary conversions, as long as the Strict option if off (by default).
Date
A data type that represents only date and various time formats is known as date data type.
Date and time values are stored internally in a special format, but you don’t need to know the exact format. They are double-precision numbers: the integer part represents the date and the fractional part represents the time. A variable declared as Date can store both date and time values with a statement like the following
Dim x as date
The following are all valid assignments:
x = #7/13/2010#
x =# 7/13/2010 11:43:12 am#
x = #“13 July, 2010”#
x = now()
The now() function returns the current date and time. The pound sign tells Visual Basic to store a date value to the x variable, just as the quotes tell Visual Basic that the value is a string. You can store a date as string to a Date variable, but it will be converted to the appropriate format.
Tip
The date format is determined by the Regional Settings (found in Control Panel). In the United States, its mm/dd/yy (in other countries, the format is dd/mm/yy). If you assign an invalid date to a date variable, like 23/04/2002, Visual Basic will automatically swap the month and day values to produce a valid date as type. If the date is invalid even after the swapping of the month and day values, then an error message will appear in the Task List window. The description of the error is “Expected expression”.
Example:
The difference between two dates is calculated by the function DateDiff(). This function accepts the argument as a constant that determines the units in which the difference will be expressed (days, hours and so on) as well as two dates, and it returns the difference between them in the specified increments. The following statement returns the number of days in the current millennium:
Dim days as long
Days = DateDiff(DateInterval.Day,#12/31/2000#,now())
Boolean
A special data type of VB.NET that represent only true/false as input from user is called Boolean.
Boolean variables are, in essence, integers that take the value -1(for True) and 0 (for False). Actually, any non-zero value is considered True. Boolean variables are by default false.
Syntax:
Dim <variable name> as Boolean
Example:
Dim i AsInteger
Dim no AsInteger
Dim z AsBoolean
PrivateSub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
no = Val(Me.TextBox1.Text)
For i = 2 To no - 1
If no Mod i = 0 Then
z = True
ExitFor
EndIf
Next
If z = FalseThen
Me.Label1.Text = "It is prime"
Else
Me.Label1.Text = "Not Prime"
EndIf
EndSub
Object
A special datatype of vb.net that accepts all the values and change according to, it is known as object.
Object is the most flexible data type because it can accommodate all other types. A variable declared as Object (or a variable that has not declared at all) is handled by Visual Basic according to the variable’s current contents. If you assign an integer value to an Object variable, Visual Basic treats an integer. If you assign a string to an object variable, Visual Basic treats it as String. Visual Basic performs the necessary conversations for you.
Example:
Dim any
Dim any as object
any = “amit”
any = 1
Edited By: Chaudhary Amit V.

Comments
Post a Comment