views:

84

answers:

3

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 keyboard 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

I have no idea how I can add the xx item to the array of items.

+1  A: 

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)
SLaks
Yours wouldn't work either, you need New(), but you fixed it while I was fixing mine. ;) I like your solution better anyway, he really ought to be using a collection. Also, you're using C# syntax, VB.NET uses `(Of T)` rather than `<T>`.
richardtallent
Thanks for reminding me about the syntax.
SLaks
Sorry so there are collections, lists and arrays? What are the diffrences
Jonathan
`Collection` and `List` are two different classes. `Collection` is more extensible, allowing you to customize its behavior.
SLaks
Arrays cannot be resized in-place.
SLaks
For more information, see here: http://msdn.microsoft.com/en-us/library/e1ad18x6.aspx or here: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/9439eda3-be67-4123-8fa5-e58828d89b15
SLaks
A: 

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.

richardtallent
This won't work - you need to specify a size.
SLaks
I can't specify a size because any number of items may be added to the array later. Would a collection be better?
Jonathan
Yes, it would be much better. See my answer.
SLaks
So what are the differences between lists, arrays and collections
Jonathan
+2  A: 

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.)

stakx