PlusNet wrote:
Why doesn't the following work?
Dim MyStopwatch() as Stopwatch
..Later in the code
redim preserve MyStopwatch(10)
..Later in the code
MyStopwatch(1).start
The problem is that all the instances of MyStopwatch are set to nothing and
when I try doing anything to them it results in an error:
Object reference not set to an instance of an object
No, the problem is that you don't have any instances at all. You have
created an array of references, but all references are null. You have to
create each StopWatch instance that you want to use.
Normally of course you would specify 'New':
Dim Mystopwatch as new Stopwatch
But you can't do a 'New' with an array.
Exactly. You have to create each and every instance of StopWatch. There
is no magic multi-constructor.
The strange thing is that I don't
get the same error thrown when I do the same with all the other objects in
my program - just the Stopwatch.
Yes, you do. Any array of references works the same.
If you create an array of value types, integers for example, that works
differently. A value type is a value in itself, so when the array is
initialised with values, the integers are set to zero.
Reference type arrays are also initialised, but the difference is that
the value is the reference, so they get initialised to null.
--
Göran Andersson
_____
http://www.guffa.com