VB.NET: Create and Apply Basic Classes

toytoy

Well-known member
Joined
Jul 16, 2004
Messages
46
Programming Experience
1-3
Does anyone know how to create classes and apply it without occur any error correctly...

I find that most of my coding is inside the forms and i am not using any classes at the monent...

thus i find it quite hard if i need to change the coding......i would search all the forms just for the word that i need to change..

Therefore i am eager to change my style of coding and uses classes more instead..

This is quite basic..

Can anyone sent me some useful links, advise or tutorials about that..

Thanks
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
in your project simply click Project -> Add Class then give it a name and the editor will open the module with your class declared already now all you gotta do is start giveing it properties and methods. then on your form declare a variable as that class and set some properties and call methods and that's roughly how a class works...
 

toytoy

Well-known member
Joined
Jul 16, 2004
Messages
46
Programming Experience
1-3
I got some questions about the uses of classes...


1)Is it a must to create both the property and methods in the

classes....if i does not uses property often....

can i just declare it inside the form and put all methods

separately to the classes...

2)Can i say that class is somehow act like the function procedure

as it can return the values except that it could be shared to all

forms if i put it in classes....

3) Lastly, how to declare one or multiple classes that i need to

the various forms... I am not sure where the Inherits

should be place... is it

coding:
VB.NET:
	Public Class Sample
	Inherits System.Windows.Forms.Form 
	Inherits Class1, Class2
Thanks
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
you can only inherit from one source in vb (c++ can have multiple inheritance)

properties are optional, jes as methods are too

you can have multiple methods and stuff too, here's a class i did for an assignment back in school:

