0

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Not in VB.NET and this is a VB question. blogs.lessthandot.com/index.php/DesktopDev/MSTech/…
This didn't work. I tried myObj.ArrayProperty = new objType() {obj1, obj2}
I stand corrected, I'm not sure what I did before, but you can do this. myObj.ArrayProperty = New objType() {obj1, obj2}.. thanks!
1

Probably you are looking for this

Dim x As SomeClass() = New SomeClass() { _
    New SomeClass With {.ID = 1, .Name = "John"}, _
    New SomeClass With {.ID = 2, .Name = "Sue"} _
}

You have to create the array before you can create and add objects to it.

It is misleading to say that your object is of an array type. I would prefer to say that you have an array of some type. When initializing the array you want to add it objects of that type and at the same time initialize the properties of these objects. The properties belong to the object in the array, not to the array itself.

The VB way of placing the array parentheses behind the variable is confusing. VB allows placing the array parentheses behind the type in some cases, which seems more logical to me.


UPDATE:

Properties (unlike variables) do not have initializers; however you could initialize the backing variable.

Private _myArrayProperty As SomeClass() = New SomeClass() { _
    New SomeClass With {.ID = 1, .Name = "John"}, _
    New SomeClass With {.ID = 2, .Name = "Sue"} _
}

Public Property MyArrayProperty() As SomeClass()
    Get
        Return _myArrayProperty
    End Get
    Set(ByVal value As SomeClass())
        _myArrayProperty = value
    End Set
End Property

If you want to assign a new array to the property later, you can do it like this

obj.MyArrayProperty = New SomeClass() { _
    New SomeClass With {.ID = 1, .Name = "John"}, _
    New SomeClass With {.ID = 2, .Name = "Sue"} _
}

You can drop the New SomeClass() in variable initializers; however, in other expressions you must explicitly specify New SomeClass() { .... The simplyfied syntax applies only to initializers.

1 Comment

Sorry, Not what I am looking for, as you can see from the examples, I know how to do the above. I want to set a property to the array and init at the same time. I already have the objects for the array instantiated elsewhere. The type doesn't matter for the purposes of the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.