Vb.net 使用属性初始化,该属性是子类中的项列表

Vb.net 使用属性初始化,该属性是子类中的项列表,vb.net,class,Vb.net,Class,我想要一个客户类,带有文本属性名。 另一个属性CustAddress将是多个地址的列表。 每个地址都有两个字符串属性 这是我的。 我不确定是否需要类地址的构造函数中的某些内容。 我甚至不确定开发这个类的代码会是什么样子 此外,我无法让F11单步调试功能单步执行类代码。如果我在类代码中加了一个中断,它确实会中断,并且工作正常。我修改了“仅我的代码”选项以删除复选框,但它没有帮助。我有一个包含一个类模块和一个Windows应用程序的解决方案 <ComClass(ComClass1.ClassI

我想要一个客户类,带有文本属性名。 另一个属性CustAddress将是多个地址的列表。 每个地址都有两个字符串属性

这是我的。 我不确定是否需要类地址的构造函数中的某些内容。 我甚至不确定开发这个类的代码会是什么样子

此外,我无法让F11单步调试功能单步执行类代码。如果我在类代码中加了一个中断,它确实会中断,并且工作正常。我修改了“仅我的代码”选项以删除复选框,但它没有帮助。我有一个包含一个类模块和一个Windows应用程序的解决方案

<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)>
Public Class ComClass1
#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "c8e723b4-f229-4368-9737-97c4c71d490a"
    Public Const InterfaceId As String = "16275ddb-5cfe-47c0-995f-84a5f868ad1b"
    Public Const EventsId As String = "dad73a5c-8ac4-4384-a5f9-8e2c388b5514"
#End Region
    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.

    'Fields  
    Private _name As String
    Public _CustAddress As List(Of address)

    'Constructor for class ComClass
    Public Sub New()
        _CustAddress = New List(Of address)
    End Sub

    Public Property CustName() As String
        Get
            Return _name
        End Get
        Set(ByVal Value As String)
            _name = Value
        End Set
    End Property

    Public Property CustAddress() As List(Of address)
        Get
            Return _CustAddress
        End Get
        Set(value As List(Of address))
            _CustAddress = value
        End Set
    End Property

    Public Class address

        Private _address1 As String
        Private _address2 As String

        Public Sub New()
            '??????
        End Sub

        Public Property Address1 As String
            Get
                Return _address1
            End Get
            Set(value As String)
                _address1 = value
            End Set
        End Property

        Public Property Address2 As String
            Get
                Return _address2
            End Get
            Set(value As String)
                _address2 = value
            End Set
        End Property
    End Class

End Class

公共类通信类1
#区域“COM GUID”
'这些GUID提供此类的COM标识
'及其COM接口。如果您更改它们,则现有
'客户端将无法再访问该类。
Public Const ClassId为String=“c8e723b4-f229-4368-9737-97c4c71d490a”
Public Const interface id As String=“16275ddb-5cfe-47c0-995f-84a5f868ad1b”
Public Const EventsId As String=“dad73a5c-8ac4-4384-a5f9-8e2c388b5514”
#末端区域
'可创建的COM类必须有一个Public Sub New()
'如果没有参数,则该类将不会
'已在COM注册表中注册,无法创建
'通过CreateObject。
“田地
Private\u名称作为字符串
公共地址(地址列表)
'类ComClass的构造函数
公共分新()
_CustAddress=新列表(地址)
端接头
公共属性CustName()作为字符串
得到
返回\u名称
结束
设置(ByVal值作为字符串)
_名称=值
端集
端属性
公共财产CustAddress()作为(地址的)列表
得到
返回地址
结束
设置(值为列表(地址))
_CustAddress=值
端集
端属性
公共课堂演讲
私有地址1作为字符串
私有地址2作为字符串
公共分新()
'??????
端接头
作为字符串的公共属性Address1
得到
返回地址1
结束
设置(值为字符串)
_地址1=值
端集
端属性
公共属性Address2作为字符串
得到
返回地址2
结束
设置(值为字符串)
_地址2=值
端集
端属性
末级
末级

我拿出com文件只是为了缩短答案。因为在属性过程中没有额外的代码,所以我将其缩短为自动属性。我还自行将address类移出。这个类在程序中的其他地方可能很有用,因此嵌套类不是必需的

