Pregunta

What I need is, checkChar(line[i]) to return false or char. So if result is not false, for loop works and if there is result, it breaks and prints out char.

I'm newbie to c#, so no idea how to do it. I tried this way.

using System.IO;
using System;

class Program
{
    static void Main()
    {
       Console.Write("Write a string: ");
       string line = Console.ReadLine();
       char result;
       if (line != null)
        {
            for(int i=0; i<=line.Length; i++){
                result = checkChar(line[i]);
                if(!result.Equals("#"))
                    break;
            }
            Console.WriteLine(result);
        }
    }    

    static char checkChar(char input){
        char[] letters={'f', 'g', 'h'};
        char noResult="#";
        foreach(char letter in letters){
            if(input.Equals(letter))
                return letter;
            else return noResult;
        }
    }
}

Getting error:

main.cs(18,31): error CS0165: Use of unassigned local variable `result'
main.cs(24,14): error CS0029: Cannot implicitly convert type `string' to `char'
main.cs(22,17): error CS0161: `Program.checkChar(char)': not all code paths return a value
Compilation failed: 3 error(s), 0 warnings
¿Fue útil?

Solución 3

Does it have to be bool? Why not null?

char? noresult = null

so the check would be

for(int i=0; i<=line.Length; i++){
    result = checkChar(line[i]);
    if(!result.HasValue)
        break;
}

Otros consejos

 char noResult="#";

should be

 char noResult= '#';

"" denotes a string where as '' denotes a char.

Should the method not be the following?

static char checkChar(char input){
        char[] letters={'f', 'g', 'h'};
        char noResult="#";
        foreach(char letter in letters){
            if(input.Equals(letter))
                return letter;
        }
        return noResult;
    }

So once the foreach has completed the iteration of letters, noResult gets returned rather than before the iteration has completed? This way you are always returning a result even if a char has not been found.

In order to return a bool the usual way to do this is to use an out parameter:

static bool checkChar(char input, out char output){
    char[] letters={'f', 'g', 'h'};
    char noResult = '#';
    foreach(char letter in letters){
        if(input.Equals(letter))
        {
            // this is okay. Continue.
        }
        else
        {
            output = noResult;
            return false;
        }
    }

    output = letter;
    return true;
}

Use it like this:

char output;
if (checkChar('?', out output))
{
    // valid, do something
}

Result error: You are creating a result variable of type char. The value of result is null at first.

Then you initialize it inside the if, and use it afterwards BUT if "line" equals null, then result is never initialized, therefore you are using a Console.WriteLine(null).

For the conversion error

char noResult="#"; 

The # should be enclosed in single quotation marks '#'

Then, not all paths returning a value is because the CheckChar has no stand alone return clause, this means that if letters is null (you and I know it is not, but the compiler does not), then the foreach never gets executed, therefore no return will be called.

You have a few options. None of them are exactly what you want, but if you write the caller correctly, they can solve the same Problem.

  • Return a nullable char (char?, or System.Nullable<char>). This can be either a char or a null reference.
  • Return a string instead of a char, return the empty string instead of false.
  • Use the Unicode code-Point 0 ('\0') instead of false.

In most cases I would return a string, but either of the other two might be the right solution for some problems.

Here is the working code...

void Main()
{
           string line = Console.ReadLine();
           bool result;
           for(int i=0; i<line.Length; i++)
           {
               result = checkChar(line[i]);
               if(!result)
                   continue;
                Console.WriteLine(line[i]);
            }
}


static bool checkChar(char input)
{
   char[] letters={'f', 'g', 'h'};
   return letters.Contains(input);
}

When defining variable, assign a default value. for example:

char result = char.MinValue;

Also change checkChar method like this:

static char checkChar(char input){
        char[] letters={'f', 'g', 'h'};
        char result='#';
        foreach(char letter in letters){
            if(input.Equals(letter))
              result = letter;  
        }

        return result;
    }
static void Main()
    {
       Console.Write("Write a string: ");
       string line = Console.ReadLine();
       object result;
       if (line != null)
        {
            for(int i=0; i<=line.Length; i++){
                result = checkChar(line[i]);
                if(result is bool)
                    break;
            }
            Console.WriteLine(result);
        }
    }    

    static object checkChar(char input){
        char[] letters={'f', 'g', 'h'};
        char noResult="#";
        foreach(char letter in letters){
            if(input.Equals(letter))
                return letter;
            else return false;
        }
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top