Can anyone spot a problem with this VB.Net code?

I am writing some code in VB.Net, which, I hope, will show my colleagues (not to say, get to know myself a little) with various design templates - and I had a problem with the FactoryMethod template.

Here is my code:

Namespace Patterns.Creational.FactoryMethod ''' <summary> ''' This is the Factory bit - the other classes are merely by way of an example... ''' </summary> Public Class CarFactory ''' <summary> ''' CreateCar could have been declared as Shared (in other words,a Class method) - it doesn't really matter. ''' Don't worry too much about the contents of the CreateCar method - the point is that it decides which type ''' of car should be created, and then returns a new instance of that specific subclass of Car. ''' </summary> Public Function CreateCar() As Car Dim blnMondeoCondition As Boolean = False Dim blnFocusCondition As Boolean = False Dim blnFiestaCondition As Boolean = False If blnMondeoCondition Then Return New Mondeo() ElseIf blnFocusCondition Then Return New Focus() ElseIf blnFiestaCondition Then Return New Fiesta() Else Throw New ApplicationException("Unable to create a car...") End If End Function End Class Public MustInherit Class Car Public MustOverride ReadOnly Property Price() As Decimal End Class Public Class Mondeo Inherits Car Public ReadOnly Overrides Property Price() As Decimal Get Return 17000 End Get End Property End Class Public Class Focus Inherits Car Public ReadOnly Overrides Property Price() As Decimal Get Return 14000 End Get End Property End Class Public Class Fiesta Inherits Car Public ReadOnly Overrides Property Price() As Decimal Get Return 12000 End Get End Property End Class End Namespace 

When I try to compile this, I get errors (BC30311) in CarFactory.CreateCar telling me that it cannot convert Fiesta, Mondeo and Focus to Car. I don’t understand what the problem is - they are all subclasses of Car.

Sure, I'm missing something simple. Can anyone notice this?

Greetings

Martin.

+4
source share
3 answers

The Inherits keyword should be on a new line. This is confirmed by Microsoft with their help and support. http://support.microsoft.com/kb/307222

Modify the SavingsAccount class definition as follows, so that the SavingsAccount inherits the account (note that the Inherits keyword should appear on a new line):

+2
source

Place Inherits on a new line or use : to separate the class name and the Inherits operator:

 Public Class Mondeo Inherits Car ... Public Class Focus Inherits Car ... Public Class Fiesta Inherits Car ... 
+4
source

The first error in the list is only the number with the lowest number; this is not always the actual cause of the error.

Further in the list of errors you will see (among several others) three more errors saying End of statement expected. in each of the subclasses. This beacuse Class and Inherits are separate statements and go on separate lines:

 Public Class Mondeo Inherits Car 

or

 Public Class Mondeo : Inherits Car 

When you fix these errors, classes really inherit from Car , and your code works.

+1
source

All Articles