FIX: Error in example of "Using Strongly-typed Objects" document (307508)



The information in this article 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 "Using Strongly-typed Objects" 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 "Service" in ROOT.CIMV2.Win32 namespace.
	
	class Sample {
		// Enumerate instances of Win32_Service class.
		void EnumerateServices() {
			Console.WriteLine("List services and their state");
			foreach(Service ser in Service.GetInstances())
				Console.WriteLine("Service: "+ ser.Name + " is " + 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.

Modification Type:MinorLast Reviewed:1/19/2006
Keywords:kbvs2005doesnotapply kbvs2005swept kbvs2002sp1sweep kbfix kbbug kbdocerr kbnofix kbreadme KB307508 kbAudDeveloper