469,313 Members | 2,338 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,313 developers. It's quick & easy.

Stopwatch Query

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
Jun 17 '07 #1
4 1384
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
Jun 17 '07 #2
"PlusNet" <Je**********@somewhere.comwrote in
news:46**********************@ptn-nntp-reader02.plus.net:
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).
Jun 17 '07 #3
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

"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
"PlusNet" <Je**********@somewhere.comwrote in
news:46**********************@ptn-nntp-reader02.plus.net:
>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).

Jun 18 '07 #4
Thanks you both, for your replies. All OK now.

-Jerry

"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
"PlusNet" <Je**********@somewhere.comwrote in
news:46**********************@ptn-nntp-reader02.plus.net:
>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).

Jul 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by luckyjameel | last post: by
3 posts views Thread by Jeff Jarrell | last post: by
1 post views Thread by =?Utf-8?B?UmFqYQ==?= | last post: by
reply views Thread by kerplunkwhoops | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.