DirectCast Operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonia.sardana
    New Member
    • Jul 2006
    • 95

    DirectCast Operator

    Dim i As String = "10.56"
    Dim j As String
    j = DirectCast(i, String)
    Console.WriteLi ne(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.
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Originally posted by sonia.sardana
    Dim i As String = "10.56"
    Dim j As String
    j = DirectCast(i, String)
    Console.WriteLi ne(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?

    Comment

    • sonia.sardana
      New Member
      • Jul 2006
      • 95

      #3
      Dim i As Object = 10.56
      Dim j As Single = DirectCast(i, Single)
      Sub main()
      Console.WriteLi ne(j)
      Console.ReadLin e()
      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.

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by sonia.sardana
        Dim i As Object = 10.56
        Dim j As Single = DirectCast(i, Single)
        Sub main()
        Console.WriteLi ne(j)
        Console.ReadLin e()
        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:
        Originally posted by Me
        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.

        Comment

        • sonia.sardana
          New Member
          • Jul 2006
          • 95

          #5
          hey 5.123 is a valid single......... ........

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Originally posted by sonia.sardana
            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...

            Comment

            Working...