Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

How to check item in collection in WMI ManagementObject

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joao Rego - 22 Dec 2005 12:35 GMT
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
Salvador - 22 Dec 2005 14:28 GMT
Hi,

I had the same problem, use

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

Cheers

Signature

Salvador Alvarez Patuel
Exony Ltd - London, UK

> Hello,
>
[quoted text clipped - 17 lines]
>
> Joao Rego
Joao Rego - 22 Dec 2005 14:36 GMT
OK... that's it... thanks for your help.

> Hi,
>
[quoted text clipped - 26 lines]
> >
> > Joao Rego
Stoitcho Goutsev (100) [C# MVP] - 22 Dec 2005 14:30 GMT
Joao,

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

Signature

Stoitcho Goutsev (100) [C# MVP]

> Hello,
>
[quoted text clipped - 18 lines]
>
> Joao Rego

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.