Problem whit loops :x
-
Hi, Im having issues whit a loop. When i try to return the function in the loop, it tells me the function has no return value.
[WebMethod] public string GetBiosInfo(string Username, string Password) { ConnectionOptions co = new ConnectionOptions(); co.Username = Username; co.Password = Password; ManagementScope ms = new System.Management.ManagementScope("\\\\127.0.0.1\\root\\cimv2", co); ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_BIOS WHERE PrimaryBIOS = TRUE"); ManagementObjectSearcher query = new ManagementObjectSearcher(ms,oq); ManagementObjectCollection queryCollection = query.Get(); foreach(ManagementObject mo in queryCollection) { return mo["Name"].ToString(); } }
-
Hi, Im having issues whit a loop. When i try to return the function in the loop, it tells me the function has no return value.
[WebMethod] public string GetBiosInfo(string Username, string Password) { ConnectionOptions co = new ConnectionOptions(); co.Username = Username; co.Password = Password; ManagementScope ms = new System.Management.ManagementScope("\\\\127.0.0.1\\root\\cimv2", co); ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_BIOS WHERE PrimaryBIOS = TRUE"); ManagementObjectSearcher query = new ManagementObjectSearcher(ms,oq); ManagementObjectCollection queryCollection = query.Get(); foreach(ManagementObject mo in queryCollection) { return mo["Name"].ToString(); } }
What the compiler is saying, is that "not all code paths return a value". This is because you don't return anything outside the foreach loop. The problem lies in the fact that it is theoritically possible that the queryCollection is empty. If it is empty you bypass the loop and go directly to the end of the method. Return something after the loop and the problem will go away. Also, are you sure you only want to execute the loop once? I mean right when you enter the foreach for the first time you will be exiting the method also. -Nathan --------------------------- Hmmm... what's a signature?