473,399 Members | 3,302 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,399 software developers and data experts.

DirectCast Operator

Dim i As String = "10.56"
Dim j As String
j = DirectCast(i, String)
Console.WriteLine(j)

OUTPUT- 10.56

I want to ask that i have used directcast in Right way. This ques tion is asked to me in interview. We can simply assign the value of i to j. Wats the use of then DirectCast Operator.
Mar 29 '08 #1
5 1842
balabaster
797 Expert 512MB
Dim i As String = "10.56"
Dim j As String
j = DirectCast(i, String)
Console.WriteLine(j)

OUTPUT- 10.56

I want to ask that i have used directcast in Right way. This ques tion is asked to me in interview. We can simply assign the value of i to j. Wats the use of then DirectCast Operator.
Um...I don't think you've quite got the essence of directcast. Directcast would be used to state that the value of the object is of the type you specify.

In the example you've given the variable i is already set as type string, so using direct cast to assign it to j doesn't really have any effect. DirectCast is generally used for specifying types of otherwise unidentifiable objects rather than for type conversion as described in a lot of documentation - for instance:

Dim o As Object = 5.213

Now, we reference object o after the fact. What type of data is in o (without looking back to where we assigned the value)? It's type "object", so who knows what is in there - it really could be any type, it could be single, double, string etc.

So we have another variable we wish to use the data from this variable o.

Dim MySingle As Single = DirectCast(o, single)
Dim MyDouble As Double = DirectCast(o, double)
Dim MyString As String = DirectCast(o, string)

So we are really telling the system that the value stored in object in its literal format is of the type we specified - in the first line, we are saying: "The type of data held in object o is of type single, assign this value to the variable MySingle"

If we tried:

Dim MyInteger As Integer = DirectCast(o, integer)

This would fail - because 5.213 is not an integer, it's a floating point numeric.

However, in this case, the CType method would work because we're taking the value from object and telling the system to convert the value held in object o to an integer rather than just saying "I want to view the value in object o and the value in object o is of type integer" - in which case, your application is going to complain "you're obviously on crack, 5.213 is not an integer in any world of math I'm familiar with".

however the following succeeds:

Dim MyInteger As Integer = CType(o, integer)

Why? Well CType isn't saying the same thing as DirectCast - CType is saying I want to view the value in object o and whatever value you have in object o, convert it to its integer representation.

So MyInteger now holds value 5.

Make sense?
Mar 29 '08 #2
Dim i As Object = 10.56
Dim j As Single = DirectCast(i, Single)
Sub main()
Console.WriteLine(j)
Console.ReadLine()
End Sub

hi check the code above, its not woking. Its same wat u replied in the previous scrap.

Read this It is from MSDN
The DirectCast keyword introduces a type conversion operation. You use it the same way you use the CType keyword, as the following example shows:

Copy Code
Dim Q As Object = 2.37 ' Requires Option Strict to be Off.
Dim I As Integer = CType(Q, Integer) ' Succeeds.
Dim J As Integer = DirectCast(Q, Integer) ' Fails.
The difference between the two keywords is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type.

Wat d bold text means??? On this basis, I thought directcast is same as converting the value of one data type to same data type.
Mar 29 '08 #3
balabaster
797 Expert 512MB
Dim i As Object = 10.56
Dim j As Single = DirectCast(i, Single)
Sub main()
Console.WriteLine(j)
Console.ReadLine()
End Sub

hi check the code above, its not woking. Its same wat u replied in the previous scrap.

Read this It is from MSDN
The DirectCast keyword introduces a type conversion operation. You use it the same way you use the CType keyword, as the following example shows:

Copy Code
Dim Q As Object = 2.37 ' Requires Option Strict to be Off.
Dim I As Integer = CType(Q, Integer) ' Succeeds.
Dim J As Integer = DirectCast(Q, Integer) ' Fails.
The difference between the two keywords is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type.

Wat d bold text means??? On this basis, I thought directcast is same as converting the value of one data type to same data type.
Sorry - 5.213 isn't a valid Single - bad example on my part...try doing DirectCast(o, Double) - that'll work.

DirectCast requires the run-time type (i.e. the type of the data contained in the object at run-time) of an object variable to be the same as the specified type (i.e. the type you are specifying).

Microsoft confusingly uses this idiom "conversion" when you're not actually converting, you're casting. With DirectCast, you're saying "This object of otherwise undeterminable type holds data of type [whatever type it is]", you're not saying "I want to convert this data to type [whatever type I want]". With CType you're saying "I want to convert whatever data this object holds to type [whatever type I want]".

Imagine the following cooking example:
I bake a cake...I throw the cake mix on a baking tray in a heap...when it comes out of the oven it looks like some kind of bread, but we really don't know what it is, or that it's even cake, let alone what type of cake until we bite into it.

Now, we do the same thing, but this time we put it in a cake form...it comes out of the oven, it's still the same mixture as it was before, but it looks like a cake. We still don't know what type of cake - but at least we know it's a cake - we've cast the object as type cake. We bite into it and now know what type of cake we have - so in two steps, we've cast this cooked object first as type cake (with the cake form) and secondly we've tasted it, effectively casting it as type carrot cake - but it's still the same type as the undeterminable cake from the first part of the example.

Now, we want to convert this cake to finger cakes. The cake wasn't of type finger cakes before, so we can't say it is now. Just because we say the cake is a finger cake, doesn't make it so. In this instance, we have to convert the cake to finger cakes by cutting it into a bunch of slices.
Mar 29 '08 #4
hey 5.123 is a valid single.................
Mar 29 '08 #5
balabaster
797 Expert 512MB
hey 5.123 is a valid single.................
Not according to VS 2008. If you do this:

Dim n As Single = 5.213

Hold your mouse over where you typed 5.213 - it'll say "Double precision number". You can't cast a double as a single...a double is a double. You can convert it to a single using CType however...
Mar 30 '08 #6

Sign in to post your reply or Sign up for a free account.

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
1
by: iwdu15 | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.