填充一个类,然后再使用它

时间:2014-06-04 22:16:19

标签: vb.net class variables

我有一个像

这样的课程
Public Class Location
    Public Name As String
    Public Column As Integer
    Public Row As Integer
    Public Occupant As String
End Class

在我的代码中,我有一个子程序来填充类

Sub Populate Location(ByValue coordinate As String)
    Dim Here As New Location
    Here.Location = coordinate.SubString(0,3)
    Here.Column = SomeFunction(coordinate, Gere.location)
    Here.Row = AnotherFunction(coordinate, Here.Column)
    Here.Occupant = ArrayOfOccupant(column, row)
End Sub

只需按一下按钮即可填写所有内容。 后来我想点击另一个按钮并使用Here类做其他事情。

我有哪些选择或我要搜索什么?

1 个答案:

答案 0 :(得分:4)

原样,您的类对象只存在于该过程中。

Dim Here As New Location    ' variable declaration, instancing
                            ' Scope is this module or form

Sub PopulateLocation(ByValue coordinate As String)
    ' assuming this might get reused, create a new instance
    Here = New Location            

    Here.Location = coordinate.SubString(0,3)
    Here.Column = SomeFunction(coordinate, Here.location)
    Here.Row = AnotherFunction(coordinate, Here.Column)
    Here.Occupant = ArrayOfOccupant(column, row)
End Sub

该模块中的其他潜在客户可以访问Here,因为它现在具有类/模块级Scope