Search code examples
.netvb.netarrays.net-2.0

How to set an array property to a new initialised array in one line


I want to set a property of an object that is an array type.

If I was declaring and initialising an array of objects, I'd do this:

Dim x() as SomeObject = {obj1, obj2}

I don't seems to be able to do something similar with the property, can you do this, does anyone know the correct syntax?

myObj.ArrayProperty = {obj1, obj2}

I get Expression Expected Error on the first {

Infer Off
Explicit On
Strict On

Answer :

myObj.ArrayProperty = New objType() {obj1, obj2}

Solution

  • you need new keyword ex:

    this work under visual studio 2010 with a targeted framework of 2.0

    Module Module1
    
    Sub Main()
        test = New String() {"a", "a"}
    End Sub
    
    Public Property test As String()
    
    
    End Module