CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2008
    Posts
    21

    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

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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( );
    }

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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"
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  5. #5
    Join Date
    May 2008
    Posts
    21

    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

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    May 2008
    Posts
    21

    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
  •  





Click Here to Expand Forum to Full Width

Featured