简体   繁体   English

Vb.net Gridview“指针”?

[英]Vb.net Gridview “pointer”?

I have code that interacts with a gridview, and the code is exactly the same for multiple gridviews. 我有与gridview交互的代码,并且对于多个gridview来说,该代码是完全相同的。 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 do this? 请问这项工作或还有另一种方法吗?

Edit: I'm checking to make sure that the data the user is inputting into the gridview is correct. 编辑:我正在检查以确保用户输入到gridview的数据是正确的。 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. 它可能是4个gridview之一,并且检查完全相同,代码中唯一更改的参数是gridview1 / gridview2 / etc。 So if I can use a "pointer" to the correct gridview then I can elimninate all the duplicate code. 因此,如果可以使用“指针”指向正确的gridview,则可以消除所有重复的代码。

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 http://msdn.microsoft.com/zh-CN/library/t63sy5hs%28VS.80%29.aspx

In fact I would probably create a new function to call on the gridview... 实际上,我可能会创建一个新函数来调用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. 设置gridViewPointer = GridView1时,实际上仅存储指向GridView1对象的指针,而不是复制对象,因此在设置之后在gridViewPointer上执行的任何操作将直接控制GridView1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM