KEMBAR78
Programming Primer Inheritance VB | PPTX
A Programme Under the compumitra Series
Programming Primer - INHERITANCE
LAB WORK GUIDE
1
OUTLINE
Inheritance Using VB in asp.net
A Parent-Child Class Example.
Example Explanation.
Home Exercise.
Summary.
2
Inheritance
Using VB in ASP.NET
3
InheritanceVB -Web Site Creation-1
From Start Page Click New Website and reach this screen
2. Select 'ASP.NET Empty Web Site'
3. Select Location=File System
4. Click 'Browse..' tab to
select the location where
you want to save your Web
Site
5. click 'OK'
1. Select Language=Visual Basic
 By default Your Web Site shall be saved in the Location- "C:Documents and
SettingsMy DocumentsVisual Studio 2008WebSites." Change it to
 "C:Learner<student-id>ProgrammingPrimerInheritanceVB" folder4
InheritanceVB -Web Site Creation-2
In the Solution Explorer Window
Select the path -> Right click ->
Add New Item…
5
InheritanceVB -Web Site Creation-3 'Add New Item' dialog box will open
1. Select 'Web Form'
2. Simply Click on
'Add' button
6
InheritanceVB – Creating a Button to create an event handler
2. Set the 'Text' Property
equal to 'Inheritance'
1. Select and Drag and Drop
'Button' in div
7
InheritanceVB– Creating Output Display Placeholders Using Label
4. Set the 'Text' Property
equal to 'Blank'
1. Press 'Enter' key to bring the
cursor one line below.
2. Select and Drag and Drop
Two 'Labels' in div
Like 'Label1', Set the 'Text Property' of 'Label2'.
3. Select the 'label1'
8
InheritanceVB –Copy Code-1
Dim c As Child
c = New Child
Dim n As String
n = c.C1()
Label1.Text = n
Dim m As String
m = c.P1()
Label2.Text = m
Copy this Code
9
InheritanceVB -Paste Code-1
Go to 'Default.aspx.vb' by double clicking on
'Button' ('Inheritance' Button) of 'Default.aspx' and
'Paste' the Code in 'Button1_Click' method
Dim c As Child
c = New Child
Dim n As String
n = c.C1()
Label1.Text = n
Dim m As String
m = c.P1()
Label2.Text = m
10
InheritanceVB– Copy Code-2
Public Class Parent
Dim s As String
Public Function P1() As String
s = "Parent P1"
Return s
End Function
End Class
Public Class Child : Inherits Parent
Dim r As String
Public Function C1() As String
r = "Child C1"
Return r
End Function
End Class Copy this Code
11
InheritanceVB -Paste Code-2
Run Code By
pressing 'F5'
'Paste' code after the 'End'
of '_Default' classPublic Class Parent
Dim s As String
Public Function P1() As String
s = "Parent P1"
Return s
End Function
End Class
Public Class Child : Inherits Parent
Dim r As String
Public Function C1() As String
r = "Child C1"
Return r
End Function
End Class
Dim c As Child
c = New Child
Dim n As String
n = c.C1()
Label1.Text = n
Dim m As String
m = c.P1()
Label2.Text = m
12
InheritanceVB -Output
Click on
'Inheritance'
button.
Output on browser
Output after clicking 'Inheritance' button.
Output from 'P1 Function' of
'Parent' class, although called
using a child class object.
Output from 'C1
Function' of 'Child' class
13
Partial Class _Default Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim c As Child
c = New Child
Dim n As String
n = c.C1()
Label1.Text = n
Dim m As String
m = c.P1()
Label2.Text = m
End Sub
End Class
Public Class Parent
Dim s As String
Public Function P1() As String
s = "Parent P1"
Return s
End Function
End Class
Public Class Child : Inherits Parent
Dim r As String
Public Function C1() As String
r = "Child C1"
Return r
End Function
End Class
InheritanceVB- Example Explanation -1
This is 'Child' class, which 'Inherits'
'Parent' class and has 'C1' function
This is 'Parent' class, which has
'P1' function.
These statements creates the
object 'c' of 'Child' class.
This statement uses the object 'c' of 'Child'
class but using a method in 'parent' class.
This is possible due to INHERITANCE.
This statement uses the object 'c' of 'Child'
class using a method in 'Child' class itself.
This is normal usage.
14
Partial Class _Default Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim c As Child
c = New Child
Dim n As String
n = c.C1()
Label1.Text = n
Dim m As String
m = c.P1()
Label2.Text = m
End Sub
End Class
Public Class Parent
Dim s As String
Public Function P1() As String
s = "Parent P1"
Return s
End Function
End Class
Public Class Child : Inherits Parent
Dim r As String
Public Function C1() As String
r = "Child C1"
Return r
End Function
End Class
InheritanceVB- Example Explanation - 2
This is 'C1' function of 'Child' class.
This statement 'returns' the value
of 'r'.
This statement calls the function 'C1' of
'Child' class and stores returned value in 'n'
which is output in next line as "Child C1"
This statement calls the function 'P1' of 'Child'
class and stores returned value in 'm' which is
output in next line as "Parent P1"
This is 'P1' function of 'Parent' class.
15
InheritanceVB- Example Explanation - 3
'n=c.C1( )' statement calls the function 'C1' of
'Child' class.
'm=c.P1( )' statement calls the function 'P1' of
'Parent' class. This is actually done using a child
class object.
16
InheritanceVB: Home Exercise
 Write a program similar to the example given which can
