类和数组如何初始化? [英] Classes and arrays how to initialize?

查看:83
本文介绍了类和数组如何初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m working on some partial classes but I can’t figure out how to do it. This is my classes:

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类,组织是相同的。

And for the class FormGroup the organization is the same.

我想构建局部类以扩展这些类,因此当我在另一个项目中使用所有这些类时,我只需要处理(参见)最高级的类(Form),而不是其他类(例如Info和FormGroup。这是我想做的事情:

I want to build partial classes to extend these classes, so when I use all this classes in another project I only have to deal with (see) the topmost class (Form) and not the other classes (like Info and FormGroup. This is what I like to do:

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

因为它是带有类的数组。我是在Form and Info类中完成的吗?

because it is arrays with classes. How can I do it in my Form and Info class?

预先感谢。

推荐答案

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

You must first create an array, then loop over the array and assign each element. Also, unless you have a good, strong reason, do this in the constructor rather than a separate init method.

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 类,该类将封装重复的初始化代码。

Alternatively, if you find yourself with a lot of array fields, and they all have default constructors, you could create a FixedCollection class that would encapsulate the repetitive initialization code.

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

编辑:添加了当元素构造函数不足时的构造函数重载。

Added constructor overloads for when an element constructor is not enough.

如果您只能将构造函数与创建者pa一起使用参数,您可以删除 T 上的 New 约束。

If you only use the constructors with a creator parameter, you could remove the New constraint on T.

按以下方式使用重载:

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

End Class

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

Based on your comments, it seems like the Init methods are an unfortunate necessity. If possible, I would recommend that you find a way to get the generated constructor changed to call this method (defined in the generated code using partial methods) for you rather than forcing you to call it yourself.

这篇关于类和数组如何初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