0

how do i return an array of characters from function? I'm currently using c#.I have this function :

public char bsortfunction(char[] letters)
{
  char temp;
  for (int i = 0; i < letters.Length; i++)
  {
   for (int x = 0; x < i; x++)
   {
    if (letters[i] < letters[x])
    {
      temp = letters[i];
      letters[i] = letters[x];
      letters[x] = temp;
     }

   }

  }
   return (cant seem to figure out how to return the letters[])

}

private void btnsort_Click(object sender, EventArgs e)
{
   char[] characters = txtstring.Text.ToCharArray();       
   lblresult.Text = bsortfunction(characters);

}
4
  • so it should be a public char. okay i'll try it sir, thanks Commented Jan 17, 2014 at 14:49
  • please clarify your question.do you want to return a string or char array? Commented Jan 17, 2014 at 14:49
  • i want to return a char array Commented Jan 17, 2014 at 14:50
  • Do it! char-array is noted with [] after char: char[] Commented Jan 17, 2014 at 15:04

6 Answers 6

4

If you just want to convert your character array into a string, you can write:

return new string(letters);

If you need to return an actual character array, you need to change the function's return type to char[] instead of string.

Sign up to request clarification or add additional context in comments.

Comments

3

You are trying to return a string. Try this :

public char[] lettersbsortfunction(char[] letters)
{
  char temp;
  for (int i = 0; i < letters.Length; i++)
  {
   for (int x = 0; x < i; x++)
   {
    if (letters[i] < letters[x])
    {
      temp = letters[i];
      letters[i] = letters[x];
      letters[x] = temp;
     }

   }

  }
   return letters;

}

2 Comments

thanks sir. now the problem is when i call the function. it says that it cannot convert char[] to string
So return a string =) look at dvnrrs answer
2

You can simply do this:

return new string(letters);

Comments

1

Just return a new string:

return new string(letters);

Comments

0

use:

  public char[] bsortfunction(char[] letters)
    {
        char temp;
        for (int i = 0; i < letters.Length; i++)
        {
            for (int x = 0; x < i; x++)
            {
                if (letters[i] < letters[x])
                {
                    temp = letters[i];
                    letters[i] = letters[x];
                    letters[x] = temp;
                }

            }

        }
        return (letters);
    }

Or you can use:

return new string(letters);

sample:

char[] chars = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'};
string s = new string(chars);

4 Comments

i got an error. Cannot implicitly convert type 'char[]' to 'char'
cannot implicitly convert type 'char[]' to 'string'. it refers to this code now: lblresult.Text = bsortfuncion(characters);
Use:return new string(letters);
there's no errors in the method anymore. the error now is when i call the function: lblresult.text = bsortfunction(characters);. it says that it cannot convert char[] to string
0

If you want to return string try:

 return new String(letters);

If you want to return char array change return type of your method to char array:

public char[] lettersbsortfunction(char[] letters)

And :

return letters;

Alternatively you can use ref keyword in this case.If you want to just manipulate letters array,you don't need to return anything:

 public void bsortfunction(ref char[] letters)
 {
    char temp;
    for (int i = 0; i < letters.Length; i++)
    {
      for (int x = 0; x < i; x++)
      {
        if (letters[i] < letters[x])
        {
           temp = letters[i];
          letters[i] = letters[x];
          letters[x] = temp;
       }

   }

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.