Stopwatch Query

P

PlusNet

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

Normally of course you would specify 'New':

Dim Mystopwatch as new Stopwatch

But you can't do a 'New' with an array. 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.

-Jerry
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

PlusNet said:
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

Guest

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

You need to instantiate EACH instance of the array.

So Dim MyStopwatch() as Stopwatch

For Each Item as StopWatch in MyStopWatch
If Item is nothing then
Item = new StopWatch
end if
Next

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

Yup - as expected.
But you can't do a 'New' with an array. 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.

Because other objects are NOT arrays.

And if they are arrays, chances are they're arrays of primatives (and
thus don't need instantiation).
 
P

PlusNet

Very many thanks for both these answers. That has helped my understanding
enormously. That has also explained other strange effects I have had in
other programs!

-Jerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top