在VB.net中仅对类声明全局变量

时间:2018-12-14 11:39:59

标签: vb.net class global-variables

我想知道什么是在类级过程中使用类级变量的足够方法。我有两个位于同一个类中的过程,并且大多数变量对于这两个过程都是相同的。我如何声明此变量。

Public Class modBuild

    Public Shared Sub GetParameterPAOut()
        Dim PA As IScrSubVarGroup

        Dim SubvarGroups As IScrNamedObjectList
        Dim SubvarGroup As IScrSubVarGroup
        Dim nSubvarGroup As Integer

        Dim Subvars As IScrNamedObjectList
        Dim Subvar As IScrSubVar
        Dim nSubvar As Integer

        PA = Mdl.findElement("$G_PA", False)
        Subvars = PA.getSubvarList(False)
        nSubvar = Subvars.count
        SubvarGroups = PA.getSubvarGroupList(False)
        nSubvarGroup = SubvarGroups.count

        Try
            For i As Integer = 0 To nSubvar - 1
                Subvar = Subvars.item(i)
                If Subvar.intent.val = 0 Then
                    dgvBuildSubvarPa.Rows.Add("Main", Subvar.name)
                End If
            Next  
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Shared Sub GetParameterMPOut()
        Dim MP As IScrSubVarGroup

        Dim SubvarGroups As IScrNamedObjectList
        Dim SubvarGroup As IScrSubVarGroup
        Dim nSubvarGroup As Integer

        Dim Subvars As IScrNamedObjectList
        Dim Subvar As IScrSubVar
        Dim nSubvar As Integer

        MP = Mdl.findElement("$G_MP", False)
        Subvars = MP.getSubvarList(False)
        nSubvar = Subvars.count
        SubvarGroups = MP.getSubvarGroupList(False)
        nSubvarGroup = SubvarGroups.count

        Try
            For i As Integer = 0 To nSubvar - 1
                Subvar = Subvars.item(i)
                If Subvar.intent.val = 0 Then
                    dgvBuildSubvarMp.Rows.Add("Main", Subvar.name)
                End If
            Next  
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

尤其是我想在类顶部和过程外部使用以下变量。

Public Class modBuild
        Dim PA As IScrSubVarGroup
        Dim MP As IScrSubVarGroup

        Dim SubvarGroups As IScrNamedObjectList
        Dim SubvarGroup As IScrSubVarGroup
        Dim nSubvarGroup As Integer

        Dim Subvars As IScrNamedObjectList
        Dim Subvar As IScrSubVar
        Dim nSubvar As Integer

        Public Shared Sub GetParameterPAOut()

            PA = Mdl.findElement("$G_PA", False)
            Subvars = PA.getSubvarList(False)
            nSubvar = Subvars.count
            SubvarGroups = PA.getSubvarGroupList(False)
            nSubvarGroup = SubvarGroups.count

            Try
                For i As Integer = 0 To nSubvar - 1
                    Subvar = Subvars.item(i)
                    If Subvar.intent.val = 0 Then
                        dgvBuildSubvarPa.Rows.Add("Main", Subvar.name)
                    End If
                Next  
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

        Public Shared Sub GetParameterMPOut()
            Dim MP As IScrSubVarGroup

            Dim SubvarGroups As IScrNamedObjectList
            Dim SubvarGroup As IScrSubVarGroup
            Dim nSubvarGroup As Integer

            Dim Subvars As IScrNamedObjectList
            Dim Subvar As IScrSubVar
            Dim nSubvar As Integer

            MP = Mdl.findElement("$G_MP", False)
            Subvars = MP.getSubvarList(False)
            nSubvar = Subvars.count
            SubvarGroups = MP.getSubvarGroupList(False)
            nSubvarGroup = SubvarGroups.count

            Try
                For i As Integer = 0 To nSubvar - 1
                    Subvar = Subvars.item(i)
                    If Subvar.intent.val = 0 Then
                        dgvBuildSubvarMp.Rows.Add("Main", Subvar.name)
                    End If
                Next  
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
End Class

2 个答案:

答案 0 :(得分:1)

如果只想访问类中的字段或属性,请将它们声明为Private

Public Class modBuild
   Private PA As IScrSubVarGroup

如果要从GetParameterPAOut的{​​{1}}访问它们,则需要在方法中具有类Shared的实例,或者使字段也{ {1}}。

答案 1 :(得分:1)

它们是相同的变量名,但根据调用的方法将包含不同的信息。将这些变量保留在您的方法中。如果它们包含相同的信息,则仅共享它们。

相反,我从两种方法中提取了相似的逻辑,然后将其放入另一种方法中。这更有意义。

Public Class modBuild

    Public Shared Sub GetParameterPAOut()
        Dim PA As IScrSubVarGroup

        PA = Mdl.findElement("$G_PA", False)

        BuildGridView(PA, dgvBuildSubvarPa)
    End Sub

    Public Shared Sub GetParameterMPOut()
        Dim MP As IScrSubVarGroup

        MP = Mdl.findElement("$G_MP", False)

        BuildGridView(MP, dgvBuildSubvarMp)
    End Sub

    Private Shared Sub BuildGridView(ByVal e As IScrSubVarGroup, ByVal dgv As DataGridView)

        Dim SubvarGroups As IScrNamedObjectList
        Dim SubvarGroup As IScrSubVarGroup
        Dim nSubvarGroup As Integer

        Dim Subvars As IScrNamedObjectList
        Dim Subvar As IScrSubVar
        Dim nSubvar As Integer

        Subvars = e.getSubvarList(False)
        nSubvar = Subvars.count
        SubvarGroups = e.getSubvarGroupList(False)
        nSubvarGroup = SubvarGroups.count

        Try
            For i As Integer = 0 To nSubvar - 1
                Subvar = Subvars.item(i)
                If Subvar.intent.val = 0 Then
                    dgv.Rows.Add("Main", Subvar.name)
                End If
            Next  
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Class

看看逻辑,这使我想知道为什么它甚至在这样的类中。您的dgv不应那样公开,而且我认为没有理由将它们共享。但是,我是在对您的程序了解很少的情况下说这句话的。