VB.net 全局变量不起作用吗?

发布于 2024-11-30 10:17:30 字数 1862 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一向肩并 2024-12-07 10:17:30

这是在网站上吗?

如果是这样,用户单击第二个按钮将创建另一个请求。您要保存的变量不会在请求之间保存,因此它不再存在(因为它是在第一个请求上设置的,现已完成)。要在多个请求中保留此类值,您需要使用一种持续更长时间的存储方式,例如会话。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文