Classes

Classes let you reuse application logic on multiple pages or even multiple applications.
If you need to write the same method more than once it should be in a class.
Access Modifiers
Used when declaring a class, method or property
Access modifiers determine who can have access to what in your classes
- Public
- No restrictions, everyone has access
- Private
- Can only be accessed from within THIS class
- Protected
- Can be accessed from only THIS class or a derived class
- Friend
- Can be accessed only from a class within the same assembly (.DLL). In ASP.Net this would be everything in the same subdirectory.
- Protected Friend
- Can be accessed from the class, a derived class or another other class in the same assembly
Let’s build a basic example:


Visual Studio recommend putting all your classes in a special folder called App_Code

- Click on Yes when it asks you to put your class in that folder
It creates the folder and places your file in it

It places you in the code:

Add the following code:
Public Function Display() As String
Return "Hello World"
End Function
- Save it
- Now we need to make a web page to display this string
- Add a new webform
- Add a label, name it lblDisplay
- Double-click it
- Add the following code in the Page_load event:
(If webform is not available to add you may have to click the project name in the solutions explorer first)
Dim objMyDisplay As New Class1
lblDisplay.Text = objMyDisplay.Display()
- Class1 has to be the same as your class filename
- Display() is the name of the function in the class
App_Code
You can add subdirectories in the app_code folder
If you have an error in one of these files and you want to come back to it later you must temporarily hide it or the rest of your site will not work properly.
Right-click on it and select Exclude from project

You can write classes in Visual Basic or C# in the same website.
Each class file has to be in the same language
You must place classes written in a different language in their own subdirectory
You cannot mix different languages in the same folder because they get compiled together.
You also need to modify the web.config file to specify you are using subdirectories.
<configuration>
<system.web>
<compilation>
<codeSubDirectories>
<add directoryName=”VBDirectory” />
<add directoryName=”CSDirectory” />
</codeSubDirectories>
</compilation>
</system.web>
</configuration>
This way two assemblies (DLL files) are created. One for C# and the other for Visual Basic. You can call from both of these classes in the same webpage.
Methods
Methods can be subroutines or functions
- Functions return a value
- Subroutines do not return a value

Shared/Static Methods
A shared method is called directly and does not have to be instantiated.
Visual Basic and C# both have these methods but call them by a different name
- Shared - Visual Basic
- Static - C#
Many of the classes in the .Net Framework have shared methods. That is why they can be called without instantiating them first.
We will use the same example we did earlier but this time make it a shared method
Change the code in your class file:
Public Shared Function Display() As String
Return "Hello World"
End Function
Change the code in your webform by erasing the following line:
Dim objMyDisplay As New Class1
You only need this line:
lblDisplay.Text = objMyDisplay.Display()
Fields
You can declare a field with the Public access modifier so it can be accessed from outside the class.
- Add a new class
- Name it FieldExample.vb
- Add the following code:
Public StrVar as String
Public Function Display() As String
Return "Hello World"
End Function
- Add a webform
- Add a label, name it lblDisplay
- Double-click the form and add this code to the page_load event
Dim objFieldEx as new FieldExample
objFieldEx.strvar=”Hello World”
lblDisplay.text=objFieldEx.Display()
Properties
Properties give you a way to set or retrieve a variable from a class and apply validation to it.
With properties you make the variable private so it can only be access from within the class
Then the property is made public so it can be accessed. The property does the retrieving (GET) and modifying (SET). This way error checking can be added.
You can leave out the SET and it becomes a readOnly property
It is customary to begin a provate member of a class with an underscore (_)
- Add a new class
- Call it propertyEx.vb
- Add
Imports System
- To line 1
- Add the following code in the class
Private _strVar as string
Public Property Display() as String
Get
Return _strVar
End Get
Set (ByVal Value as String)
If Value.Length > 10 then
Throw new exception(“Example error”)
End if
_strvar=Value
End Set
Public Function Display() As String
Return _strVar
End Function
- Add a webform
- Add a label named lblDisplay
- Add this code to the page_load
Dim objPropertyVar as new PropertyEx
objPropertyVar.strVar=”Hello”
lblDisplay.text= objPropertyVar.Display()
Overloading Methods
Overloading is when you have two methods with the same name in a class. The methods have different signatures. Signature are the order and type of parameters the method accepts.
Example
Public class OverloadExample
Public Sub Example(ByVal name as String)
‘ do something
End Sub
Public Sub Example(ByVal Name as String, ByVal age as integer)
‘do something
End Sub
End Class
You can call the Example subroutine by passing a String or a String and an Integer
Partial Classes
You can define a class that spans two or more files
Include Partial in the class declaration
Code-behind pages in ASP.net use partial classes
Example:
Partial Public Class Parts
Public StrVar as String
End Class
Partial Public Class Parts
Public Function Display() As String
Return "Hello World"
End Function
End Class
Inheritance
One class can inherit from another class
When it does it includes all the non-private methods and properties of the parent class.
.Net uses inheritance extensively
All classes derive from the System.Object class
ASP.Net classes derive from System.Web.UI.Page class
Example:
Public Class FirstClass
Private _price As Decimal
Public Property Price() As Decimal
Get
Return _price
End Get
Set(ByVal value As Decimal)
_price = value
End Set
End Property
End Class
Public Class secondClass
Inherits FirstClass
Private _tax As Decimal
Public Property Tax() As Decimal
Get
Return Tax
End Get
Set(ByVal value As Decimal)
_tax = value * Price()
End Set
End Property
End Class
The second class inheritas from the firstclass and is able to retrieve the price from the first class
Overriding Classes
You can overrise a method or proerty when inheriting a class.
The class that is inherited from is called the base class.
Example:
Public Class FirstClass
Private _price As Decimal
Public Overridable Property Price() As Decimal
Get
Return _price
End Get
Set(ByVal value As Decimal)
_price = value
End Set
End Property
End Class
Public Class secondClass
Inherits FirstClass
Public Overrides Property Price() As Decimal
Get
Return MyBase.Price * 0.78
End Get
Set(ByVal value As Decimal)
MyBase.Price = value
End Set
End Property
End Class
MyBase.Price refers to the base class’s Property
Abstract Classes
You can use MustInherit to create an abstract class that must be inherited. An abstract class cannot be instantiates by itself.
Example:
Public MustInherit Class FirstClass
Private _price As Decimal
Public MustOverride ReadOnly Property Price() As Decimal
End Class
Public Class secondClass
Inherits FirstClass
Public Overrides ReadOnly Property Price() As Decimal
Get
Return 20.00
End Get
End Property
End Class
This example create an abstract class called firstClass that must be overridden
The second class overrides it