user1608166
user1608166

Reputation: 3

Name of field or property being initialized in an object initializer must start with '.'

I'm trying to convert this couple of lines of C# to Vb for hours and i can't make it work.

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

i get this error Name of field or property being initialized in an object initializer must start with '.'

in this part {1}

The original code

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;
}

Edited to add the Datacache class

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

Upvotes: 0

Views: 2159

Answers (3)

Adam Maras
Adam Maras

Reputation: 26863

Looks like you're trying to use a collection initializer. Use the From keyword, like this:

New DataCache() From { 1, tMath.GExp(i) }

Upvotes: 5

Rolf Bjarne Kvinge
Rolf Bjarne Kvinge

Reputation: 19345

It looks like there is an implicit conversion between DataCache and Int32 (int/Integer), in which case you should just remove the With keyword:

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

Upvotes: 0

JDB
JDB

Reputation: 25855

I don't recognize the C# you are using, but the VB With keyword is used to set properties of the initialized object.

New Foo() With { .Bar = 1 }

where Foo is the class and Bar is the property.

See: http://msdn.microsoft.com/en-us/library/bb385125.aspx

This is identical to the way that C# initializes object properties, except C# dispenses with the "."

new Foo() { Bar = 1 }

See: http://msdn.microsoft.com/en-us/library/bb384062.aspx

Upvotes: 0

Related Questions