473,545 Members | 4,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1543
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**********@s omewhere.comwro te in
news:46******** **************@ ptn-nntp-reader02.plus.n et:
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**********@r ogers.comwrote in message
news:Xn******** *************** ***********@127 .0.0.1...
"PlusNet" <Je**********@s omewhere.comwro te in
news:46******** **************@ ptn-nntp-reader02.plus.n et:
>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**********@r ogers.comwrote in message
news:Xn******** *************** ***********@127 .0.0.1...
"PlusNet" <Je**********@s omewhere.comwro te in
news:46******** **************@ ptn-nntp-reader02.plus.n et:
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6311
by: luckyjameel | last post by:
Hi Guys Hope this message shall find u all in gr8 moods. As i was developing a Application for Online Exam and i am in need of a way to maintain the time for the logged in user. So Can any one let me know how can i do this with the help of JavaScript. Regards Lucky
5
1546
by: Xero | last post by:
I have written a program that functions similarly to a stopwatch. When the user clicks the button, a label on the form starts counting. The program, however, does not tick once a second. How can I fix this? The following is the program. It assumes that the forms has a Button named Button1, a Label named Label1 and a Timer named Timer1. ...
1
2941
by: Anirhudra | last post by:
Could you anyone can help me how can I send a Stopwatch Javascript value send it through PHP file .... this is my time JavaScript file ... var timercount = 0; var timestart = 0; function displaytime() { if(timercount) { clearTimeout(timercount);
5
10836
by: not_a_commie | last post by:
So I have a motherboard with multiple CPU sockets. It seems that if I create a StopWatch on one thread and then call the Elapsed member from a different thread that sometimes I get a tick count that's a million miles away. My thinking is that I can subclass the StopWatch. Then when the Elapsed member is called, I can invoke it on the thread...
3
2960
by: Jeff Jarrell | last post by:
I am unable to add up two timespans as created by the System.Diagnostics.Stopwatch class. Each stopwatch should be about two seconds and is appears ok, but when I try and add them up it goes to heck. I need to keep sum totals and do avgs and things. Here is the result of the following NUnit test. I'd hope it to be a little closer than...
1
2516
by: =?Utf-8?B?UmFqYQ==?= | last post by:
I am running a sample application that gives me performance time for each action. I am using Stopwatch class (found in System.Diagnostics namespace in ..Net 2.0) to get the Elapsed time. If I run my app in our server that has mutiple cups (4 cpu box) it gives me negative elapsed time and it is unpredictable. Anyone knows how to make...
1
2860
by: fightclub777 | last post by:
Hi, I've done some reasearch to be able to find the best way of acheiving this, but have been unlucky so far. What I'm trying to do is create a stopwatch on a .net page, whereby when a user clicks the "start" button, the timer begins to count up and displays a real-time timer (I'd prefer an Ajax method) to avoid postbacks. The start time will...
8
5448
by: Firecore | last post by:
Hello. I am making a Stopwatch program in C. Can anyone help? Is it possible to make a timer(stopwatch) function in C?
0
2989
by: kerplunkwhoops | last post by:
Hello I am wanting to use the high accuracy of System.Diagnostics.Stopwatch to get the current time using GetTimeSpan. Underneath, System.Diagnostics.Stopwatch.GetTimeStamp uses the win32 API QueryPerformanceCounter which has a reliability issue and can jump forward unexpectedly (http://support.microsoft.com/kb/274323). Does...
0
7464
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7413
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.