代码之家  ›  专栏  ›  技术社区  ›  user1608166

在对象初始值设定项中正在初始化的字段或属性的名称必须以“”开头

  •  0
  • user1608166  · 技术社区  · 12 年前

    我正试图将这几行C#转换为Vb长达数小时,但我无法使其工作。

    Friend Shared Function GetErrorCorrectPolynomial(ByVal errorCorrectLength As Integer) As tPolynomial
        Dim a As tPolynomial
    
        a =  New tPolynomial(New DataCache() With {1}, 0)
    
        For i As Integer = 0 To errorCorrectLength - 1
            a = a.Multiply(New tPolynomial(New DataCache() With { 1, tMath.GExp(i) }, 0))
        Next i
    
        Return a
    End Function
    

    我得到这个错误 在对象初始值设定项中正在初始化的字段或属性的名称必须以“”开头

    在本部分中{1}

    原始代码

    internal static tPolynomial GetErrorCorrectPolynomial(int errorCorrectLength)
    {
        tPolynomial a = new tPolynomial(new DataCache() { 1 }, 0);
    
        for (int i = 0; i < errorCorrectLength; i++)
        {
            a = a.Multiply(new tPolynomial(new DataCache() { 1, tMath.GExp(i) }, 0));
        }
    
        return a;
    }
    

    已编辑以添加Datacache类

    Friend Class DataCache
        Inherits List(Of Integer)
    
        Public Sub New(ByVal capacity As Integer)
            MyBase.New()
            For i As Integer = 0 To capacity - 1
                MyBase.Add(0)
            Next i
        End Sub
    
        Public Sub New()
            MyBase.New()
        End Sub
    
    
    End Class
    
    3 回复  |  直到 12 年前
        1
  •  5
  •   Adam Maras    12 年前

    看起来你正试图使用 collection initializer .使用 From 关键字,如下所示:

    New DataCache() From { 1, tMath.GExp(i) }
    
        2
  •  0
  •   JDB    12 年前

    我认不出你用的C#,但VB With 关键字用于设置初始化对象的属性。

    New Foo() With { .Bar = 1 }
    

    其中Foo是类,Bar是属性。

    请参见: http://msdn.microsoft.com/en-us/library/bb385125.aspx

    这与C#初始化对象属性的方式相同,只是C#省略了“ .

    new Foo() { Bar = 1 }
    

    请参见: http://msdn.microsoft.com/en-us/library/bb384062.aspx

        3
  •  0
  •   Rolf Bjarne Kvinge    12 年前

    看起来DataCache和Int32(int/Integer)之间有一个隐式转换,在这种情况下,您应该只删除With关键字:

    a = New tPolynomial(New DataCache() {1}, 0)