VB.NET中只读集合属性的集合初始值设定项?

时间:2011-07-25 14:34:12

标签: .net vb.net collections syntax properties

我正在尝试在我的框架上支持Basic.NET,所以我试图将C#4代码转换为Basic.NET 10.微软致力于“共同发展”这两个但我遇到了问题集合初始化......

我发现我可以像在C#中一样初始化一个集合:

Dim list = New List(Of Int32) From {1, 2, 3, 4, 5, 6, 7, 8, 9}

大! 但是,在初始化只读集合属性时,这不起作用。例如,如果我有这个类:

Public Class Class1

  Private ReadOnly list = New List(Of Int32)

  Public ReadOnly Property ListProp() As List(Of Int32)
    Get
      Return list
    End Get
  End Property

End Class

我无法以这种方式初始化它:

Dim class1 = New Class1 With {.ListProp = New List(Of Int32) From {1, 2, 3, 4, 5, 6, 7, 8, 9}}

或者这样:

Dim class1 = New Class1 With {.ListProp = {1, 2, 3, 4, 5, 6, 7, 8, 9}}

我得到一个“Property'ListProp'是'ReadOnly'。”消息是正确的但是,它说here在Basic.NET中支持集合初始值设定项,其中自动调用Add方法。我错过了什么或不支持属性吗? C#4支持这个......

提前致谢, aalmada

编辑:

以下是等效的可编译C#代码供参考:

using System;
using System.Collections.Generic;

namespace ConsoleApplication3
{
    class Class1
    {
        private readonly List<Int32> list = new List<Int32>();

        public List<Int32> ListProp
        {
            get
            {
                return this.list;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // a collection initialization
            var list = new List<Int32> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            // a read-only collection property initialization
            var class1 = new Class1
            {
                ListProp = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
            };
        }
    }
}

6 个答案:

答案 0 :(得分:1)

您正尝试将ListProp属性设置为新的List(Of Int32)实例。

由于它是ReadOnly,你不能这样做。

答案 1 :(得分:1)

您正尝试分配到ListProp属性对象。您只能在类构造函数中修改只读字段 从另一方面来说,您可以在任何地方修改只读列表的元素。

答案 2 :(得分:1)

编辑:根据Meta-Knight的评论,这不编译。但是,我会把它留在教育目的。如果真的不支持它会让我感到惊讶,但是如果不花费更多的时间来解决它,我就无法弄清楚VB语法。


好吧,我会尝试等同于C#代码,即

' No idea whether this would work or not, but worth a try
Dim class1 = New Class1 With {.ListProp = From {1, 2, 3, 4, 5, 6, 7, 8, 9}}

现在缺少New List(Of Int32) - 就像在C#中你会写的那样:

var class1 = new Class1 { ListProp = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } };

尝试在C#代码中包含new List<int>会以同样的方式失败,因此请尝试将其从VB版本中删除...

答案 3 :(得分:0)

除非类接受构造函数参数并将其分配给private list变量,否则无法直接初始化readonly属性。

答案 4 :(得分:0)

这里的问题是collection属性标记为ReadOnly,而不是初始值设定项。任何时候,赋值运算符的左侧都不会出现只读属性。

作为一种变通方法,您可以将集合作为参数传递给构造函数,并将其作为ReadOnlyCollection<T>System.Collections.ObjectModel.ReadOnlyCollection<T>)公开。

答案 5 :(得分:0)

你能保持简单吗?像这样?

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim list = New List(Of Int32) From {1, 2, 3, 4, 5, 6, 7, 8, 9}
    Dim class1x = New Class1(New List(Of Int32) From {1, 2, 3, 4, 5, 6, 7, 8, 9})
    Dim class1y = New Class1(list)
    Dim list2 = New List(Of Int32)
    list2 = class1x.ListProp
    list2 = class1y.ListProp
End Sub
End Class
Public Class Class1
Sub New(l As List(Of Int32))
    list = l
End Sub

Private ReadOnly list = New List(Of Int32)

Public ReadOnly Property ListProp() As List(Of Int32)
    Get
        Return list
    End Get
End Property

End Class