erik.ohlin
erik.ohlin

Reputation: 127

System.StackOverflowException when creating instance of class

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.

Upvotes: 1

Views: 996

Answers (1)

Kevin
Kevin

Reputation: 2243

Okay, I think this might be a misunderstanding of why you want to derive classes. Let me give you an example using your two classes:

Public Class Vehicle
    Public Sub DoVehicleStuff()
        ' code here
    End Sub
    Public Overridable Sub OverridableVehicleStuff()
        'code here
    End Sub
End Class

Public Class Car
    Inherits Vehicle

    Public Sub DoCarStuff()
        'code here
    End Sub
    Public Overrides Sub OverridableVehicleStuff()
        'code here
    End Sub
End Class

Okay, Vehicle has two functions - and one of them is 'overridable', which means it's there, but derived classes can override it. Then, Car inherits from Vehicle - which means it gets the same DoVehicleStuff() and OverridableVehicleStuff(). But then it adds an additional function DoCarStuff() and replaces the OverridableVehicleStuff with its own version of the function.

Putting it all together, we can use the classes like this:

Dim myVehicle As Vehicle = New Vehicle()
myVehicle.DoVehicleStuff()
myVehicle.OverridableVehicleStuff() ' does the VEHICLE version of the function
'this will not compile: myVehicle.DoCarStuff() - because vehicles do not have that function.  It is in the car sub-class.

Dim myCarStoredInVehicle As Vehicle = New Car()
myCarStoredInVehicle.DoVehicleStuff() 'I can still use this function - because Car inherited it
myCarStoredInVehicle.OverridableVehicleStuff() 'this will do the CAR version of the function.
'this will not compile: myCarStoredInVehicle.DoCarStuff()

Dim myCar As Car = New Car()
myCar.DoVehicleStuff() 'I can still call this function.
myCar.DoCarStuff() 'and I can do the DoCarStuff() function, because the variable is a Car.

So to answer your question: is doStuff() something that all vehicles need, or just cars? If it's something that all vehicles need to do in some manner or another, you need to write a overridable (or abstract) function. If it's something that only Cars use, then you can't use a Vehicle class instance for what you want. You want one of these options:

  1. Declare your variable as a Car.
  2. Declare your variable as a Vehicle, but instantiate it as a New Car(). And then cast the variable as a (Car) when you need to use DoStuff().

EDIT: Whoops, the OP is asking about VB, not C#. Edited the code as appropriate.

Upvotes: 3

Related Questions