Академический Документы
Профессиональный Документы
Культура Документы
What is Inheritance?
Inheritance is the property in which a derived class
acquires the attributes of its base class. Recall that classes contain
Fields Properties Event Methods
What is Inheritance?
Inheritance implies at least two classes Class play role of parent (superclass) Class play role of child (subclass) To implement inheritance, use the Inherits keyword.
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
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
Construct 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.
VB.NET String.Length()
The Length() function in String Class returned the
String
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
Returns: String : A new String retrun with str1 Concat with str2
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.
Returns:
The specified substring.
Exceptions:
System.ArgumentOutOfRangeException : the beginIndex or
length less than zero, or the begin index + length not within the specified string
Exception Handling
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