title: Why Cast (CType/DirectCast) controls versus implicit conversion
body: <p>Suppose I have a control within a webforms GridViewRow...</p>
  
  <pre><code>&lt;asp:Literal ID="ltl_abc" runat="server" /&gt;
  </code></pre>
  
  <p>Within the RowDataBound event I can access the control using any of the following methods.  I've always used DirectCast historically:</p>
  
  <pre><code>Protected Sub gv_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv.RowDataBound
      Select Case e.Row.RowType
          Case DataControlRowType.DataRow
              ' 1) Dim ltl_abc As Literal = DirectCast(e.Row.FindControl("ltl_abc"), Literal)
              ' 2) Dim ltl_abc As Literal = CType(e.Row.FindControl("ltl_abc"), Literal)
              ' 3) Dim ltl_abc As Literal = e.Row.FindControl("ltl_abc")
  </code></pre>
  
  <p>Is there any advantage of using any particular approach?  I guess DirectCast is slightly more efficient, but possibly prone to errors, but are there any dangers of the implicit cast (option 3)?</p>
  
  <p>Historically I've never seen any errors until I try to actually assign a value to the control's property, which makes me think that this first step isn't really that important?</p>
  
  <p><strong>Please note this is not meant to be a DirectCast vs CType discussion, more about whether casting is even necessary here?</strong></p>
  
  <p><strong>Update for clarity</strong></p>
  
  <pre><code>Protected Sub gv_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv.RowDataBound
      Select Case e.Row.RowType
          Case DataControlRowType.DataRow
  
              ' This works fine, but no explicit casting is done:
              Dim ltl_abc As Literal = e.Row.FindControl("ltl_abc") ' no (explicit) cast
              ltl_abc.Text = "Hello World"
  
              ' This also works until I try to access the object's properties
              Dim ltl_abc As Literal = DirectCast(e.Row.FindControl("NonExistentId"), Literal)
  </code></pre>
  
  <p>Why therefore should a developer cast (in this example), or is this example just too simple?</p>