VB.NET:
  'Compiler Options
  Option Explicit On 
  Option Strict On
  
  Public Class Business
  	'============================================================
  	'Module : Business
  	'============================================================
  	'Abstract : This is the business tier where the actual rental
  	'fee/charge is calculated.
  	'============================================================
  	Public Enum MyCarType
  		Economy
  		Midsize
  		Luxury
  	End Enum
  
  	'============================================================
  	'Module level variables
  	'============================================================
  	Private mdecDailyRates() As Decimal = {26.95D, 32.95D, 50.95D}
  	Private mdecMileageCharges() As Decimal = {0.12D, 0.15D, 0.2D}
  	Private mstrTypeOfCar As String
  	Private mstrAccountType As String
  	Private mdecNumberOfDays As Decimal
  	Private mintStartOdo As Integer
  	Private mintEndOdo As Integer
  	Private mdecTotalRent As Decimal
  	Private mdecMileageCharge As Decimal
  	Private mdecDiscountRate As Decimal
  	Private mdecDailyCharge As Decimal
  	Private mdecDailyRate As Decimal
  
  	Public Sub New()
  
  	End Sub
  
  	'============================================================
  	'Property : NumberOfDays
  	'============================================================
  	'Number of days used to calculate the Rental.
  	'============================================================
  	Public Property NumberOfDays() As Decimal
  		Get
  			Return mdecNumberOfDays
  		End Get
  		Set(ByVal Value As Decimal)
  			If Value >= 1 And Value <= 180 Then
  				mdecNumberOfDays = Value
  			Else
 			 Throw New BusinessException("Days Rented is outside of valid range", "Days Rented")
  			End If
  		End Set
  	End Property
  
  	'============================================================
  	'Property : TypeOfCar
  	'============================================================
  	'Car type used to calculate the Rental.
  	'============================================================
  	Public Property TypeOfCar() As String
  		Get
  			Return mstrTypeOfCar
  		End Get
  		Set(ByVal Value As String)
 			If Value = "Economy" Or Value = "Midsize" Or Value = "Luxury" Then
  				mstrTypeOfCar = Value
  			Else
 			 Throw New BusinessException("Type of car is not of valid type", "Car Type")
  			End If
  		End Set
  	End Property
  
  	'============================================================
  	'Property : StartOdometer
  	'============================================================
  	'Start Odometer reading used to calculate the Rental.
  	'============================================================
  	Public Property StartOdometer() As Integer
  		Get
  			Return mintStartOdo
  		End Get
  		Set(ByVal Value As Integer)
  			If Len(Value) >= 0 And Len(Value) <= 6 Then
  				mintStartOdo = Value
  			Else
 			 Throw New BusinessException("Start Odometer isn't 6 digits", "Start Odometer")
  			End If
  		End Set
  	End Property
  
  	'============================================================
  	'Property : EndOdometer
  	'============================================================
  	'End Odometer reading used to calculate the Rental.
  	'============================================================
  	Public Property EndOdometer() As Integer
  		Get
  			Return mintEndOdo
  		End Get
  		Set(ByVal Value As Integer)
  			If Len(Value) >= 0 And Len(Value) <= 6 Then
  				mintEndOdo = Value
  			Else
 			 Throw New BusinessException("Start Odometer isn't 6 digits", "End Odometer")
  			End If
  		End Set
  	End Property
  
  	'============================================================
  	'Property : AccountType
  	'============================================================
  	'Type of Account used to calculate the Rental.
  	'============================================================
  	Public Property AccountType() As String
  		Get
  			Return mstrAccountType
  		End Get
  		Set(ByVal Value As String)
 			If Value = "Regular" Or Value = "Insurance" Or Value = "Corporate" Then
  				mstrAccountType = Value
  			Else
 			 Throw New BusinessException("Account isn't of valid Type", "Account Type")
  			End If
  		End Set
  	End Property
  
  	'============================================================
  	'Property : DailyCharge
  	'============================================================
  	'Daily Charge returned after rental is calculated.
  	'============================================================
  	Public ReadOnly Property DailyCharge() As Decimal
  		Get
  			Return mdecDailyCharge
  		End Get
  	End Property
  
  	'============================================================
  	'Property : MileageCharge
  	'============================================================
  	'Mileage Charge returned after rental is calculated.
  	'============================================================
  	Public ReadOnly Property MileageCharge() As Decimal
  		Get
  			Return mdecMileageCharge
  		End Get
  	End Property
  
  	'============================================================
  	'Property : DiscountRate
  	'============================================================
  	'Discount rate returned after rental is calculated.
  	'============================================================
  	Public ReadOnly Property DiscountRate() As Decimal
  		Get
  			Return mdecDiscountRate
  		End Get
  	End Property
  
  	'============================================================
  	'Property : TotalRent
  	'============================================================
  	'Total rent returned after rental is calculated.
  	'============================================================
  	Public ReadOnly Property TotalRent() As Decimal
  		Get
  			Return mdecTotalRent
  		End Get
  	End Property
  
  	'============================================================
  	'Property : DailyRate
  	'============================================================
  	'Daily Rate returned after rental is calculated.
  	'============================================================
  	Public ReadOnly Property DailyRate() As Decimal
  		Get
  			Return mdecDailyRate
  		End Get
  	End Property
  
  	'============================================================
  	'Function : CalculateDailyRate
  	'============================================================
  	'Calculates the total daily rate based on the dailyratefee and
  	'the accoutn type.
  	'============================================================
  	Private Function CalculateDailyRate(ByVal DailyRate As Decimal, ByVal AccountType As String) As Decimal
  		Dim decDailyRateCharge As Decimal
  		Select Case AccountType
  			Case "Corporate"
  			    decDailyRateCharge = (mdecNumberOfDays * DailyRate)
  			Case "Insurance"
  			    decDailyRateCharge = (mdecNumberOfDays * DailyRate)
  			Case "Regular"
  			    decDailyRateCharge = mdecNumberOfDays * DailyRate
  			Case Else
  				decDailyRateCharge = 0
 			 MessageBox.Show("Oops you're not supposed to see this", "Christian's Car Rental", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
  		End Select
  		Return decDailyRateCharge
  	End Function
  
  	'============================================================
  	'Function : GetMileageCharge
  	'============================================================
  	'Calculates the total mileage charge based on the total miles
  	'Mileage rate, and the account type.
  	'============================================================
 	Private Function GetMileageCharge(ByVal TotalMiles As Decimal, ByVal MileageRate As Decimal, ByVal AccountType As String) As Decimal
  		Dim decMileageCharge As Decimal
  		If AccountType <> "Corporate" Then
  			decMileageCharge = TotalMiles / mdecNumberOfDays
  			If decMileageCharge > 100 Then
 			 decMileageCharge = (decMileageCharge - 100) * MileageRate
 			 decMileageCharge = decMileageCharge * mdecNumberOfDays
  			Else
  				decMileageCharge = 0D
  			End If
  			Return decMileageCharge
  		Else
  			Return 0
  		End If
  	End Function
  
  	'============================================================
  	'Function : CalcDiscount
  	'============================================================
  	'Calculates the applicable discount based on the daily rate
  	'and account type.
  	'============================================================
  	Private Function CalcDiscount(ByVal DailyRateCharge As Decimal, ByVal AccountType As String) As Decimal
  		Dim decDiscountRate As Decimal
  		Select Case mstrAccountType
  			Case "Corporate"
  			    decDiscountRate = DailyRateCharge * 0.05D
  			Case "Insurance"
  			    decDiscountRate = DailyRateCharge * 0.1D
  			Case "Regular"
  			Case Else
  				DailyRateCharge = 0
 			 MessageBox.Show("Oops you're not supposed to see this", "Christian's Car Rental", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
  		End Select
  		Return decDiscountRate
  	End Function
  
  	'============================================================
  	'Sub : CalcTotalRental
  	'============================================================
  	'Calculates the Total Rental and sets the output properties.
  	'============================================================
  	Public Sub CalcTotalRental()
  		Dim decMileageRate As Decimal = GetCarTypeMileageRates(mstrTypeOfCar)
  		Dim decTotalMiles As Decimal = Convert.ToDecimal(mintEndOdo - mintStartOdo)
  		Dim strAccountType As String = mstrAccountType
  		mdecDailyRate = GetCarTypeDailyRates(mstrTypeOfCar)
  		mdecDailyCharge = CalculateDailyRate(mdecDailyRate, strAccountType)
 		mdecMileageCharge = GetMileageCharge(decTotalMiles, decMileageRate, strAccountType)
  		mdecDiscountRate = CalcDiscount(mdecDailyCharge, strAccountType)
  		mdecTotalRent = (mdecDailyCharge + mdecMileageCharge) - mdecDiscountRate
  	End Sub
  
  	'============================================================
  	'Function : GetCarTypeMileageRates
  	'============================================================
  	'Gets the mileage rate based on the type of car.
  	'============================================================
  	Private Function GetCarTypeMileageRates(ByVal Car As String) As Decimal
  		Select Case Car
  			Case "Economy"
  			    Return mdecMileageCharges(MyCarType.Economy)
  			Case "Midsize"
  			    Return mdecMileageCharges(MyCarType.Midsize)
  			Case "Luxury"
  			    Return mdecMileageCharges(MyCarType.Luxury)
  			Case Else
 			 MessageBox.Show("Oops you're not supposed to see this", "Christian's Car Rental", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
  		End Select
  	End Function
  
  	'============================================================
  	'Function : GetCarTypeDailyRates
  	'============================================================
  	'Gets the daily rate based on the type of car.
  	'============================================================
  	Private Function GetCarTypeDailyRates(ByVal Car As String) As Decimal
  		Select Case Car
  			Case "Economy"
  			    Return mdecDailyRates(MyCarType.Economy)
  			Case "Midsize"
  			    Return mdecDailyRates(MyCarType.Midsize)
  			Case "Luxury"
  			    Return mdecDailyRates(MyCarType.Luxury)
  			Case Else
 			 MessageBox.Show("Oops you're not supposed to see this", "Christian's Car Rental", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
  		End Select
  	End Function
  End Class
  
  
  Public Class BusinessException
  	Inherits System.ApplicationException
  
  	'============================================================
  	'Module level variables
  	'============================================================
  	Private invalidFieldString As String
  
  	Sub New(ByVal Message As String, ByVal InvalidField As String)
  		MyBase.New(Message) 'REQUIRED!!!!
  		invalidFieldString = InvalidField
  	End Sub
  
  	Public ReadOnly Property FieldInError() As String
  		Get
  			Return invalidFieldString
  		End Get
  	End Property
  End Class
it has 1 method (this one isnt a function, though you can have it return a value), multiple properties (at least 1 readonly and 1 writeonly plus lots of read&write ones)
it's got private subs (only that class can access it, nothing else) yea, it's got a little of everything (it doesnt draw any graphics like a lable, textbox, combobox, etc.. has stuff drawn on the form) yea.. have fun :)

oh yea, it also has a custom exception class included in case anyone's interesting in how to make a custom exception
 

toytoy

Well-known member
Joined
Jul 16, 2004
Messages
46
Programming Experience
1-3
what about interface.....is it the same as classes?

Can i inherits class and implement interface together...

I have another two questions....

1) Is there a need to create a sub New() in all the classes that i create...

I ask it because i came cross a book where it will create a sub New() everytime it add a new class to it...

what is the purpose of that and what would happen if i left that out...

2) Just to ask for your opinion..

in what situation will you consider create a class best with

1) function procedure and property
2) sub procedure and property
3) Just property
4) All of the above together

and do you create a Sub New() inside depending on certain situation..

Thanks
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
as long as the implemented interface isnt inherited (single inheritance thing) it's all good

1) i dont believe it's neccessary to have an empty Sub New, but a good programmer would include it (you can have an overloaded Sub New, one blank, one with some properties set upon instantiation, yada, yada.....)

2) Multi-Teir apps is what most of the books use as examples of "Creating your own class"

(added) 3) you might want to consider picking up a vb.net book that includes Makeing Classes, a book could answer much more than i
 
Top Bottom