C
Codemonkey
Hi,
Sorry for a stupid question, but is it possible to do a narrowing conversion
with an object array with Option Strict On in VB?
E.g:
------------------
Dim aBase as Base() = {New Derived(), New Derived(), New Derived()}
Dim aDerived as Derived()
' Compiler error with Option Strict On, Run Time "Invalid Cast" exception
with Strict OFF
aDerived = aBase
' Compiles with Strict On, still gets Run Time "Invalid Cast" exception
aDerived = ctype(aBase, Derived())
------------------
I would have thought that this is a valid assignment and a run time
exception should only be raised if the objects held in aBase were not of
type Derived. The sample in the help file seems to work fine for a narrowing
conversion between an object array that contains strings and a string array,
so why doesn't this work with inherited objects? Is it a case of my brain
being fried on a Friday afternoon?
I can get around it by the Using Array.CopyTo method (which is probably the
same thing that is done under the scenes), but would like a neater way of
doing it.
Thanks for your help.
Trev.
Sorry for a stupid question, but is it possible to do a narrowing conversion
with an object array with Option Strict On in VB?
E.g:
------------------
Dim aBase as Base() = {New Derived(), New Derived(), New Derived()}
Dim aDerived as Derived()
' Compiler error with Option Strict On, Run Time "Invalid Cast" exception
with Strict OFF
aDerived = aBase
' Compiles with Strict On, still gets Run Time "Invalid Cast" exception
aDerived = ctype(aBase, Derived())
------------------
I would have thought that this is a valid assignment and a run time
exception should only be raised if the objects held in aBase were not of
type Derived. The sample in the help file seems to work fine for a narrowing
conversion between an object array that contains strings and a string array,
so why doesn't this work with inherited objects? Is it a case of my brain
being fried on a Friday afternoon?
I can get around it by the Using Array.CopyTo method (which is probably the
same thing that is done under the scenes), but would like a neater way of
doing it.
Thanks for your help.
Trev.