我是否可以传递包含对象的属性而无需处理每个属性?

时间:2017-12-01 15:45:14

标签: .net class properties

以这些课程为例:

Public Class ClsA
    Public Property l As Integer
    Public Property w As Integer
    Public Property h As Integer
End Class


Public Class ClsB

    Private Property myClsA As ClsA

    Public Property l As Integer
        Get
            Return myClsA.l
        End Get
        Set(value As Integer)
            myClsA.l = value
        End Set
    End Property

    Public Property w As Integer
        Get
            Return myClsA.w
        End Get
        Set(value As Integer)
            myClsA.w = value
        End Set
    End Property

    Public Property h As Integer
        Get
            Return myClsA.h
        End Get
        Set(value As Integer)
            myClsA.h = value
        End Set
    End Property

    Public Property someOtherProperty As Integer
End Class

这意味着不是像以下那样访问“l”:

myClsB.myClsA.l

我可以简单地

myClsB.l

这就是我想要的,但我必须单独传递每个属性,因此随着ClsA的公共属性数量的增加,它的工作量就越多。

有没有办法传递包含对象的所有属性(好像它们是容器对象拥有的属性),而不必单独处理每个属性?

1 个答案:

答案 0 :(得分:1)

Inherit From a Base Class

我认为获得你所追求的东西的唯一方法就是做一些继承。见上面的链接。

Public Class ClsA
    Public Property l As Integer
    Public Property w As Integer
    Public Property h As Integer
End Class


Public Class ClsB
    Inherits ClsA
End Class

用法:

Dim b As ClsB = New ClsB()
b.l = 1
相关问题