VB.NET

Share Embed Donate


Short Description

Download VB.NET...

Description

VB.NET Data Types : DATATYPE in a programming language describes that what type of data a variable can hold . When we declare a variable, we have to tell the compiler about what type of the data the variable can hold or which data type the variable belongs to. Syntax : Dim VariableName as DataType VariableName : the variable we declare for hold the values. DataType : The type of data that the variable can hold VB.NET sample  Dim count As Integer  count : is the variable name  Integer : is the data type The above example shows , declare a variable 'count' for holding integer values. In the variable count we can hold the integer values in the range of -2,147,483,648 to +2,147,483,647. The follwing are the some of commonly using datatypes in vb.net.

1) Boolean Boolean variables are stored 16 bit numbers and it can hold only True or false. VB.NET Runtime type : S ystem.Boolean VB.NET declaration : dim check as Boolean VB.NET Initialization : check = false VB.NET default initialization value : false

2) Integer Integer variables are stored sighned 32 bit integer values in the range of -2,147,483,648 to +2,147,483,647 VB.NET Runtime type : System.Int32 VB.NET declaration : dim count as Integer  VB.NET Initialization : count = 100 VB.NET default initialization value : 0

3) String String variables are stored any number of alphabetic, numerical, and special characters . Its range from 0 to approximately 2 billion Unicode characters. VB.NET Runtime type : System.String VB.NET declaration : Dim str As String VB.NET Initialization : str = "String Test" VB.NET default initialization value : Nothing In VisualBasic.Net we can convert a datatype in two ways. Implicit Conversion and Explicit conversion . Also we can convert a type value to a reference and reference value to a type value. These are called Boxing and unBoxing . Boxing referes value type to reference type and unboxing , reference type ot value type.

Example: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim check As Boolean check = True

MsgBox("The value assigned for check is : " & check) Dim count As Integer count = 100 MsgBox("The value holding count is : " & count) Dim str As String str = "String test " MsgBox("The value holding str is : " & str) End Sub End Class

VB.NET Implicit and Explicit Conversions : Implicit Type Conversions Implicit Conversion perform automatically in VB.NET, that is the compiler is taking care of the conversion. The following example you can see how it happen. 1. Dim iDbl As Double  2. Dim iInt As Integer   3. iDbl = 9.123  4. MsgBox("The value of iDbl is " iDbl) 5. iInt = iDbl 6. MsgBox("The value of iInt is " iInt) line no 1 : Declare a Double datatype variable iDble line no 2 : Declare an Integer datatype variable iInt line no 3 : Assign a decimal value to iDbl line no 4 : Display the value of iDbl line no 5 : Assign the value of iDbl to iInt line no 6 : Display the value of iInt The first messagebox display the value of iDbl is 9.123 The second messegebox display the value od iInt is 9 iInt display only 9 because the value is narrowed to 9 to fit in an Integer variable. Here the Compiler made the conversion for us. These type fo conversions are called Implicit Conversion . The Implicit Conversion perform only when the Option Strict switch is OFF

Example: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim iDbl As Double Dim iInt As Integer iDbl = 9.123 MsgBox("The value of iDbl is " & iDbl) iInt = iDbl 'after conversion MsgBox("The value of iInt is " & iInt) End Sub End Class

How to VB.NET Access Specifiers : AccessSpecifiers describes as the scope of accessibility of an Object and its members. We can control the scope of the member object of a class using access specifiers. We are using access specifiers for providing security of our applications. Visual Basic .Net provide five access specifiers , they are as follows : Public, Private , Protected , Friend and ProtectedFriend .

Public : Public is the most common access specifier. It can be access from anywhere, that means there is no restriction on accessability. The scope of the accessibility is inside class also in outside the class.

Private : The scope of the accessibility is limited only inside the classes in which they are decleared. The Private members can not be accessed outside the class and it is the least permissive access level.

Protected : The scope of accessibility is limited within the class and the classses derived (Inherited )from this class.

