Skip to main content

Data Types in VB.NET

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

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Email/Password

In our  previous  article we saw how to create authentication module using Google identity provider. Now we will see how to implement Email and Password authentication using Firebase. We will use Bootstrap form to create intuitive user interface for sign-up and login using Email and Password. Step 1: Create signUp component. ng generate component signUp Step 2: Create custom form in sign-up.component.html file. The result of above code: Step 3: Add two functions for creating the user and login using Email and Password in src/app/providers/AFAuth.ts file. Step 4: Call createUserWithEmailAndPassword function in service from sign-up.component.ts. Step 5: Update routing configuration in app.module.ts  to include signUp component. Step 6: Update Login form to have Bootstrap form. The result of the above code. Step 7: Enable Email/Password component in Firebas...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...