1

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.

9
  • Vehicle shouldn't instantiate a car class, it should be the other way around Commented Jul 7, 2017 at 17:02
  • 1
    Without knowing what doStuff() does, it's hard to tell, but "I need to be able to access doStuff() from another file without having to create an instance of Car" would typically be solved by making it Shared - i.e. Public Shared Sub doStuff(). You could then call it directly from a method in Foo: Car.doStuff(). Commented Jul 7, 2017 at 17:09
  • 5
    You dont just 'have an object reference to the Car class` vehicle creates a new Car instance and Car inherits from Vehicle, hence the endless loop: Vehicle -> Car -> Vehicle... Commented Jul 7, 2017 at 17:13
  • @Plutonix ah yes that is what I meant, sorry for the incorrect wording. Is it possible to make the Car instance not inheritable so that endless loop doesn't happen? Or do I need to completely rethink the way I want to structure this? Commented Jul 7, 2017 at 17:55
  • 1
    Thats not how inheritance works. You dont need a Vehicle instance, just a car or truck instance. Dim c = New Car() then Car.Park() or Truck.Honk where Park and Honk are Vehicle methods. Since Car inherits from Vehicle all the base class methods will be available (directly) Commented Jul 7, 2017 at 18:17

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Lews - thanks! I didn't even notice that. I edited my answer to change it to vb.net

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.