user8666372
user8666372

Reputation:

Declaring variables global for only class in VB.net

I wonder wwhat's the sufficient method to use variables in class level for class' procedures. I have two procedure located in same class and most of the variables are common for both procedure. How can i declare this 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

especially i'd like to use below variables at the top of the class and outside from the procedures.

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

Upvotes: 0

Views: 83

Answers (2)

the_lotus
the_lotus

Reputation: 12748

They are the same variable name but will contain different information depending on which method you call. Keep those variable inside your method. Only share the variable if they contain the same information.

Instead, I pull out the similar logic from both method and put it in an other method. This makes more sense.

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

Looking at the logic, it makes me wonder why it's even in a class like this. Your dgv shouldn't be public like that and I don't see a reason to have these as shared. But, I'm saying this with very limited knowledge of your program.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460078

If you want to access fields or properties in the class only, declare them as Private

Public Class modBuild
   Private PA As IScrSubVarGroup

If you want to access them from GetParameterPAOut which is Shared, you either need to have an instance of the class modBuild in the methods or make the field(s) also Shared.

Upvotes: 1

Related Questions