|
-
May 6th, 2008, 11:18 PM
#1
How can I 'return' an array of values
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
-
May 7th, 2008, 12:06 AM
#2
Re: How can I 'return' an array of values
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( );
}
-
May 7th, 2008, 02:10 AM
#3
Re: How can I 'return' an array of values
Why didnt you just try it?
Code:
//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;
}
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 string
C# would give an error something like "no implicit conversion exists between string[] and strong"
-
May 7th, 2008, 03:32 AM
#4
Re: How can I 'return' an array of values
As cjar said: the answer is in the question itself
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
May 7th, 2008, 07:15 AM
#5
Re: How can I 'return' an array of values
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
-
May 7th, 2008, 08:52 AM
#6
Re: How can I 'return' an array of values
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.
-
May 7th, 2008, 11:48 AM
#7
Re: How can I 'return' an array of values
Well, cilu, it is returning a list....
this is what you mean right?
Code:
return list.ToArray( );
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|