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

}
arukiri123
  • 141
  • 1
  • 2
  • 15

6 Answers6

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.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
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;

}
Plue
  • 1,739
  • 1
  • 18
  • 22
2

You can simply do this:

return new string(letters);
Belogix
  • 8,129
  • 1
  • 27
  • 32
1

Just return a new string:

return new string(letters);
D Stanley
  • 149,601
  • 11
  • 178
  • 240
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);
Community
  • 1
  • 1
  • i got an error. Cannot implicitly convert type 'char[]' to 'char' – arukiri123 Jan 17 '14 at 14:55
  • cannot implicitly convert type 'char[]' to 'string'. it refers to this code now: lblresult.Text = bsortfuncion(characters); – arukiri123 Jan 17 '14 at 15:00
  • Use:return new string(letters); –  Jan 17 '14 at 15:00
  • 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 – arukiri123 Jan 17 '14 at 15:08
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;
       }

   }

 }
Selman Genç
  • 100,147
  • 13
  • 119
  • 184