【问题标题】:Classes and arrays how to initialize?类和数组如何初始化?
【发布时间】:2011-11-01 10:49:41
【问题描述】:

我正在做一些部分课程,但我不知道该怎么做。 这是我的课:

Partial Public Class Form
    Private InfoField() As Info

    Private FormgroupField() As FormGroup

    Private tittle As String


    Public Property Info() As Info()
    Get
        Return Me. InfoField
    End Get
    Set
        Me. InfoField = value
    End Set
    End Property

Public Property FormGroup() As FormGroup()
    Get
        Return Me.GromGroupField
    End Get
    Set
        Me.FormGroupField = value
    End Set
    End Property

   Public Property tittle() As String
    Get
        Return Me.tittleField
    End Get
    Set
        Me.tittleField = value
    End Set
 End Property
End class



Partial Public Class Info
      Private ChangeFormField() As ChangeForm

    Private formYearField() As FormYea

    Private idField As String

    Public Property ChangeForm() As ChangeForm()
    Get
        Return Me.changeFormField
    End Get
    Set
        Me.changeFormField = value
    End Set
    End Property

   Public Property FormYear() As FormYear()
    Get
        Return Me.formYearField
    End Get
    Set
        Me.formYearField = value
    End Set
    End Property

   Public Property id() As String
    Get
        Return Me.idField
    End Get
    Set
        Me.idField = value
    End Set
    End Property    

  End Class

 Partial Public Class ChangeForm
Private idField As String

    Private valueField As String

<properties goes here>
End Class

Partial Public Class FormYear

    Private idField As String

    Private valueField As String

<properties goes here>
 End Class

对于 FormGroup 类,组织是相同的。

我想构建部分类来扩展这些类,所以当我在另一个项目中使用所有这些类时,我只需要处理(查看)最顶层的类(Form)而不是其他类(如 Info 和 FormGroup.这就是我喜欢做的事情:

Partial Public Class Form
 Public Sub Init()
    Me.Info = New Info
    Me.FormGroup = New FormGroup

    Me.Info.Init()
    Me.FormGroup.Init()
End Sub

End Class

Partial Public Class Info
 Public Sub Init()
    Me.FormYear = New FormYear
    Me.ChangeForm = New ChangeForm

    Me.changeForm.Init()
End Sub

但我不会写

Me.Info = New Info
Me.FormGroup = New FormGroup

因为它是带有类的数组。如何在我的表单和信息类中做到这一点?

提前致谢。

【问题讨论】:

    标签: vb.net class partial-classes


    【解决方案1】:

    您必须首先创建一个数组,然后遍历该数组并分配每个元素。此外,除非您有充分的理由,否则请在构造函数中执行此操作,而不是单独的 init 方法。

    Public Class Form
        Public Sub New()
            'In VB, you give the max index, not the length.
            'I prefer listing this as (whatever I want for length) - 1
            Me.Info = New Info(size - 1) {}
            For i = 0 to size - 1
                Me.Info(i) = New Info()
            Next
            'similarly for other fields
        End Sub
    End Class
    

    或者,如果您发现自己有很多数组字段,并且它们都有默认构造函数,您可以创建一个 FixedCollection 类来封装重复的初始化代码。

    Public Class FixedCollection(Of T As New)
        Inherits Collection(Of T)
    
        Public Sub New(ByVal size As Integer)
            MyBase.New(New T(size - 1) {})
            For i = 0 to size - 1
                Me.Items(i) = New T()
            Next
        End Sub
    
        'alternate constructors if you need additional initialization
        'beyond construction of each element
        Public Sub New(ByVal size As Integer, ByVal creator As Func(Of T))
            MyBase.New(New T(size - 1) {})
            If creator Is Nothing Then Throw New ArgumentNullException("creator")
            For i = 0 to size - 1
                Me.Items(i) = creator()
            Next
        End Sub
        'this overload allows you to include the index in the collection
        'if it would matter to creation
        Public Sub New(ByVal size As Integer, ByVal creator As Func(Of Integer, T))
            MyBase.New(New T(size - 1) {})
            If creator Is Nothing Then Throw New ArgumentNullException("creator")
            For i = 0 to size - 1
                Me.Items(i) = creator(i)
            Next
        End Sub
    
        'other collection overrides as needed here
    
    End Class
    

    编辑:为元素构造函数不够用时添加了构造函数重载。

    如果您只使用带有 creator 参数的构造函数,您可以移除 T 上的 New 约束。

    按如下方式使用重载:

    Public Class Form
        Private InfoField As New FixedCollection(Of Info)(10,  
                    Function()
                        Dim ret As New Info()
                        ret.Init()
                    End Function)
    
    End Class
    

    根据您的 cmets,Init 方法似乎是一种不幸的必需品。如果可能的话,我建议您找到一种方法来更改生成的构造函数以调用此方法(使用部分方法在生成的代码中定义),而不是强迫您自己调用它。

    【讨论】:

    • 谢谢!但是,当我使用部分类时,如何在构造函数中执行此操作?当我的“原始”类中存在构造函数时,我认为我的部分类中不能有构造函数?或者这是错的?
    • @Liss 不,您不能在两个不同的地方定义相同的方法。如果分部类的另一部分创建了一个默认构造函数,那么您将无法将其设置为 Init 方法。我遇到的任何部分类都没有阻止我创建默认构造函数。您应该避免使用部分类,除非其中一个部分是由工具(如 WinForm/WPF 设计器)生成的,因为它们将代码拆分并没有太多好处。使用FixedCollection,可以让您初始化内联(例如:Private InfoField As New FixedCollection(Of Info))并完全避免构造函数问题。
    • 谢谢!是的,我的“原始”部分类是自动生成的并且有一个构造函数,我无法更改这个自动生成的类。那么使用部分类来扩展“原始”类对我来说是不是最好的选择?
    • @Liss 在这种情况下,我倾向于使用 FixedCollection,而不是数组,因为它可以保存初始化逻辑。如果调用集合元素的构造函数还不够,您可以更改集合构造函数以获取元素创建函数(请参阅编辑后的答案)。
    • 非常感谢您的宝贵时间!
    【解决方案2】:

    你可以像这样初始化一个类的数组:

    Public FieldTypes As FieldTypeInfo() =  
        {  
        New FieldTypeInfo("Byte", 1),  
        New FieldTypeInfo("Int16", 2),  
        New FieldTypeInfo("Int32", 3),  
        New FieldTypeInfo("Integer", 3),  
        New FieldTypeInfo("Int64", 4),  
        New FieldTypeInfo("UInt16", 5),  
        New FieldTypeInfo("UInt32", 6),  
        New FieldTypeInfo("UInteger", 6),  
        New FieldTypeInfo("UInt64", 7)  
    }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      相关资源
      最近更新 更多