473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

directcast

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
Nov 21 '05 #1
1 4086
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

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sahil Malik | last post by:
I can't find it .. what am I missing? - Sahil Malik http://dotnetjunkies.com/weblog/sahilmalik
4
by: Andreas Klemt | last post by:
Hello, what has the better performance and what are you using? Dim myObj As Object = 70 a) Dim myInt As Integer = DirectCast(myObj, Integer) b) Dim myInt As Integer = Convert.ToInt32(myObj) ...
11
by: Tubs | last post by:
i am attempting to write something which can morph itself to whatever comes in and get the value property from it but i don't know what type it is until runtime. I am therefore trying to use...
13
by: Crirus | last post by:
Can I use DirectCast to convert a object to it's base or should I use CType? I have a instance of a class as Object. The run-time tipe is a derived of a class, but I need to refer to that instance...
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
5
by: Michael Ramey | last post by:
Hello, There are quite a few ways to convert one object, say an integer to a string. Dim myStr as string dim myInt as integer = 123 myStr = cstr(myInt) myStr = myInt.toString()
6
by: Mark Nethercott | last post by:
I get the following failure when trying to access the builtin properties; An unhandled exception of type 'System.InvalidCastException' occurred in resultsoutput.dll Additional information:...
7
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
3
by: =?Utf-8?B?TWlrZQ==?= | last post by:
If Visual Studio knows the type, why does the system-generated property use CType instead of DirectCast? DirectCast is more efficient right? For example - Here's what we have for a setting...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.