Excel 在vba中为用户定义的类型创建属性
我正试图在VBA中创建我的第一个用户定义类型,我被困在这里了。我想在我的类型中添加“area”属性:Excel 在vba中为用户定义的类型创建属性,excel,vba,user-defined-types,Excel,Vba,User Defined Types,我正试图在VBA中创建我的第一个用户定义类型,我被困在这里了。我想在我的类型中添加“area”属性: Public Type My_Node x As Double y As Double w As Double h As Double used As Boolean area As Double area = w * h End Type 为了这样称呼它: Dim node as My_Node Dim surface as doubl
Public Type My_Node
x As Double
y As Double
w As Double
h As Double
used As Boolean
area As Double
area = w * h
End Type
为了这样称呼它:
Dim node as My_Node
Dim surface as double
surface = node.area
我认为这不是很正确,但我找不到如何实现它 谢谢你的评论,这对我理解有很大帮助。 这是我最后一次更新,效果很好:
Public x As Double
Public y As Double
Public w As Double
Public h As Double
Public used As Boolean
Public Property Get Area() As Double
Area = w * h
End Property
是的,我可以在外部制作,但如果我知道如何这样做,对我将来会很有用!
谢谢 谢谢你的评论,这对我理解有很大帮助。 这是我最后一次更新,效果很好:
Public x As Double
Public y As Double
Public w As Double
Public h As Double
Public used As Boolean
Public Property Get Area() As Double
Area = w * h
End Property
是的,我可以在外部制作,但如果我知道如何这样做,对我将来会很有用!
谢谢 对于OPs anwser,您确实需要所有这些公共字段的属性。您可能会认为这是一个没有什么好处的大量样板文本。但它将允许您验证输入。VBA提供的免费且奇妙的Rubberduck插件的重构完全消除了样板文件的单调乏味 只需单击几下封装字段重构,就可以更改OP答案中的代码
Private Type TClass1
X As Double
Y As Double
W As Double
H As Double
Used As Boolean
Surface As Double
End Type
Private this As TClass1
Public Property Get X() As Double
X = this.X
End Property
Public Property Let X(ByVal RHS As Double)
this.X = RHS
End Property
Public Property Get Y() As Double
Y = this.Y
End Property
Public Property Let Y(ByVal RHS As Double)
this.Y = RHS
End Property
Public Property Get W() As Double
W = this.W
End Property
Public Property Let W(ByVal RHS As Double)
this.W = RHS
End Property
Public Property Get H() As Double
H = this.H
End Property
Public Property Let H(ByVal RHS As Double)
this.H = RHS
End Property
Public Property Get Used() As Boolean
Used = this.Used
End Property
Public Property Let Used(ByVal RHS As Boolean)
this.Used = RHS
End Property
Public Property Get Surface() As Double
Surface = this.Surface
End Property
Public Property Let Surface(ByVal RHS As Double)
this.Surface = RHS
End Property
Public Property Get Area() As Integer
Area = W * H
End Property
对于OPs anwser,您确实需要所有这些公共字段的属性。您可能会认为这是一个没有什么好处的大量样板文本。但它将允许您验证输入。VBA提供的免费且奇妙的Rubberduck插件的重构完全消除了样板文件的单调乏味 只需单击几下封装字段重构,就可以更改OP答案中的代码
Private Type TClass1
X As Double
Y As Double
W As Double
H As Double
Used As Boolean
Surface As Double
End Type
Private this As TClass1
Public Property Get X() As Double
X = this.X
End Property
Public Property Let X(ByVal RHS As Double)
this.X = RHS
End Property
Public Property Get Y() As Double
Y = this.Y
End Property
Public Property Let Y(ByVal RHS As Double)
this.Y = RHS
End Property
Public Property Get W() As Double
W = this.W
End Property
Public Property Let W(ByVal RHS As Double)
this.W = RHS
End Property
Public Property Get H() As Double
H = this.H
End Property
Public Property Let H(ByVal RHS As Double)
this.H = RHS
End Property
Public Property Get Used() As Boolean
Used = this.Used
End Property
Public Property Let Used(ByVal RHS As Boolean)
this.Used = RHS
End Property
Public Property Get Surface() As Double
Surface = this.Surface
End Property
Public Property Let Surface(ByVal RHS As Double)
this.Surface = RHS
End Property
Public Property Get Area() As Integer
Area = W * H
End Property
UDT只能存储数据,不能操作数据。您正在寻找一个类类型:您必须在外部代码中计算
area
。例如node.area=node.w*node.h:surface=node.area
。或者,作为@AlexK。写时,使用更灵活的类对象。UDT只能存储数据,不能操作数据。您正在寻找一个类类型:您必须在外部代码中计算area
。例如node.area=node.w*node.h:surface=node.area
。或者,作为@AlexK。写,使用更灵活的类对象。对不起,我没有真正得到附加值,你有更实际的例子让我理解@freeflow吗?如果宽度或高度为负值会发生什么。如果负值将导致错误的结果,那么属性let方法可能包括检查负值并发出警告。RuberDuck允许在单击按钮时将字段转换为属性,而不是键入代码。它不是全部或全部,您可以选择要转换的字段。鉴于您只是在VBA中发现“属性”和类,您可能还没有完全理解属性与公共字段相比所带来的好处。谢谢您的解释!我会努力的,创建我们自己的类是非常有趣的,看起来非常强大(即使我现在没有得到所有的分数)!对不起,我没有真正得到附加值,你有更实际的例子让我理解@freeflow吗?如果宽度或高度为负值会发生什么。如果负值将导致错误的结果,那么属性let方法可能包括检查负值并发出警告。RuberDuck允许在单击按钮时将字段转换为属性,而不是键入代码。它不是全部或全部,您可以选择要转换的字段。鉴于您只是在VBA中发现“属性”和类,您可能还没有完全理解属性与公共字段相比所带来的好处。谢谢您的解释!我会努力的,创建我们自己的类是非常有趣的,看起来非常强大(即使我现在没有得到所有的分数)!