Equivalent in VB.Net
-
Tuesday, April 01, 2008 5:50 PM
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
GridView1.EditIndex = -1;
FillCustomerInGrid();
}What is the equivalent code in vb.net for the highlighted line?
Thank you,
Gish
Answers
-
Tuesday, April 01, 2008 5:54 PM
TextBox txtName = DirectCast(GridView1, TextBox).Rows(e.RowIndex).FindControl("txtName")
or perhaps
TextBox txtName = DirectCast(GridView1, TextBox).Rows.Item(e.RowIndex).FindControl("txtName")
-
Tuesday, April 01, 2008 5:54 PM
Code SnippetProtected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim txtName As TextBox = _
DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtName"), TextBox)
GridView1.EditIndex = -1
FillCustomerInGrid()
End Sub -
Tuesday, April 01, 2008 6:21 PM
Borrowed from: http://www.novicksoftware.com/TipsAndTricks/tip-visual-basic-ctype-directcast-dot-net.htm
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 and DirectCast requires that the run-time type of an object variable to be the same as the specified type that it's being cast to. Really the same, not just that one can be converted to the other.
Use DirectCast if you're absolutely positively sure that an object is the specified type and the run-time type of the expression are the same. If you're not sure but expect that the conversion will work, use CType. The run-time performance of DirectCast is better than that of CType. However, DirectCast throws an InvalidCastException error if the argument types do not match, so you must be sure.
-
Tuesday, April 01, 2008 6:26 PM
DirectCast is the equivalent of the C# type cast, so if the C# code works then DirectCast will, so it is safe to go with DirectCast.
All Replies
-
Tuesday, April 01, 2008 5:54 PM
TextBox txtName = DirectCast(GridView1, TextBox).Rows(e.RowIndex).FindControl("txtName")
or perhaps
TextBox txtName = DirectCast(GridView1, TextBox).Rows.Item(e.RowIndex).FindControl("txtName")
-
Tuesday, April 01, 2008 5:54 PM
Code SnippetProtected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim txtName As TextBox = _
DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtName"), TextBox)
GridView1.EditIndex = -1
FillCustomerInGrid()
End Sub -
Tuesday, April 01, 2008 6:01 PM
I use to use CType in these occasions. Does DirectCast is better than CType?
Thank you,
Gish
-
Tuesday, April 01, 2008 6:21 PM
Borrowed from: http://www.novicksoftware.com/TipsAndTricks/tip-visual-basic-ctype-directcast-dot-net.htm
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 and DirectCast requires that the run-time type of an object variable to be the same as the specified type that it's being cast to. Really the same, not just that one can be converted to the other.
Use DirectCast if you're absolutely positively sure that an object is the specified type and the run-time type of the expression are the same. If you're not sure but expect that the conversion will work, use CType. The run-time performance of DirectCast is better than that of CType. However, DirectCast throws an InvalidCastException error if the argument types do not match, so you must be sure.
-
Tuesday, April 01, 2008 6:26 PM
DirectCast is the equivalent of the C# type cast, so if the C# code works then DirectCast will, so it is safe to go with DirectCast.
-
Tuesday, April 01, 2008 10:45 PM
Not quite.
This C# code works fine:
i = (int)someDouble;
but the following VB code will not compile:
i = DirectCast(someDouble, Integer)
Also, integer casts have further differences between C# and VB.
I wrote up some notes on this here:
http://www.tangiblesoftwaresolutions.com/Articles/VB%20Equivalent%20to%20CSharp%20Casts.htm

