Vb.net Gridview "pointer"?
I have code that interacts with a gridview, and the code is exactly the same for multiple gridviews. So can I do something like this:
Dim gridViewPointer As GridView
If (gridViewNumber = 1) Then
gridViewPointer = GridView1
ElseIf (gridViewNumber = 8) Then
gridViewPointer = GridView8
...
and then
If (gridViewPointer.DataSourceID = SQLDatasourcetemp.ID) Then
...
Will this work or is there another way to d开发者_Python百科o this?
Edit: I'm checking to make sure that the data the user is inputting into the gridview is correct. It could be one of 4 gridviews, and the checks are exactly the same, the only parameter that changes in the code is gridview1/gridview2/etc. So if I can use a "pointer" to the correct gridview then I can elimninate all the duplicate code.
Yes that is not a problem at all.
Whenever you assign an object to a variable you are actually assigning a memory reference to the variable. Using that reference you can read, write, and call all properties and methods of the object as if it were there original.
You might want to read up on the differences between value and reference types. This is primarily a concern when passing data through function calls.
http://msdn.microsoft.com/en-us/library/t63sy5hs%28VS.80%29.aspx
In fact I would probably create a new function to call on the gridview...
Private Sub GridOperations(ByVal grid as GridView)
//Do work here.
End Sub
If (gridViewNumber = 1) Then
GridOperations(GridView1)
ElseIf (gridViewNumber =8) Then
GridOperations(GridView8)
...
What you are asking is correct. When you set gridViewPointer = GridView1, you are actually only storing the pointer to the GridView1 object, not copying the object, so any action you perform on gridViewPointer after the set will directly control GridView1.
精彩评论