Professional Documents
Culture Documents
What is Inheritance?
Inheritance is the property in which a derived class acquires the attributes of its base class. Recall that classes contain
What is Inheritance?
Class play role of parent (superclass) Class play role of child (subclass)
Inheritance
When user create class, new class can be based on another class. User can create new class inherit from one of existing .NET classes or from one of user own classes. Example:
Partial Public Class Form1 Inherits System.Windows.Forms.Form The Inherits statement must follow the class header prior to any comments. Public class NewClass Inherits BaseClass
Constructor in Inheritance
Subclass cannot inherit constructor from base class. Each class must have its own constructor. With this statemen MyBase.NEW() Example: Sub New(ByVal title As String, ByVal price By Decimal)
assign property values call the base class constructor MyBase.NEW(title, price) End Sub
Important Of Inheritance
Provides re-usability of code. Help in reducing the code size and better project management. Use Inherits keyword to inherit a class into other class. Old class called Base class and new class called Derived class Use mybase keyword to reference the mambers of base class.
Polymorphism
Polymorphism is one of the crucial features of OOP. An object can be treated as if it were a different kind of object provided common sense prevails. It is also called as Overloading which means the use of same thing for different purposes. Using Polymorphism we can create as many functions we want with one function name but with different argument list.
Example:
Sub Main() Dim two As New One() WriteLine(two.add(10)) 'calls the function with one argument WriteLine(two.add(10, 20)) 'calls the function with two arguments WriteLine(two.add(10, 20, 30)) 'calls the function with three arguments Read() End Sub
Implement Polymorphism
Class Parent Public Overridable Sub p1() Console.Write("Parent.p1") End Sub Public Sub p2() MyClass.p1() 'Implementing keyword MyClass here tells all derived classes to refer to the base / abstract class wherein the keyword MyClass appears End Sub End Class
Implement Polymorphism
Implementing the keyword MyClass in the base class tells all derived classes to use the method in the base class itself and, therefore, a stack overflow error is prevented. p.p2() 'stack overflow error is prevented c = New Child() c.p1() 'stack overflow error is prevented c.p2() 'stack overflow error is prevented End Sub
Chapter 4.3
Construct String
The String Class represents character strings. The String data type comes from the System.String class The String type represents a string of Unicode Characters . The String class is a sealed class , so you cannot inherit another class from the String class.
What is Parameters?
VB.NET String.Format()
VB.NET String.Length()
The Length() function in String Class returned the number of characters occurred in a String. System.String.Length() As Integer Returns:
Integer : The number of characters in the specified String "This is a Test".Length() returns 14.
For exmple:
VB.NET String.Concat()
Concat in VB.NET String Class using for concat two specified String Object System.String.Concat(ByVal str1 As String, ByVal str2 As String) As String String Concat method returns a new String Parameters:
String str1 : Parameter String String str2 : Parameter String String : A new String retrun with str1 Concat with str2
Returns:
VB.NET String.substring()
Substring in Vb.Net String Class returns a new string that is a substring of this string. The substring begins at the specified given index and extended up to the given length. Public Function Substring(ByVal startIndex As Integer, ByVal length As Integer) As String Parameters:
startIndex: The index of the start of the substring. length: The number of characters in the substring. The specified substring.
Returns:
Exceptions:
System.ArgumentOutOfRangeException : the beginIndex or length less than zero, or the begin index + length not within the specified string
Chapter 4.4
Exception Handling
What is exception?
An error that occurs at run time. Exception handling is an in built mechanism in .NET framework to detect and handle run time errors. Logic errors. System errors.
Exception Handling?
Cause of error?
VB.NET provides three keywords try, catch and finally to do exception handling.
Example
Public Sub MyException(ByVal a As Int32, ByVal b As Int32) Dim Result As Int32 Try 'The try block from where an exception will thrown Result = a / b textBox3.Text = Result.ToString() Catch e As System.DivideByZeroException 'The catch block where Exception will be handle 'Here I am using e.Message MessageBox.Show(e.Message) End Try End Sub