I am getting a System.StackOverflowException when I create an instance of my Vehicle class. I believe it is happening because I have an object reference to my Car class which inherits from Vehicle, and it is getting stuck an infinite loop.
Public Class Vehicle
Public Car As New Car
End Class
Public Class Car
Inherits Vehicle
Public Sub doStuff()
'does stuff
End Sub
End Class
While probably bad practice, I have it structured like this because I need to be able to access doStuff() from another file without having to create an instance of Car, like so:
'some other file
Public Class Foo
Private Vehicle As New Vehicle
Vehicle.Car.doStuff() 'this is what I am trying to accomplish
End Class
Is there another way I can accomplish this?
EDIT: Since there seems to be a little bit of confusion, I want to clarify that I have multiple classes that inherit from Vehicle (Car, Truck, Bike, etc.). All of these classes have their own unique methods but all need to use some of the methods and properties of Vehicle. Using virtual isn't exactly what I am looking for since I am not needing to override any methods.
doStuff()does, it's hard to tell, but "I need to be able to accessdoStuff()from another file without having to create an instance ofCar" would typically be solved by making itShared- i.e.Public Shared Sub doStuff(). You could then call it directly from a method inFoo:Car.doStuff().Dim c = New Car()thenCar.Park()orTruck.Honkwhere Park and Honk are Vehicle methods. Since Car inherits from Vehicle all the base class methods will be available (directly)