Microsoft KB Archive/307508

= FIX: Error in example of &quot;Using Strongly-typed Objects&quot; document =

Article ID: 307508

Article Last Modified on 1/19/2006

-

APPLIES TO


 * Microsoft Windows Management Instrumentation in . NET
 * Microsoft Visual Studio .NET 2002 Professional Edition

-



This article was previously published under Q307508



SUMMARY
The code sample for the strongly-typed objects usage in the .NET document titled &quot;Using Strongly-typed Objects&quot; is incorrect and does not compile.



MORE INFORMATION
Errors in the sample code:   The code does not compile. The compiler throws the following exception in the foreach statement: t.cs(15,3): error CS0030: Cannot convert type 'System.Management.ManagementBaseObject' to 'ROOT.CIMV2.Win32.Service'   The ManagementObjectSearcher constructor has an incorrect query string. The following is the correct string: select * from Win32_Service  The EnumerateServices method is missing .

Replace the sample code with the following sample code: using System; using ROOT.CIMV2.Win32; // Contains the strongly-typed generated class &quot;Service&quot; in ROOT.CIMV2.Win32 namespace. class Sample { // Enumerate instances of Win32_Service class. void EnumerateServices { Console.WriteLine(&quot;List services and their state&quot;); foreach(Service ser in Service.GetInstances) Console.WriteLine(&quot;Service: &quot;+ ser.Name + &quot; is &quot; + ser.State); }       public static void Main(string[] args) { Sample test = new Sample; test.EnumerateServices; return; }   }



STATUS
This bug was corrected in Visual Studio .NET 2003.

Additional query words: kbreadme

Keywords: kbbug kbfix kbnofix kbreadme kbdocerr kbvs2002sp1sweep kbvs2005swept kbvs2005doesnotapply KB307508

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.