473,396 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PerformanceCounter array within Constructor

Here is my problem. I'm creating an Instrumentation class that will use
previously created Performance Categories and Counters in order to time
various processes (ie. query duration etc.). This Instrumentation class
will be used by a variety of "services", so the Categories and Counters
to be used within the object must be set during object construction.

So I created a class variable array of:

private static PerformanceCounter[] arrayCounters = null;

I created an overloaded constructor:

public Instrumentation(string logName, string PerfCategory, string[]
PerfCounters, string CASContext)

The parameters are passed in by the consuming service, with the
PerformanceCounters to be tracked in the form of a string array.

Within the constructor, I process the string array and try to set the
array members of PerformanceCounter array:

long arrayCountersIndex = 0;
foreach (string counter in PerfCounters)
arrayCounters[arrayCountersIndex++] = new
PerformanceCounter(PerfCategory, counter, false);

The solution builds with no errors, but as soon as the previous code
line is executed during testing I get a "System.NullReferenceException:
Object reference not set to an instance of an object." error on the
arrayCounters object.

The constructor will work if I comment out the setting of the array and
hard code the original class variable like this:

private static PerformanceCounter[] arrayCounters = {new
PerformanceCounter("ServiceName", "ServiceLocationDuration", false),
new PerformanceCounter("ServiceName", "OracleConnectionDuration",
false),
new PerformanceCounter("ServiceName", "QueryDuration", false),
new PerformanceCounter("ServiceName", "WebServiceDuration", false)};

Any help I could get with getting that constructor array to work would
be greatly appreciated.

Thanks - jimbo
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
1 4186
Jimbo,

You have to declare the space for the array before you assign it.
Basically, in your constructor, before you look through the PerfCounters
array, you should do this:

// Allocate the array.
arrayCounters = new PerformanceCounter[PerfCounters.Length];

And this will allocate your array. Of course, if PerfCounters is null,
you will get an error, so make that check beforehand. Other than that, the
code should work fine.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"jimbo" <no****@nospam.ca> wrote in message
news:u0**************@tk2msftngp13.phx.gbl...
Here is my problem. I'm creating an Instrumentation class that will use
previously created Performance Categories and Counters in order to time
various processes (ie. query duration etc.). This Instrumentation class
will be used by a variety of "services", so the Categories and Counters
to be used within the object must be set during object construction.

So I created a class variable array of:

private static PerformanceCounter[] arrayCounters = null;

I created an overloaded constructor:

public Instrumentation(string logName, string PerfCategory, string[]
PerfCounters, string CASContext)

The parameters are passed in by the consuming service, with the
PerformanceCounters to be tracked in the form of a string array.

Within the constructor, I process the string array and try to set the
array members of PerformanceCounter array:

long arrayCountersIndex = 0;
foreach (string counter in PerfCounters)
arrayCounters[arrayCountersIndex++] = new
PerformanceCounter(PerfCategory, counter, false);

The solution builds with no errors, but as soon as the previous code
line is executed during testing I get a "System.NullReferenceException:
Object reference not set to an instance of an object." error on the
arrayCounters object.

The constructor will work if I comment out the setting of the array and
hard code the original class variable like this:

private static PerformanceCounter[] arrayCounters = {new
PerformanceCounter("ServiceName", "ServiceLocationDuration", false),
new PerformanceCounter("ServiceName", "OracleConnectionDuration",
false),
new PerformanceCounter("ServiceName", "QueryDuration", false),
new PerformanceCounter("ServiceName", "WebServiceDuration", false)};

Any help I could get with getting that constructor array to work would
be greatly appreciated.

Thanks - jimbo
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: W1ld0ne74 | last post by:
I am implimenting Performance counters into a web application. I use the following code to create the counters during setup: private void SetupPerfCntrs() {...
1
by: Chris | last post by:
I'm trying to do something really easy: just get the % CPU load. However, I just get mostly 0's sprinkled with an occasional 100. I poked around on the web for info, but all the stuff I found (I...
4
by: Eric A. Johnson | last post by:
For the following code: ' return String representation of CTriangleShape Public Overrides Function ToString() As String ' use MyBase reference to return CShape String Return...
3
by: Rob Meade | last post by:
Hi all, I'm having a bit of trouble with the following function.... Private Function GetSystemUpTime() As TimeSpan ' declare variables Dim Result As TimeSpan Dim PerformanceCounter As...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
3
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and...
5
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... I've got some custom performance counters that can have multiple instances. I use the PerformanceCounter() constructor with the instance name parameter. The thing that puzzles me, though,...
14
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that...
11
by: =?Utf-8?B?U2FsYW1FbGlhcw==?= | last post by:
Has anybody worked with performancecounter object to access counters on remote machine? I managed to write code that retrieves counters categories froma remote machine, when I try another remote...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.