【发布时间】:2023-03-17 16:22:01
【问题描述】:
这里是另一个难过的新手,试图解决一个问题。
我正在尝试使用具有属性获取和设置的类模块,而不是公共变量。 我想在带有文本框的用户窗体中设置这些值,并在输入时更新用户窗体中的列表框,最好是在文本框事件_afterupdate上, 当用户点击保存按钮时,我希望将配置文件属性存储到一个范围内。 毫无疑问,这将是一个可笑的烂摊子,但我已经被难住了好几天了,我要带着帽子来到这里。我就是想不通。
类 Mod 示例:
Private mProfileName As String
Private mStartDate As Date
Private mEndDate As Date
Private mOngoing As Boolean
Sub Class_Initialise()
'Set default values for properties
mLastName = "Enter Last Name"
mStartDate = "Enter Date"
mEndDate = Date
mOngoing = True
End Sub
'********************************
'The relevant property procedures:
'********************************
Property Get ProfileName() As String
ProfileName = mProfileName
End Property
Property Let ProfileName(Value As String)
mProfileName = Value
End Property
Property Get EndDate() As Date
EndDate = mEndDate
End Property
Property Let EndDate (Value As Date)
mEndDate = Value
End Property
Property Get Ongoing() As Boolean
Ongoing = mOngoing
End Property
Property Let Ongoing(Value As Boolean)
If mEndDate = Date Then
mOngoing = True
End If
End Property
在我目前拥有的用户表单中:
Option Explicit
'hopefully not needed:
'Private mTextBoxUpdated As Boolean
'Private mListBoxUpdated As Boolean
'Private mEnteredText As String
'Private mIndexText As String
Private DictThisForm As Dictionary
Private ProfileData As clsProfileData
Private Sub UserForm_Initialize()
Debug.Print "UserForm Intialised"
Set DictThisForm = New Dictionary
Debug.Print "DictThisForm Created"
Set ProfileData = New clsProfileData
Call UserForm_UpdateListBox
还有这个可笑的烂摊子:(甚至还没有接近工作)
Sub UserForm_UpdateListBox()
With lbxListBox1
.Clear
.ColumnCount = 2
.AddItem
'.List(0, 1) = "Profile Name",ProfileData.ProfileName '
'another attempt
'the below throws a Type Mismatch Error
.AddItem ProfileData.ProfileName, "Profile Name"
.AddItem ProfileData.StartDate, "Start Date"
.AddItem ProfileData.EndDate, "End Date"
.AddItem ProfileData.OnGoing, "Ongoing?"
And(带有私有声明的模块级变量)
Private Sub tbxProfileName_AfterUpdate()
ProfileData.FirstName = tbxProfileName.Text
enter code here
Call UserForm_UpdateListBox
End Sub
目前我只是在尝试测试类模块,看看我是否可以将属性值放入变量中,或者更好的是,将这些属性值放入隐藏页面上的范围中,可能是通过字典,并且从那里更新列表?将属性值放入列表框似乎很麻烦......
Sub testClassProfile()
Dim getPropertyAsStringVar As String
Dim NewProfile As clsProfileData
Set NewProfile = New clsProfileData
getPropertyAsStringVar = NewProfile.FirstName
Debug.Print getPropertyAsStringVar
End Sub
当前 Debug.Print 输出为“”,即 zip。没有默认值。
非常感谢任何建议。 如果我一次问太多,请告诉我,我会尝试将问题的范围缩小到我当前的问题,我认为上下文可能会有所帮助......
【问题讨论】:
-
发布一个完整(但可能更小)的示例类可能会更好 - 即。一些我们可以运行的代码。只需要完整的课程和最后一个测试子。
-
欢迎来到 StackOverflow。作为猜测,这可能是一个实例问题。在您的
Sub TestClassProfile中,您创建一个clsProfileData的实例。但我怀疑你没有将它传递给你要填充的用户窗体。在那里,您正在创建该类的单独实例。尝试将外部创建的实例传递给用户表单。 -
嘿伙计们,感谢您的意见...@TimWilliams 如果将上面的两个粘贴到一个类 mod 中,它们应该运行吗?我应该把它们做成一个单元吗?
-
@ainwood 我实际上是在尝试尽可能多地从另一个模块运行用户窗体,这只是暂时的,看看我是否可以将属性值添加到列表框中,但我没有t认为代码甚至是正确的,或者列表框不接受这样的类属性值?但是我对如何传递“clsProfileData”的实例非常感兴趣——不过,这掩盖了我的无知,我认为一个类实例及其属性最终将是全局可用的?但我调暗新的部分原因也是为了让我可以访问智能感知......
-
自定义类不会“自动实例化”——您需要先创建一个实例才能使用它。没有仅通过存在类模块而创建的全局可访问实例。可能值得略读其中涵盖在 VBA 中使用自定义类的众多指南。
标签: vba class variables userform listboxitem