How can I return an array of values in C# (instead of the usual just one string or int or double, etc? I want to return an array of strings....
Thanks
Printable View
How can I return an array of values in C# (instead of the usual just one string or int or double, etc? I want to return an array of strings....
Thanks
While you can manually create an array of objects, an easier way to do it is to use a generic list (or array):
Code:public string[] GetStrings( )
{
List< string > list = new List< string >( );
list.Add( "string1" );
list.Add( "string2" );
return list.ToArray( );
}
Why didnt you just try it?
at a guess, i'd say you forgot to change the return type (underlined) of the method, and you were trying to return a string[] via a stringCode://if you know how to do this:
public string GetString(){
string s = "s";
return s;
}
//try this
public string[] GetStrings(){
string[] s = {"s","t"};
return s;
}
C# would give an error something like "no implicit conversion exists between string[] and strong"
As cjar said: the answer is in the question itself :)
Ok guys, here's the tough part though...I'm trying to call that method GetStrings() to display on a checkbox list once a button is clicked. GetStrings() is actually in a dll file....So the difficulty I'm having is displaying the items in the array in GetStrings onto the checkbox list....
Any ideas?
Thanks again
So, you're saying that you have an assembly, where there is a class with a method GetStrings, and you want to call this method and it should display some strings in a checkbox list? That is completely wrong. That method should return a list, and in your code you take that list and populate the control with its elements.
Well, cilu, it is returning a list....
this is what you mean right?
Code:return list.ToArray( );