首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在类模块中使用公共变量?

如何在类模块中使用公共变量?
EN

Stack Overflow用户
提问于 2017-11-27 03:30:02
回答 2查看 1K关注 0票数 0

我使用一个类模块来使一个保存按钮集合都做同样的事情。但是,当我试图让它们运行一个需要变量的子程序时,我无法将变量传递给它们。

使用@Teasel关于属性的建议进行编辑。问题似乎在于,Let属性不允许我从Module1中设置变量。

Class1

代码语言:javascript
复制
Public WithEvents SaveBtn As MSForms.CommandButton

Dim currentrow As Long

Private Sub SaveBtn_Click()
     SendMessage
     `Even if I just have it Msgbox currentrow it returns 0
End Sub

Property Let GetRow(myrow As Long)
    currentrow = myrow
End Property

Property Get GetRow() As Long
    GetRow = currentrow
End Property

Module1

代码语言:javascript
复制
`Trying to send the value into the Class using Let
Private Sub SendRow_Click()
Module1.GetRow = 22
End Sub

`Trying to Get the value back from the Class
Public Sub SendMessage()
    Dim therow As Long
    therow = Module1.GetRow
    `I get the "Method or Data Member not found" error in the line above
    MsgBox therow
End Sub

UserForm1

代码语言:javascript
复制
`This part works fine
Dim colSaveButtons As New Collection

Private Sub UserForm_Initialize()

Dim i As Long
Dim ctl As MSForms.Control
Dim obEvents As Class1

For Each ctl In Me.Controls
    If TypeOf ctl Is MSForms.CommandButton Then
        For i = 0 To 5
            If ctl.Name = "btnSavePage" & i Then
                Set obEvents = New Class1
                Set obEvents.SaveBtn = ctl
                colSaveButtons.Add obEvents
            End If
        Next
    End If
Next ctl
End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-27 23:58:25

在类模块中添加"CurrentRow“字段:

代码语言:javascript
复制
Public WithEvents SaveBtn As MSForms.CommandButton
Public CurrentRow As Long '<< add this

Private Sub SaveBtn_Click()
     SendMessage CurrentRow 
End Sub

在你的循环中:

代码语言:javascript
复制
...
If ctl.Name = "btnSavePage" & i Then
    Set obEvents = New Class1
    obEvents.CurrentRow = 10 'or whatever...
    Set obEvents.SaveBtn = ctl
    colSaveButtons.Add obEvents
End If
...

还有你的SendMessage方法:

代码语言:javascript
复制
Public Sub SendMessage(CurrentRow As Long)
     MsgBox "This works"
End Sub
票数 1
EN

Stack Overflow用户

发布于 2017-11-27 06:33:22

您可以使用两种不同的方法来实现这一点。

1.公共财产

要简单地访问变量的值,您需要一个Get属性,而要设置它的值,则需要一个Let属性。

在你的舱里:

代码语言:javascript
复制
'Your module private variable
Dim nameOfYourModuleVariable As String

...

'Set property to assign a value to your variable
Public Property Let nameOfYourProperty(value As String)
    nameOfYourModuleVariable = value
End Property

'Get property to return the value of your variable
Public Property Get nameOfYourProperty() As String
    nameOfYourProperty = nameOfYourModuleVariable
End Property

然后,您可以这样使用它:

代码语言:javascript
复制
'Set the value
MyModule.nameOfYourProperty = "foo"

'Get the value
MyModule.nameOfYourProperty

我强烈建议使用属性来做这些事情,但是也可以简单地将变量设置为public,如第2点所示。

2.公共变量

将变量定义为公共,以便可以从任何地方访问它。

在你的舱里:

代码语言:javascript
复制
Public nameOfYourVariable As String

从另一个模块获取或设置值:

代码语言:javascript
复制
'Set the value
MyModule.nameOfYourVariable = "foo"

'Get the value
MyModule.nameOfYourVariable
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47503558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档