473,406 Members | 2,849 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,406 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 12565
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
3
by: KathyB | last post by:
Hi, I'm trying to find a way to validate input text boxes where I don't know the names until the page is rendered. I've got 2 validate functions that fire with the onsubmit button of a "mini" form...
7
by: Neil | last post by:
I have a check box on a form that's bound to a function that returns a True/False value. When the user clicks on the check box, I run some code through the MouseDown event. Everything works fine. ...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
2
by: jmhmaine | last post by:
I've created a lookup class for a windows form application and I'm stuck on how on using the collection object. The process is simple, if the item is in the collection then return that object, if...
0
by: Chris Fink | last post by:
I am looking for all the string indexers for the ManagementObject collection and do not know where in the MSDN documentation to find them. Name and Description are two of them, but I would like a...
3
by: Bmack500 | last post by:
I have a sub, and a class. The sub is like this: Sub dothis() Dim aInfoListX As New infoWrapperClass aInfoListX.params = ReadConfig() Dim sqlCMD As New SqlCommand aInfoListX.sqlCmd = sqlCMD ...
2
by: AdawayNoSpam | last post by:
Said that I have the following class Class MyRootClass(Of T) End Class Class MySubClass1(Of T) Inherits MyRootClass(Of T) End Class
1
by: jkinneberg | last post by:
In the process of developing a C# GUI for our desktop support team. Basic code following allows me to query the contents of a collection. Would someone help me by posting a sample of how to add...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.