Public Class ComClass1
    Public Property CustName As String
    Public Property CustAddress As List(Of address)
    Public Sub New(cName As String, cAddresses As List(Of address))
        CustName = cName
        CustAddress = cAddresses
    End Sub
End Class

Public Class address
    Public Property Address1 As String 'Street Address
    Public Property Address2 As String 'City and State
    Public Sub New(a1 As String, a2 As String)
        Address1 = a1
        Address2 = a2
    End Sub
End Class

Private Sub DeclareAComClass1()
    Dim addrList As New List(Of address) From {
        New address("12 Main Street", "Los Angeles, CA"),
        New address("13 Park Avenue", "New York, NY")
    }
    Dim cc As New ComClass1("Big Company, Inc.", addrList)
End Sub

这就是我的结局@玛丽让我更领先了。但是因为我使用的是COM类,所以不能有任何带参数的公共构造函数

我添加了一个名为AddAddress的方法,它为我提供了所需的功能。 在我最初的帖子中,我不知何故遗漏了COM类所需的MyBase.New

我鼓励对这种方法发表见解

<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)>
Public Class ComClass1
#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "c8e723b4-f229-4368-9737-97c4c71d490a"
    Public Const InterfaceId As String = "16275ddb-5cfe-47c0-995f-84a5f868ad1b"
    Public Const EventsId As String = "dad73a5c-8ac4-4384-a5f9-8e2c388b5514"
#End Region
    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.

    'Fields  
    Private _name As String
    Private _CustAddress As List(Of address)

    'Constructor for class ComClass
    Public Sub New()
        MyBase.New
        _CustAddress = New List(Of address)
    End Sub

    Public Sub AddAddress(a1 As String, a2 As String)
        Dim addr As New address(a1, a2)
        _CustAddress.Add(addr)
    End Sub

    Public Property CustName() As String
        Get
            Return _name
        End Get
        Set(ByVal Value As String)
            _name = Value
        End Set
    End Property

    Public Property CustAddress() As List(Of address)
        Get
            Return _CustAddress
        End Get
        Set(value As List(Of address))
            _CustAddress = value
        End Set
    End Property

    Public Class address

        Private _address1 As String
        Private _address2 As String

        Public Sub New(a1 As String, a2 As String)
            _address1 = a1
            _address2 = a2
        End Sub

        Public Property Address1 As String
            Get
                Return _address1
            End Get
            Set(value As String)
                _address1 = value
            End Set
        End Property

        Public Property Address2 As String
            Get
                Return _address2
            End Get
            Set(value As String)
                _address2 = value
            End Set
        End Property
    End Class

End Class

您没有指定问题。你能解释得更清楚些吗?你想要地址类构造函数的代码吗?为什么你认为你可能需要
address
构造函数中的代码?你认为这样的代码可以实现什么,而你当前的代码却没有?为什么公共属性CustAddress的backer字段(_CustAddress)也是公共的?是的,我的帖子似乎缺少一个特定的问题。这个类是否允许我实例化一个customer对象并将多个地址关联到该对象。Mary,。我仍然可以将其用作com类吗?我读到com类不支持构造函数上的参数。我需要从VBA使用这个类。我同意一个独立的地址类会很有用,但在这种情况下,我试图理解子类。另外,关于com类和不支持带有参数的构造函数的文本让我认为,拥有一个未公开的子类意味着我可以在我的子类中使用参数化构造函数,并且仍然将主类用作com类。太好了!我在构造函数(Sub-New)中看到的唯一一件事就是它。将参数分配给公共属性,而不是私有后台字段(以下划线开头的字段)。这是一个很好的做法,因为属性过程中可能有代码。你比我更了解COM。很遗憾你不能使用自动属性。我猜你使用的是旧版本的VS。
Dim TestClass As New ComClass1
        Dim myint As Int32

        TestClass.CustName = "John Smith"
        TestClass.AddAddress("123 Main Street", "Los Angeles")

        TestClass.AddAddress("13 Park Avenue", "New York")

        Debug.Print(TestClass.CustAddress(0).Address1)   '123 Main Stree'
        Debug.Print(TestClass.CustAddress(1).Address1)   '13 Park Avenue

        TestClass.CustAddress.Remove(TestClass.CustAddress(0))

        Debug.Print(TestClass.CustAddress(0).Address1)    ' 13 Park Avenue