472,805 Members | 3,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 4056
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.