Vb.net iii

Download Vb.net iii

Post on 07-Jul-2015




1 download

Embed Size (px)


Vb.net iii


<ul><li> 1. ArguS academy V.B-PART-IIIPage 1Exception HandlingAn exception is a response to an abnormal or unexpected condition occurring in a program in a program, due to which the execution of a program may be aborted. To avoid the abrupt termination of the program, the exception is generally caught and handled appropriately. Code is written such that in case an exception occurred, alternative action is taken instead of aborting the program abruptly.VB.Net used a structured mechanism to handle exceptions- the try catchend try block.Module mtrySub main()Dim a as integer=9Dim b as integerDim c as integerTryc=a/bSystem.Console.WriteLine(c)Catch s as System.ExceptionSystem.Console.WriteLine(Error .divide by zero)End tryEnd subEnd moduleWhen keywordModule mtry1Sub main()Dim a as shortDim b as shortDim c as decimalSystem.Console.WriteLine(Enter values of a and b)a=System.Console.ReadLine()b= System.Console.ReadLine()tryc=a/bSystem.Console.WriteLine(c)Catch s as System.Exception when b=0System.Console.WriteLine(Error..divide by zero)FinallySystem.Console.WriteLine(End of main)End tryEnd subEnd moduleMultiple catch blockThere can be multiple catch blocks in one TryEnd Try block. The order in which these catch statements are placed is very important. If a catch with a generic exception is placed right at the beginning followed by specific exception handlers, the specific exception handlers will never be executed as the generic exception handler will be executed first.Option Strict onModule trymSub main()Dim a as shortDim b as longa=5000b=50000trya=CType(b,Short)System.Console.WriteLine(a)Catch s as System.OverflowExceptionSystem.Console.WriteLine(Error..value exceeds range of short)Catch s as System.ExceptionSystem.Console.WriteLine(Exception Occured)System.Console.WriteLine(s)End tryEnd subEnd module</li></ul><p> 2. ArguS academy V.B-PART-IIIPage 2FinallyThe finally Keyword is optionally used with the Try .Ent try Block when an action when an action has to be carried out regardless of whether an exception occurred or not. If the exception occurs, the control is transferred to the catch block wherein the statements enclosed within are executed. The control is then passed onto the finally statement.Module trym12Sub main()Dim a as integerDim b as integerDim c as integerSystem.Controle.WriteLine(Enter value of a and b)a=System.Console.ReadLine()b=System.Console.ReadLine()tryc=a/bSystem.Console.WriteLine(c)Catch s as System.ExceptionSystem.Console.WriteLine(Errordivide by zero)FinallySystem.Console.WriteLine(End of main)End TryEnd subEnd moduleEnumerationEnumeration are used when we need to refer to values of primitive types such as byte, short, integer and long as constants or literalsModule m1Enum cities as shortRanchiBokaroDhanbadJamshed PurEnd EnumSub main()System.Console.WriteLine(cities.Dhanbad)End subEnd moduleE.g.-iiEnum Grade as ByteA=100B=80C=70D=60E=40End EnumModule m12Sub main()Dim gradevalue as byteSystem.Console.WriteLine(Choose one of the values: 40/60/70/80/100)Gradevalue=System.Console.ReadLine()Select case gradevalueCase grade.ASystem.Console.WriteLine(You got an A)Case grade.BSystem.Console.WriteLine(You got an B)Case grade.CSystem.Console.WriteLine(You got an C)Case grade.DSystem.Console.WriteLine(You got an D)Case grade.E 3. ArguS academy V.B-PART-IIIPage 3System.Console.WriteLine(You got an E)Case ElseSystem.Console.WriteLine(Wrong Choice)End selectEnd subEnd ModuleStructureA structure is a composite user-defined data type using which we can group data of different data types together. A structure is useful when we wish to create a record-like structure for an entity. A structure can hold not only fields but also methods, properties and events.Module m12Public structure bookPublic title as stringPublic author as stringPublic pages as integerPublic price as singleEnd structureSub main()Dim b as new book()System.Console.WriteLine(Enter Book Title)b.title=System.Console.ReadLine()System.Console.WriteLine(Enter Author Name)b.author=System.Console.ReadLine()System.Console.WriteLine(Enter Page)b.page=System.Console.ReadLine()System.Console.WriteLine(Enter Price)b.price=System.Console.ReadLine()System.Console.WriteLine(Book Title &amp;b.title)System.Console.WriteLine(Author Name &amp;b.author)System.Console.WriteLine(Page &amp;b.page)System.Console.WriteLine(Price &amp;b.price)Ens subEnd moduleArray within structuresModule m123Public structure xPublic arr() as integerEnd structureSub main()Dim obj as xRedim obj.arr(1)obj.arr(0)=155obj.arr(1)=200System.Console.WriteLine(1st Values &amp; obj.arr(0))System.Console.WrteLine(2nd Values &amp; obj.arr(1))End subEnd moduleProcedures within StructureIn addition to data members, a structure can also define functions and procedures within its declarationModule m123Structure studPublic name as stringPublic rollno as integerPublic sub accept() 4. ArguS academy V.B-PART-IIIPage 4System.Console.WriteLine(Enter Name)name=System.Console.ReadLine()System.Console.WriteLine(Enter Roll No)rollno=System.Console.ReadLine()end subend structuresub main()dim s as studs.accept()System.Console.WriteLine(The student details are)System.Console.WriteLine(Name &amp; s.name)System.Console.WriteLine(Roll No &amp;s.rollno)End subEnd ModuleStructure within structureA structure can be declared within another structure. This can be useful when a structure contains a composite data type as it member.Module m321Structure dateofbirthPublic day as integerPublic month as integerPublic year as integerEnd structureStructure studPublic name as StringPublic rollno as integerPublic dob as dateofbirthPublic sub accept()System.Console.WriteLine(enter name)name=System.Console.readLine()System.Console.WriteLine(enter roll no)rollno=System.Console.readLine()end subpublic void acceptdob()System.Console.WriteLine(enter day)dob.day=System.Console.readLine()System.Console.WriteLine(enter month)dob.month=System.Console.readLine()System.Console.WriteLine(enter year)dob.year=System.Console.readLine()end subend structuresub main()dim s as studs.accept()s.acceptdob()System.Console.WriteLine(The Student details are)System.Console.WriteLine(Student Name &amp; s.name)System.Console.WriteLine(Roll No &amp;s.rollno)System.Console.WriteLine(Date &amp; s.dob.day &amp; /)System.Console.WriteLine(Month &amp; s.dob.month &amp; /)System.Console.WriteLine(Year &amp; s.dob.year &amp; /)End subEnd moduleObject oriented programmingObject oriented programming (oop) is based on a real world interpretation of programming elements. It is a type of programming in which programmers define not only the data elements but also the operations that can be performed on the data elements. An object is an entity consisting of data as well as actions. 5. ArguS academy V.B-PART-IIIPage 5Object oriented programming are analogous to real words object and define not only the data elements but also define operation that may be applied to the elements. An object can have attributes and operations.Public class vehiclePublic companyname as stringPublic price as integerPublic mileage as integerEnd classPublic sub mian()Dim car as new vehicleDim truck as new vehicleSystem.Console.WriteLine(Enter car company name )car.companyname=System.Console.readLine()System.Console.WriteLine(Enter car price )car.price=System.Console.readLine()System.Console.WriteLine(Enter car mileage)car.mileage=System.Console.readLine()System.Console.WriteLine(Enter truck company name )truck.companyname=System.Console.readLine()System.Console.WriteLine(Enter truck price )truck.price=System.Console.readLine()System.Console.WriteLine(Enter truck mileage)truck.mileage=System.Console.readLine()end subend moduleMethodsMethods govern the behaviour of the object. Methods in a class typically consist of action statements similar to a function or a procedure and are written using a sub main(). End sub blockModule m145Public class data1Public a as integerPublic b as integerPublic c as integerPublic sub getdata()System.Console.WriteLine(Enter two no)a=System.Console.ReadLine()b=System.Console.ReadLine()c=a+bend subend classpublic sub main()dim ob as new data1ob.getdata()System.Console.WriteLine(Value of A = &amp; ob.a)System.Console.WriteLine(value of B= &amp; ob.b)System.Console.WriteLine(sum of A &amp; B = &amp; ob.c)End subEnd modulee. g. iiModule m111Public name as StringPublic gender as charPrivate age as integerPublic address as StringPublic property agevalue() as integerSet(ByVal value as integer)age=valueend set 6. ArguS academy V.B-PART-IIIPage 6getreturn ageend getend propertypublic sub input()System.Console.WriteLine(Enter the name, gender and address)name=System.Console.ReadLine()gender= System.Console.ReadLine()address= System.Console.ReadLine()end subend classpublic sub main()dim student as new personstudent.age=19student.input()System.Console.writeLine()System.Console.writeLine(Student details are )System.Console.writeLine()System.Console.writeLine(Student Name &amp; student.name)System.Console.writeLine(Student Gender &amp; student.gender)System.Console.writeLine(Student Age &amp; student.age)System.Console.writeLine(Student Address &amp; student.address)End subEnd moduleConstructor and DestructorsA constructor is a method having the same name as the class within which it is defined. This method executes automatically every time an object is created. Constructor are typically declared public and are used for initialization purposes. Destructors are methods that execute whenever the object goes out of scope or is destroyed.ConstructorsIn VB. Net, the lifetime of an object begins when it is created with the New keywords. Constructors in VB. Net are written using the sub New() end Sub blockModule mconPublic class pointPublic x as integerPublic y as integerPublic sub New()System.Console.WriteLine(This is a Constructor)System.Console.WriteLine(Enter values for point)x= System.Console.ReadLine()y= System.Console.ReadLine()end subend classpublic sub main()dim p as new point()System.Console.WriteLine(Point p is)System.Console.WriteLine(Value of x= &amp; p.x)System.Console.WriteLine(Value of y= &amp; p.y)End subEnd moduleDestructorsModule mdesPublic class pointPublic x as integerPublic y as integerPublic sub new()System.Console.WriteLine(This is a constructor)System.Console.WriteLine(Enter values for point)x= System.Console.ReadLine() 7. ArguS academy V.B-PART-IIIPage 7y= System.Console.ReadLine()end suboverrides protected sub finalize()System.Console.WriteLine(This is a destructor executing since object is now going to be destroyed)End subEnd classPublic sub main()Dim p as new point()System.Console.WriteLine(Point p is )System.Console.WriteLine(p.x)System.Console.WriteLine(p.x)End subEnd moduleOverloading, Inheritance and overridingOverloading is the process of declaring method of a class having the same name but different signatures. The signature of a method comprises of the name of the method, the number and type of arguments. The return type of a method is not a part of the signature and hence cannot be used as a factor for overloading.The overloads keyword indicates that the method or property is being overloaded. It is not mandatory to use the overloads keyword. However, once it is used with a method all subsequent overloaded methods must use it.Procedure and function OverloadingModule m121Class xSub max(a as integer, b as integer)Dim c as integerc=a+bSystem.Console.WriteLine(Sum of a and b is &amp; c)End subSub max(s1 as String, s2 as String)Dim s3 as Strings3=s1+s2System.Console.WriteLine(the combined string is &amp; s3)End subEnd classSub main()Dim y as new x()y.max(10,15)y.max(Argus, Academy)end subend moduleConstructor overloading is a special case of method overloading. Parameterized constructors that satisfy the rule for overloading can be used within classes.Module m1234Class basePublic sub new()System.Console.WriteLine(Parameterless constructor )End subPublic sub new(x as integer)System.Console.WriteLine(integer parameter constructor )End subPublic sub new( s as String)System.Console.WriteLine(String parameter constructor )End subEnd classSub main()Dim b1 as new base()Dim b2 as new base()Dim b3 as new base(Hello)End sub 8. ArguS academy V.B-PART-IIIPage 8End moduleProperty OverloadingJust as procedures and function can be overloaded, properties within a class too can be overloaded.Module m1Public class xPrivate addr as stringPrivate addr2 as stingOverloads public property address() as stringSet(Byval add as String)addr=addend setgetreturn addrend getend propertyoverloads public property Address (s as string) as stringset (Byval add as string)addr2=addend setgetreturn addr2end getend propertyend classpublic sub main()Dim y as new x()Dim s as StringSystem.Console.WriteLine(Enter Company Address)y.address()=System.Console.ReadLine()System.Console.WriteLine(Enter residence address)y.address(residence)=System.Console.ReadLine()System.Console.Writeline(the residence Address is :)System.Console.Writeline(y.address(residence))System.Console.Writeline(the Company Address is :)System.Console.Writeline(y.address())End subEnd moduleInheritanceInheritance is the process of creating classes based on existing classes. Inheritance is based on the principle of code reuse. Instead of creating a class from scratch, we can add new methods, properties, and events to existing classes, thus saving time and effort. The class based on which the new class is created is called base class or parent class and the class that is created is called derived or child class.Access Modifiers and inheritanceWith respect to inheritance, access modifiers play a very importance role. In VB. Net, there are five access modifiers that are relevant to inheritance : Public, private, protected, Friends and protected friend- All public elements of a class are inherited by its derived classes.- Private elements of a class cannot be inherited by derived class.- Protected access for elements denotes that elements may be used within the derived class but not outside the derive class.- Friend access modifier is used to denote that the element is accessible throughout the program in which the class is defined.- Protected Friend access modifier indicates that the elements bears features of both protected and friend modifiers.Module m123Public class xPublic j as integer 9. ArguS academy V.B-PART-IIIPage 9Private k as integerProtected l as integerFriend m as integerPublic sub test()l=100end subend classpublic class yInherits xPublic n as integerEnd classPublic sub main()Dim a as new x()Dim b as new y()a.j=100a.m=100a.test()b.j=120b.m=130b.test()b.n=150System.Console.WriteLine(Value of j in base class &amp; a.j)System.Console.WriteLine(Value of m in base class &amp; a.m)System.Console.WriteLine(Value of j in derive class &amp; b.j)System.Console.WriteLine(Value of m in derive class &amp; b.m)System.Console.WriteLine(Value of n in derive class &amp; b.n)End subEnd moduleInheriting Constructor and destructorConstructor and destructors are by default, inherited by derived classes.Module m1Class basePublic sub new()System.Console.WriteLine(Constructo...</p>