0

Is there a way to declare a CollectionBase to be of a certain object type? So say I want to create a CollectionBase that accepts object type of car

Public Class carCollection
    Inherits CollectionBase

In my main program, say I declare and assign a car to the collection

Dim myCarCollection As New carCollection
Dim myCar As car = New car()
myCarCollection.Add(myCar);

When I try this, Compiler tells me it disallows late binding:

myCarCollection(0).Drive()

I have to convert first so it's early bound in order to fix it:

(CType(myCarCollection(0),car).Drive()

Is there a way to just declare carCollection to be a CollectionBase of type car first, so I don't have to convert the type every time I want to make a call?

Turning option restrict off is a workaround too but I just feel like there should be a way to declare what type of object your collectionBase is going to hold.

Hrqls
  • 2,944
  • 4
  • 34
  • 54
rawr rang
  • 763
  • 2
  • 7
  • 24

0 Answers0