1

How create a array of objects (another class) in VB.NET and initialise it. Since I am not sure about the length of the array, it should be generic. I mean I should be able to add any number of objects to the array. NB: I am much familiar with Generic List, but my client has given me array of objects :(

Sathish
  • 869
  • 2
  • 13
  • 28
  • consider using the List Collection of System.collection.Generic namespace – Devjosh Jul 05 '11 at 07:27
  • oops i read your NB later sorry :) – Devjosh Jul 05 '11 at 07:28
  • 1
    You cannot add any number of objects to the array. Arrays cannot be dynamically resized. You need an `ArrayList` for that, one of the other classes found in the `Collections` namespace. – Cody Gray - on strike Jul 05 '11 at 07:30
  • @Cody Gray Thats not true - you can use `ReDim` statement for this – VMAtm Jul 05 '11 at 07:34
  • @VMAtm: `ReDim` is not an operator, it is a statement. And there are lots of important caveats to its usage, like you can only resize the *last* dimension of the array, which makes it quite inflexible for use with arrays with multiple dimensions. As well, that essentially re-creates the entire array (and if you specify the `Preserve` option, copies the original items back into the newly-created array). It's quite an expensive operation, and not at all equivalent to the behavior you get with the `Collection` classes. – Cody Gray - on strike Jul 05 '11 at 07:36
  • @Cody Gray Sorry, I misspeled the word. But you are wrond - not the last, but > You can use the ReDim statement to change the size of one or more dimensions of an array that has already been declared : http://msdn.microsoft.com/en-us/library/w8k3cys2%28v=VS.100%29.aspx – VMAtm Jul 05 '11 at 07:39
  • @VMAtm: have a look at [this question](http://stackoverflow.com/questions/327916/redim-preserve-in-c), especially J.Skeets answer. You can resize arrays with more than one dimension but only with `Redim Preserve` what copies all elements to the new array. If you need this, you'll know that you should use a generic List/ArrayList instead. – Tim Schmelter Jul 05 '11 at 08:42
  • @Tim Schmelter Yes, I know, and I mensioned it in the my answer. What exactly you are going to prove me? – VMAtm Jul 05 '11 at 08:48
  • @VMAtm: sorry, i've yet only followed these comments, it wasn't meant in a pejorative sense. – Tim Schmelter Jul 05 '11 at 08:55

1 Answers1

1

I should be able to add any number of objects to the array

Simply, you can’t, arrays aren’t resizable. You can use Array.Resize (or ReDim Preserve) but this will re-allocate the whole array and has abysmal runtime.

Use the list (List(Of T)) for your purpose. If you get the input in the form of an array, it’s a simple matter of transforming this input:

Dim lst As New List(Of YourObject)(inputArray)

Likewise for return values: if you need to return an array, use the ToArray() method of the list.

But using arrays in public interfaces (that is, as parameters and return values of public methods) is bad design and shouldn’t be done anyway. Talk to your client about this.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • But using arrays in public interfaces (that is, as parameters and return values of public methods) is bad design and shouldn’t be done anyway. Talk to your client about this. <- I know this is old but I have an instance with dsf(device simulation framework) where I need an object array to be passed as variant to com and this interface is impossible to please without an array somewhere as the input requires multiple bytes to send. But overall good answer. It more then likely is bad design but tell that to Microsoft with their driver code. – jeffery Aug 02 '13 at 21:24