VB.net 全局变量不起作用吗?
我在 VB.Net 中遇到全局变量问题。
在一个子程序中(由按钮触发),我创建对象并将它们存储到全局变量(数组)中。之后我创建一个表(DataGrid)。
通过第二个按钮,另一个子被触发并尝试使用全局变量中的数组,但它似乎是空的。
这是代码:
<script runat="server">
Dim array_datensatz(12) As Datensatz
Sub submit1_click(ByVal Sender As Object, ByVal E As EventArgs)
Dim cellX As New TableCell()
For n = 0 To 12 '13 objects a created
Dim tmpklasse As New Datensatz(n)
array_datensatz(n) = tmpklasse
' MsgBox(tmpklasse.methode_merkmal1) ' method_merkmal1 returns a Integer
' MsgBox(array_datensatz(n).methode_merkmal1()) 'both work
Next n
ItemsGrid.DataSource = CreateDataSource() 'creates table
ItemsGrid.DataBind()
End Sub
Function CreateDataSource() As ICollection
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("Merkmal1"))
dt.Columns.Add(New DataColumn("Merkmal2"))
dt.Columns.Add(New DataColumn("Merkmal3"))
Dim i As Integer
For i = 0 To (array_datensatz.GetLength(0) - 1) 'länge des array der ergebnise (0. dim) = #results
dr = dt.NewRow()
dr(0) = array_datensatz(i).methode_merkmal1()
dr(1) = array_datensatz(i).methode_merkmal2()
dr(2) = array_datensatz(i).methode_merkmal3()
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function 'CreateDataSource
Public Sub sub_anonym(ByVal Sender As Object, ByVal E As EventArgs)
For i = 0 To (array_datensatz.GetLength(0) - 1)
MsgBox(array_datensatz(i).methode_merkmal1()) 'throws an exception saying that ~"the object reference doesn't target an object-instance"
Next i
End Sub
</script>
有什么问题吗? 为什么数组中没有对象?
I have a problem with a global variable in VB.Net.
In one sub (triggered by a button) I create objects and store them into a global variable (an array). After that I create a table (DataGrid).
With a second button another sub is triggered and tries to use the array from the global variable, but it seems to be empty.
Here is the code:
<script runat="server">
Dim array_datensatz(12) As Datensatz
Sub submit1_click(ByVal Sender As Object, ByVal E As EventArgs)
Dim cellX As New TableCell()
For n = 0 To 12 '13 objects a created
Dim tmpklasse As New Datensatz(n)
array_datensatz(n) = tmpklasse
' MsgBox(tmpklasse.methode_merkmal1) ' method_merkmal1 returns a Integer
' MsgBox(array_datensatz(n).methode_merkmal1()) 'both work
Next n
ItemsGrid.DataSource = CreateDataSource() 'creates table
ItemsGrid.DataBind()
End Sub
Function CreateDataSource() As ICollection
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("Merkmal1"))
dt.Columns.Add(New DataColumn("Merkmal2"))
dt.Columns.Add(New DataColumn("Merkmal3"))
Dim i As Integer
For i = 0 To (array_datensatz.GetLength(0) - 1) 'länge des array der ergebnise (0. dim) = #results
dr = dt.NewRow()
dr(0) = array_datensatz(i).methode_merkmal1()
dr(1) = array_datensatz(i).methode_merkmal2()
dr(2) = array_datensatz(i).methode_merkmal3()
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function 'CreateDataSource
Public Sub sub_anonym(ByVal Sender As Object, ByVal E As EventArgs)
For i = 0 To (array_datensatz.GetLength(0) - 1)
MsgBox(array_datensatz(i).methode_merkmal1()) 'throws an exception saying that ~"the object reference doesn't target an object-instance"
Next i
End Sub
</script>
What is the problem?
Why arn't the objects in the array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是在网站上吗?
如果是这样,用户单击第二个按钮将创建另一个请求。您要保存的变量不会在请求之间保存,因此它不再存在(因为它是在第一个请求上设置的,现已完成)。要在多个请求中保留此类值,您需要使用一种持续更长时间的存储方式,例如会话。
This is in a website?
If so, the user clicking the second button creates another request. The variable you're saving to isn't saved between requests, so it's not there anymore (as it was set on the first request, which is now completed). To preserve values like that across multiple requests you'll need to use a way of storing them that lasts longer, such as a Session.