Friend : The Friend access specifier can access within the program that contain its declarations and also access within the same assembly level. You can use friend instead of Dim keyword.

ProtectedFriend : ProtectedFriend is same access lebels of both Protected and Friend. It can access anywhere in the same assebly and in the same class also the classes inherited from the same class .

VB.NET Option Explicit [On | Off] : Option Explicit statement ensures whether the compiler requires all variables to be explicitly declared or not before it use in the program. Option Explicit [On Off] The Option Explicit has two modes. On and Off mode. If Option Explicit mode in ON , you have to declare all the variable before you use it in the program . If not , it will generate a compile-time error whenever a variable that has not been declared is encountered .If the Option Explicit mode is OFF , Vb.Net automatically create a variable whenever it sees a variable without proper declaration. By default the Option Explicit is On With the Option Explicit On , you can reduce the possible errors that result from misspelled variable names. Because in Option Explicit On mode you have to declare each variable in the program for storing data. Take a look at the following programs, it will give you a clear picture of Option Explicit. The following program is a normal vb.net program , so the default mode of Option Explicit On is using. The default is Option Explicit On , so we do not need to put it in the source code.

VB.NET Source Code

Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim someVariable As String someVariable = "Option Explicit ON" MsgBox(someVariable) End Sub End Class The above program , declare a String variable and assigned a value in it and it displays. Take a look at the following program , it is an example of Option Explicit Of 

Example: Option Explicit Off  Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click someVariable = "Option Explicit ON" MsgBox(someVariable) End Sub End Class Here "someVariable" is not declared , because the Option Explicit Of , without any compiler error you can continue the program.

VB.NET Option Strict [On | Off] : Option Strict is prevents program from automatic variable conversions, that is implicit data type conversions . Option Strict [On Off] By default Option Strict is Off  From the following example you can understand the use of Option Strict.

VB.NET Source Code

Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim longNum As Long Dim intNum As Integer longNum = 12345 intNum = longNum MsgBox(intNum) End Sub End Class The above program is a normal vb.net program and is in default Option Strict Off  . Because its Option Strict Off we can convert the value of Long to an Integer. Take a look at the following program.

Example: Option Strict On Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim longNum As Long Dim intNum As Integer longNum = 12345 intNum = longNum MsgBox(intNum) End Sub End Class

When you write this source code the compiler will shows the message "Option Strict On disallows implicit conversions from 'Long' to 'Integer'" The compiler generate error because in the program we put "Option Strict On" and prevent the program from automatic conversion.

  VB.NET On Error GoTo : On Error GoTo statements is an example of Vb.Net's Unstructured Exception Handling . VB.NET has two types of Exception handling . Structured Error Handling and Unstructured Error handling . VB.NET using Try..Catch statement for Structured Error handling and On Error GoTo statement is using for Unstructured Error handling. Error GoTo redirect the flow of the program in a given location.

On Error Resume Next - whenever an error occurred in runtime , skip the statement and continue execution on following statements. Take a look at the following program

VB.NET Source Code

Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim result As Integer Dim num As Integer num = 100 result = num / 0 MsgBox("here") End Sub End Class when u execute this program you will get error message like "  Arithmetic operation resulted in an overflow " See the program we put an On Error GoTo statement

Example: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click On Error GoTo nextstep Dim result As Integer Dim num As Integer num = 100 result = num / 0 nextstep: MsgBox("Control Here") End Sub End Class When you execute the program you will get the message box "Control Here" . Because the On Error statement redirect the exception to the Label statement.

How to VB.NET Exceptions : Exceptions are the occurrence of some condition that changes the normal flow of execution . For ex: you programme run out of memory , file does not exist in the given path , network connections are dropped etc. More specifically for better understanding , we can say it as Runtime Errors . In .NET languages , Structured Exceptions handling is a fundamental part of Common Language Runtime . It has a number of advantages over the On Error  statements provided in previous versions of Visual Basic . All exceptions in the Common Language Runtime are derived from a single base class , also you can create your own custom Exception classes. You can create an Exception class that inherits from Exception class . You can handle Exceptions using Try..Catch statement .

Try code exit from Try Catch [Exception [As Type]] code - if the exception occurred this code will execute exit from Catch Finally The code in the finally block will execute even if there is no Exceptions. That means if you write a finally block , the code should execute after the execution of try block or catch block.

Try code exit from Try Catch [Exception [As Type]] code - if the exception occurred this code will execute exit Catch Finally code - this code should execute , if exception occurred or not From the following VB.NET code , you can understand how to use try..catch statements. Here we are going to divide a number by zero .

Example: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Try Dim i As Integer Dim resultValue As Integer i = 100 resultValue = i / 0 MsgBox("The result is " & resultValue) Catch ex As Exception MsgBox("Exception catch here ..") Finally MsgBox("Finally block executed ") End Try End Sub End Class

How to use IF ELSE in VB.NET: The conditional statement IF ELSE , is use for examining the conditions that we provided, and making decision based on that contition. The conditional statement examining the data using comparison operators as well as logical operators. If [your condition here]   Your code here Else   Your code Here End If  If the contition is TRUE then the control goes to between IF and Else block , that is the program will execute the code between IF and ELSE statements. If the contition is FLASE then the control goes to between ELSE and END IF block , that is the program will execute the code between ELSE and END IF statements. If you want o check more than one condition at the same time , you can use ElseIf . If [your condition here]   Your code here ElseIf [your condition here]   Your code here ElseIf [your condition here]   Your code here Else   Your code Here End If  Just take a realtime example When we want to analyze a mark lists we have to apply some conditions for grading students depends on the marks. Following are the garding rule of the mark list: 1) If the marks is greater than 80 then the student get higher first class 2) If the marks less than 80 and greater than 60 then the student get first class 3) If the marks less than 60 and greater than 40 then the student get second class 4) The last condition is , if the marks less than 40 then the student fail. Now here implementing these conditions in a VB.NET program. 1. 2. 3. 4. 5. 6. 7. 8. 9.

If totalMarks >= 80 Then MsgBox("Got Higher First Class ") ElseIf totalMarks >= 60 Then MsgBox("Got First Class ") ElseIf totalMarks >= 40 Then MsgBox("Just pass only") Else MsgBox("Failed") End If 

Line 1 : Checking the total marks greaterthan or equal to 80 Line 2 : If total marks greater than 80 show message "Got Higher First Class "

Line 3 : Checking the total marks greaterthan or equal to 60 Line 4 : If total marks greater than 60 show message "Got First Class " Line 5 : Checking the total marks greaterthan or equal to 40 Line 6 : If total marks greater than 40 show message "Just pass only" Line 7 : If those three conditions failed program go to the next coding block Line 8 : If all fail shows message "Failed" Line 9 : Ending the condition block  VB.NET Source Code Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim totalMarks As Integer totalMarks = 59 If totalMarks >= 80 Then MsgBox("Gor Higher First Class ") ElseIf totalMarks >= 60 Then MsgBox("Gor First Class ") ElseIf totalMarks >= 40 Then MsgBox("Just pass only") Else MsgBox("Failed") End If  End Sub End Class In this example the total marks is 59 , when you execute this program you will get in messagebox "Just Pass Only" If you want to check a condition within condition you can use nested if statements If [your condition here] If [your condition here]   Your code here Else   Your code Here End If  Else   Your code Here End If   Also you can write IF ELSE Statements in a single line  If [your condition here] [Code] Else [code] Example: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim totalMarks As Integer

totalMarks = 39 If totalMarks >= 50 Then MsgBox("passed ") Else MsgBox("Failed ") End Sub End Class When you execute this program you will get in messagebox "Failed ".

