471,318 Members | 3,268 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to check item in collection in WMI ManagementObject

Hello,

Im using WMI to get CPU information through Win32_Processor.
It returns a collection but some items are "not valid".

.....
foreach( ManagementObject oReturn in oReturnCollection )
{
Console.WriteLine(oReturn["Manufacturer"].ToString());
Console.WriteLine(oReturn["ProcessorId"].ToString());
}

If oReturn["ProcessorId"] does not "exist" it crash with error:
Additional information: Object reference not set to an instance of an object.

How can I test to see if oReturn["ProcessorId"] is set to an instance
befor I WriteLine(...) it?

Thanks,

Joao Rego

Dec 22 '05 #1
3 12390
Hi,

I had the same problem, use

if (oReturn["ProcessorId"] != null)
<read operations here>

Cheers

--
Salvador Alvarez Patuel
Exony Ltd - London, UK
"Joao Rego" wrote:
Hello,

Im using WMI to get CPU information through Win32_Processor.
It returns a collection but some items are "not valid".

....
foreach( ManagementObject oReturn in oReturnCollection )
{
Console.WriteLine(oReturn["Manufacturer"].ToString());
Console.WriteLine(oReturn["ProcessorId"].ToString());
}

If oReturn["ProcessorId"] does not "exist" it crash with error:
Additional information: Object reference not set to an instance of an object.

How can I test to see if oReturn["ProcessorId"] is set to an instance
befor I WriteLine(...) it?

Thanks,

Joao Rego

Dec 22 '05 #2
Joao,

what if you first read the property in a object variable and check this
variable for *null* before printing it out?
--

Stoitcho Goutsev (100) [C# MVP]

"Joao Rego" <Jo******@discussions.microsoft.com> wrote in message
news:2F**********************************@microsof t.com...
Hello,

Im using WMI to get CPU information through Win32_Processor.
It returns a collection but some items are "not valid".

....
foreach( ManagementObject oReturn in oReturnCollection )
{
Console.WriteLine(oReturn["Manufacturer"].ToString());
Console.WriteLine(oReturn["ProcessorId"].ToString());
}

If oReturn["ProcessorId"] does not "exist" it crash with error:
Additional information: Object reference not set to an instance of an
object.

How can I test to see if oReturn["ProcessorId"] is set to an instance
befor I WriteLine(...) it?

Thanks,

Joao Rego

Dec 22 '05 #3
OK... that's it... thanks for your help.

"Salvador" wrote:
Hi,

I had the same problem, use

if (oReturn["ProcessorId"] != null)
<read operations here>

Cheers

--
Salvador Alvarez Patuel
Exony Ltd - London, UK
"Joao Rego" wrote:
Hello,

Im using WMI to get CPU information through Win32_Processor.
It returns a collection but some items are "not valid".

....
foreach( ManagementObject oReturn in oReturnCollection )
{
Console.WriteLine(oReturn["Manufacturer"].ToString());
Console.WriteLine(oReturn["ProcessorId"].ToString());
}

If oReturn["ProcessorId"] does not "exist" it crash with error:
Additional information: Object reference not set to an instance of an object.

How can I test to see if oReturn["ProcessorId"] is set to an instance
befor I WriteLine(...) it?

Thanks,

Joao Rego

Dec 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Chris Fink | last post: by
1 post views Thread by jkinneberg | last post: by
reply views Thread by rosydwin | last post: by

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.