gpt4 book ai didi

excel - 具有属性的 VBA 类

转载 作者:行者123 更新时间:2023-12-02 21:30:53 25 4
gpt4 key购买 nike

我正在尝试构建一个类,该类基本上描述了一个地理空间条件语句,其中参数之一是海拔。 Altitude 有 4 个属性,例如 Min、Max、Units、Invert,我正在尝试使该类支持 2 层属性。

IE

Dim blah as qClass
Set blah = New qClass

blah.Altitude.Min = 100

这就是我正在寻找的效果,但我很难弄清楚如何实现它。 (有中等VBA经验,但第一次上课)

我的解决方案:我创建了一个通用类,其中包含我将用于每个参数的 Min/Max/Unit/Invert 参数,这样只需设置一次即可重复使用。

设置类

Private pMin As Integer
Private pMax As Integer
Private pUnit As String
Private pInvert As Boolean

Public Property Get Min() As Integer
Min = pMin
End Property

Public Property Get Max() As Integer
Max = pMax
End Property

Public Property Get Unit() As String
Unit = pUnit
End Property

Public Property Get Invert() As Boolean
Invert = pInvert
End Property

Public Property Let Min(Value As Integer)
pMin = Value
End Property

Public Property Let Max(Value As Integer)
pMax = Value
End Property

Public Property Let Unit(Value As String)
pUnit = Value
End Property

Public Property Let Invert(Value As Boolean)
pInvert = Value
End Property

父类设置使用

'CPA Range Property
Public Property Get CPARange() As cSettings
If pCPARange Is Nothing Then Set pCPARange = New cSettings
Set CPARange = pCPARange
End Property
Public Property Set CPARange(Value As cSettings)
Set pCPARange = Value
End Property

最佳答案

如果你想这样做:

Public Sub Test()
Dim a As New aClass
a.InitializeFromValues 40, 150

Dim q As New qClass
q.Altitude = a
' q.Altitude.Min = 40
' q.Altitude.Max = 150

Dim z As New qClass
z.Altitude.Max = 100
' z.Altitude.Min = 0 (default)
' z.Altitude.Max = 100
End Sub

您需要两个类(class)。我称它们为 qClassaClass

=== aClass 定义 ===

Private m_min As Double
Private m_max As Double

Private Sub Class_Initialize()
m_min = 0#
m_max = 0#
End Sub

Public Sub InitializeFromValues(ByVal Min As Double, ByVal Max As Double)
m_min = Min
m_max = Max
End Sub

Public Property Get Min() As Double
Min = m_min
End Property

Public Property Let Min(ByVal X As Double)
m_min = X
End Property

Public Property Get Max() As Double
Max = m_max
End Property

Public Property Let Max(ByVal X As Double)
m_max = X
End Property

=== qClass 定义===

Private m_alt As aClass

Private Sub Class_Initialize()
Set m_alt = New aClass
End Sub

Public Sub InitializeFromValues(ByVal alt As aClass)
Set m_alt = alt
End Sub

Private Sub Class_Terminate()
Set m_alt = Nothing
End Sub

Public Property Get Altitude() As aClass
Set Altitude = m_alt
End Property

Public Property Set Altitude(ByVal X As aClass)
Set m_alt = X
End Property

关于excel - 具有属性的 VBA 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37347686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com