How to use FOR NEXT loop in vb.net : Whenever you face a situation in programming to repeat a task for several times (more than one times ) or you have to repeat a task till you reach a condtition, in these situations you can use loop statements to achieve your desired results. FOR NEXT Loop, FOR EACH Loop , WHILE Loop and DO WHILE Loop are the Commonly used loops in Visual Basic.NET 2005 ( VB.NET 2005) . FOR NEXT Loop : The FOR NEXT Loop , execute the loop body (the source code within For ..Next code block) to a fixed number of times. For var=[startValue] To [endValue] [Step] [loopBody] Next [var] var : The counter for the loop to repeat the steps. starValue : The starting value assign to counter variable . endValue : When the counter variable reach end value the Loop will stop . loopBody : The source code between loop body Lets take a simple real time example , If you want to show a messagebox 5 times and each time you want to see how many times the message box shows. 1. 2. 3. 4. 5.

startVal=1 endVal = 5 For var = startVal To endVal show message Next var

Line 1: Loop starts value from 1 Line 2: Loop will end when it reach 5 Line 3: Assign the starting value to var and inform to stop when the var reach endVal Line 4: Execute the loop body Line 5: Taking next step , if the counter not reach the endVal  VB.NET Source Code Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim var As Integer Dim startVal As Integer

Dim endVal As Integer startVal = 1 endVal = 5 For var = startVal To endVal MsgBox("Message Box Shows " & var & " Times ") Next var End Sub End Class When you execute this program , It will show messagebox five time and each time it shows the counter value. If you want to Exit from FOR NEXT Loop even before completing the loop Visual Basic.NET provides a keyword Exit to use within the loop body. For var=startValue To endValue [Step] [loopBody] Contition [Exit For] Next [var] Example: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim var As Integer Dim startVal As Integer Dim endVal As Integer startVal = 1 endVal = 5 For var = startVal To endVal MsgBox("Message Box Shows " var " Times ") If var = 3 Then Exit For End If  Next var End Sub End Class When you execute the above source code , the program shows the message box only three times .

How to use FOR EACH loop in VB.NET : Whenever you face a situation in programming to repeat a task for several times (more than one times ) or you have to repeat a task till you reach a condition, in these situations you can use loop statements to achieve your desired results. FOR NEXT Loop, FOR EACH Loop , WHILE Loop and DO WHILE Loop are the Commonly used loops in Visual Basic.NET 2005 ( VB.NET 2005) .

For Each Loop FOR EACH Loop usually using when you are in a situation to execute every single element or item in a group (like every single element in an Array, or every single files in a folder or , every character in a String ) , in these type of situation you can use For Each loop. For Each [Item] In [Group] [loopBody] Next [Item] Item : The Item in the group Group : The group containing items LoopBody : The code you want to execute within For Each Loop Let's take a real time example , if you want to display the each character in the website name "HTTP://NETINFORMATIONS.COM" , it is convenient to use For Each Loop. 1. siteName = "HTTP://NETINFORMATIONS.COM"  2. For Each singleChar In siteName 3. MsgBox(singleChar) 4. Next Line 1: Assigning the site name in a variable Line 2: This line is extracting the single item from the group Line 3: Loop body Line 4: Taking the next step Example: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim siteName As String Dim singleChar As Char siteName = "HTTP://NETINFORMATIONS.COM" For Each singleChar In siteName MsgBox(singleChar) Next End Sub End Class When you execute this program you will get each character of the string in Messagebox.

How to use vb.net While End While loop : Whenever you face a situation in programming to repeat a task for several times (more than one times ) or you have to repeat a task till you reach a condition, in these situations you can use loop statements to achieve your desired results. FOR NEXT Loop, FOR EACH Loop , WHILE Loop and DO WHILE Loop are the Commonly used loops in Visual Basic.NET 2005 ( VB.NET 2005) .

While ..End While While .. End While Loop execute the code body (the source code within While and End while statements ) until it meets the specified condition.

While [condition] [loop body] End While Condition : The condition set by the user 1. counter = 1 2. While (counter
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF