Jonathan.
Jonathan.

Reputation: 55544

Not able to add to array (need to use new keyword) in VB.NET

I have a property which is an array of items, but when I come to add an item to the array it says I must use the new keyword, but I can't use a new keyword with a property.

(item is a custom class.)

Private itemsvalue As item()
Public Property Items() As item()
    Get
        Return itemsvalue
    End Get
    Set(ByVal value As DBPFindexitem())
        itemsvalue = value
    End Set
End Property

Sub New(ByVal indexbytes As Byte(), ByVal number as integer)

     For count As Integer = 0 To number
        Dim offset As Integer = count * 2 * 4
        Dim xx As New item
        xx.Group = BitConverter.ToInt32(indexbytes, offset + 0)
        xx.ID = BitConverter.ToInt32(indexbytes, offset + 8)
        Items(count) = xx
    Next
End Sub

How can I add the xx item to the array of items?

Upvotes: 0

Views: 585

Answers (3)

SLaks
SLaks

Reputation: 887365

You should be using a collection instead of an array.
A Collection will automatically resize to hold as many items as you put into it.

For example:

Private itemsvalue As New List(Of item)
Public Property Items() As List(Of item)
    Get
        Return itemsvalue
    End Get
    Set(ByVal value As List(Of item))
        itemsvalue = value
    End Set
End Property

Sub New(ByVal indexbytes As Byte(), ByVal number as integer)
     For count As Integer = 0 To number
        Dim offset As Integer = count * 2 * 4
        Dim xx As New item
        xx.Group = BitConverter.ToInt32(indexbytes, offset + 0)
        xx.ID = BitConverter.ToInt32(indexbytes, offset + 8)
        Items.Add(xx)    
    Next
End Sub

If you're writing a public library, you should use a System.Collections.ObjectModel.Collection(Of item) instead. This will allow you to replace it later with an inherited collection with special behavior, without breaking existing clients. See here.

If you don't want other people to modify the collection, you should expose a System.Collections.ObjectModel.ReadOnlyCollection(Of item).


If you really want to use an array, you should initialize the array, like this:

Items = New item(number)

Upvotes: 1

One more thing: unless you want to be able to replace the whole Items collection with another one, you might want to get rid of the setter and make Items read-only:

Private itemsvalue As New List(Of item)

Public ReadOnly Property Items() As List(Of item)
    Get
        Return itemsvalue
    End Get
End Property

This way, Items is initially an empty list and cannot be changed to another collection. If you need a fresh start with it, you call Items.Clear().

(This suggestion is from the .NET Framework Design Guidelines btw.)

Upvotes: 2

richardtallent
richardtallent

Reputation: 35373

You've declared the private array, but you never instantiated it.

In VB.NET, arrays are objects.

Private itemsvalue As New item(numItems)

That's where the "new" goes... also, you'll need to provide some bounds for that array, which is why it's better to use generic List<Item> rather than an array, as another person suggested, but you still have to instantiate the list.

Private itemsvalue As New System.Collections.Generic.List(Of Item)

Then you can call itemsvalue.Add(myitem) as many times as necessary without worrying about array bounds.

Upvotes: 0

Related Questions