locked
directcast, trycast, ctype RRS feed

  • Question

  • User1913822523 posted

    I've tried to make sense of these. And im down to directcast vs. trycast in most cases. My question is directcast vs. trycast when bringing in Controls like a label from a formview to change the text: DirectCast(Formview1.FindControl("Label1"), label).Text = "abc". Shall prefer TryCast over DirectCast in such situations? Both works but is there any "performance gain" or something else?

    /curious

    Sunday, November 8, 2009 5:42 PM

Answers

  • User-158764254 posted

    The difference between a DirectCast and a TryCast is that TryCast will return Nothing if the cast cant be performed while a DirectCast will throw an exception if the cast cant be performed. 

    TryCast can be used where the cast may fail to avoid the performance penalty associated with throwing an exception.  But, you need to check the return from the TryCast to see if it is Nothing before you can safely work with it.

    In you example above, I would just use a DirectCast as the TryCast doesn't benefit you.  If for any reason, you cannot cast the control you found to a Label control, then DirectCast will throw an exception.  TryCast will not throw an exception, but it will return Nothing and as soon as you try to use the Text property of Nothing, you'll get an object reference exception.

    If you wanted to code the cast a little more defensively you could do something like this:

            Dim lbl As Label = TryCast(Formview1.FindControl("Label1"), Label)
            If Not lbl Is Nothing Then
                lbl.Text = "abc"
            End If
    


    In this case, by incorporating the TryCast, if FindControl fails to find the label control, or finds something that is not a label control, no exception will be thrown and the lbl object will simply be Nothing.

    From a performance perspective, i would think TryCast has just a little more going on under the hood than DirectCast in order to prevent the exceptions but i suspect that the performance differences are extremely minimal.

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 8, 2009 8:45 PM

All replies

  • User-158764254 posted

    The difference between a DirectCast and a TryCast is that TryCast will return Nothing if the cast cant be performed while a DirectCast will throw an exception if the cast cant be performed. 

    TryCast can be used where the cast may fail to avoid the performance penalty associated with throwing an exception.  But, you need to check the return from the TryCast to see if it is Nothing before you can safely work with it.

    In you example above, I would just use a DirectCast as the TryCast doesn't benefit you.  If for any reason, you cannot cast the control you found to a Label control, then DirectCast will throw an exception.  TryCast will not throw an exception, but it will return Nothing and as soon as you try to use the Text property of Nothing, you'll get an object reference exception.

    If you wanted to code the cast a little more defensively you could do something like this:

            Dim lbl As Label = TryCast(Formview1.FindControl("Label1"), Label)
            If Not lbl Is Nothing Then
                lbl.Text = "abc"
            End If
    


    In this case, by incorporating the TryCast, if FindControl fails to find the label control, or finds something that is not a label control, no exception will be thrown and the lbl object will simply be Nothing.

    From a performance perspective, i would think TryCast has just a little more going on under the hood than DirectCast in order to prevent the exceptions but i suspect that the performance differences are extremely minimal.

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 8, 2009 8:45 PM
  • User1913822523 posted

    I appreciate that elaborated answer, thanks!


    Monday, November 9, 2009 5:42 AM