demonstrate two classes "polygon" and a child class "square".
From the main routine, Use class "polygon" to return the
string "sides" and use class "square" to return the string "4".
Finally display the string, "I am a polygon called square, I
have 4 sides".
Remember that the program should be based on
INHERITANCE.
 You can further extend this program where a user inputs the
name of a polygon and based on user's provided name the
program returns number of sides. For example input triangle
to return "3 sides" and square to return "4 sides".
17
InheritanceVB : Learning Summary Review
Concept of Inheritance
Child class inherits properties of parent class.
Objects created from child class can always use
methods and properties from parent class. (The
reverse is not true).
Programming techniques to create classes,
and subclasses.
Programming techniques to return values.
Programming techniques to concatenate
strings.
18
Ask and guide me at
sunmitraeducation@gmail.com
Share this information with as
many people as possible.
Keep visiting www.sunmitra.com
for programme updates.
19

Programming Primer Inheritance VB

  • 1.
    A Programme Underthe compumitra Series Programming Primer - INHERITANCE LAB WORK GUIDE 1
  • 2.
    OUTLINE Inheritance Using VBin asp.net A Parent-Child Class Example. Example Explanation. Home Exercise. Summary. 2
  • 3.
  • 4.
    InheritanceVB -Web SiteCreation-1 From Start Page Click New Website and reach this screen 2. Select 'ASP.NET Empty Web Site' 3. Select Location=File System 4. Click 'Browse..' tab to select the location where you want to save your Web Site 5. click 'OK' 1. Select Language=Visual Basic  By default Your Web Site shall be saved in the Location- "C:Documents and SettingsMy DocumentsVisual Studio 2008WebSites." Change it to  "C:Learner<student-id>ProgrammingPrimerInheritanceVB" folder4
  • 5.
    InheritanceVB -Web SiteCreation-2 In the Solution Explorer Window Select the path -> Right click -> Add New Item… 5
  • 6.
    InheritanceVB -Web SiteCreation-3 'Add New Item' dialog box will open 1. Select 'Web Form' 2. Simply Click on 'Add' button 6
  • 7.
    InheritanceVB – Creatinga Button to create an event handler 2. Set the 'Text' Property equal to 'Inheritance' 1. Select and Drag and Drop 'Button' in div 7
  • 8.
    InheritanceVB– Creating OutputDisplay Placeholders Using Label 4. Set the 'Text' Property equal to 'Blank' 1. Press 'Enter' key to bring the cursor one line below. 2. Select and Drag and Drop Two 'Labels' in div Like 'Label1', Set the 'Text Property' of 'Label2'. 3. Select the 'label1' 8
  • 9.
    InheritanceVB –Copy Code-1 Dimc As Child c = New Child Dim n As String n = c.C1() Label1.Text = n Dim m As String m = c.P1() Label2.Text = m Copy this Code 9
  • 10.
    InheritanceVB -Paste Code-1 Goto 'Default.aspx.vb' by double clicking on 'Button' ('Inheritance' Button) of 'Default.aspx' and 'Paste' the Code in 'Button1_Click' method Dim c As Child c = New Child Dim n As String n = c.C1() Label1.Text = n Dim m As String m = c.P1() Label2.Text = m 10
  • 11.
    InheritanceVB– Copy Code-2 PublicClass Parent Dim s As String Public Function P1() As String s = "Parent P1" Return s End Function End Class Public Class Child : Inherits Parent Dim r As String Public Function C1() As String r = "Child C1" Return r End Function End Class Copy this Code 11
  • 12.
    InheritanceVB -Paste Code-2 RunCode By pressing 'F5' 'Paste' code after the 'End' of '_Default' classPublic Class Parent Dim s As String Public Function P1() As String s = "Parent P1" Return s End Function End Class Public Class Child : Inherits Parent Dim r As String Public Function C1() As String r = "Child C1" Return r End Function End Class Dim c As Child c = New Child Dim n As String n = c.C1() Label1.Text = n Dim m As String m = c.P1() Label2.Text = m 12
  • 13.
    InheritanceVB -Output Click on 'Inheritance' button. Outputon browser Output after clicking 'Inheritance' button. Output from 'P1 Function' of 'Parent' class, although called using a child class object. Output from 'C1 Function' of 'Child' class 13
  • 14.
    Partial Class _DefaultInherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim c As Child c = New Child Dim n As String n = c.C1() Label1.Text = n Dim m As String m = c.P1() Label2.Text = m End Sub End Class Public Class Parent Dim s As String Public Function P1() As String s = "Parent P1" Return s End Function End Class Public Class Child : Inherits Parent Dim r As String Public Function C1() As String r = "Child C1" Return r End Function End Class InheritanceVB- Example Explanation -1 This is 'Child' class, which 'Inherits' 'Parent' class and has 'C1' function This is 'Parent' class, which has 'P1' function. These statements creates the object 'c' of 'Child' class. This statement uses the object 'c' of 'Child' class but using a method in 'parent' class. This is possible due to INHERITANCE. This statement uses the object 'c' of 'Child' class using a method in 'Child' class itself. This is normal usage. 14
  • 15.
    Partial Class _DefaultInherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim c As Child c = New Child Dim n As String n = c.C1() Label1.Text = n Dim m As String m = c.P1() Label2.Text = m End Sub End Class Public Class Parent Dim s As String Public Function P1() As String s = "Parent P1" Return s End Function End Class Public Class Child : Inherits Parent Dim r As String Public Function C1() As String r = "Child C1" Return r End Function End Class InheritanceVB- Example Explanation - 2 This is 'C1' function of 'Child' class. This statement 'returns' the value of 'r'. This statement calls the function 'C1' of 'Child' class and stores returned value in 'n' which is output in next line as "Child C1" This statement calls the function 'P1' of 'Child' class and stores returned value in 'm' which is output in next line as "Parent P1" This is 'P1' function of 'Parent' class. 15
  • 16.
    InheritanceVB- Example Explanation- 3 'n=c.C1( )' statement calls the function 'C1' of 'Child' class. 'm=c.P1( )' statement calls the function 'P1' of 'Parent' class. This is actually done using a child class object. 16
  • 17.
    InheritanceVB: Home Exercise Write a program similar to the example given which can demonstrate two classes "polygon" and a child class "square". From the main routine, Use class "polygon" to return the string "sides" and use class "square" to return the string "4". Finally display the string, "I am a polygon called square, I have 4 sides". Remember that the program should be based on INHERITANCE.  You can further extend this program where a user inputs the name of a polygon and based on user's provided name the program returns number of sides. For example input triangle to return "3 sides" and square to return "4 sides". 17
  • 18.
    InheritanceVB : LearningSummary Review Concept of Inheritance Child class inherits properties of parent class. Objects created from child class can always use methods and properties from parent class. (The reverse is not true). Programming techniques to create classes, and subclasses. Programming techniques to return values. Programming techniques to concatenate strings. 18
  • 19.
    Ask and guideme at sunmitraeducation@gmail.com Share this information with as many people as possible. Keep visiting www.sunmitra.com for programme updates. 19