You can use Directcast to convert objects to their actual type...it cannot be
used to convert to types other than the underlying type of an object. For
example;
Public Class myClass
.....
Public ReadOnly Property Length() as single
Get
Return 25.6
End get
End Property
End Class
Dim myArrayList as new ArrayList
Dim myClassInstance as New myClass
myArrayList.Add(myClassInstance) .
You could use Directcast as follows:
Dim lgth as Single = DirectCast(myArrayList(0), myClass).Length
or a simpler example:
Dim obj as Object
dim i as Integer = 25
obj = i
Dim j as integer = DirectCast(obj, Integer) 'Works to convert obj to an
Integer
dim j as integer = Ctype(obj, Integer) 'Works to convert obj to an Integer
Dim j as Double = DirectCast(obj, Double) 'Doesn't work because the
underlyng type of obj is not a Double.
Dim j as Double = Ctype(obj, Double) 'Works because the underlying obj type
of integer can be converted to a double.
You should use Directcast when possible to convert objects to their
underlying types instead of Ctype because it is faster. You could also use
Option Explicit OFF to avoid either DirectCast or Ctype by using late binding
but this is much slower and is more prone to allowing hard to detect errors
to be introduced into your procedures.
In VB.Net 2005, a lot of DirectCasts will not be required becasue of the
implemetation of "Generics".
Hope this helps
Dennis in Houston
"iwdu15" wrote:
can anyone explain the directcast code...ive tried using it and lookin it up
but im lookin for an easy definition and how it works...ive tried using it
before byut it throws errors saying it can convert it
--
-iwdu15