Hi
I have a general question about Lists in C#.
Here is my Code:
public List<string> Example()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSStorageDriver_FailurePredictStatus");
List<string> output = new List<string>();
foreach (ManagementObject queryObj in searcher.Get())
{
output.Add(System.Convert.ToString(queryObj["InstanceName"]));
}
return output;
}
and now I want to give the first input out
public FormMain()
{
Debug.WriteLine(Example(1));
}
No overload for method ‘output’ takes 1 arguments
I hope you can explain me this and sorry for my question, I am an absoltue beginner
Best wishes
Well output is a List. Since you have coded Example as a method, which returns a list, to access it you need to call it using method syntax with empty parentheses. The return value is an instance of a
List<string>. If you hit the decimal point after typingExample()you will see in intellisense the members of this object. One of them will show as square brackets like this[]. this is the member you need to use to access whatever you have put in the list. The values you would provide are zero-based, that is they start at zero (for the first item in the list), and increase from there. So to access the first item in the list, you would write:Debug.WriteLine(Example()[1]);using the square brackets, not parentheses. You still need the parentheses in Example(), because it is a method… If you recoded it as a property:
Then you would not need those parentheses and could just write
Debug.WriteLine(Example[1]);