-3

Went to check this program I've been doing and I seem to of hit another road back with the error saying; The out parameter 'checkedIfInsured' must be assigned to before control leave the current method.

I can paste the rest of the code if necessary but to me looking at it, it looks fine.

static void GetData(out int patientsID, out string patientsName, out int patientsAge, out decimal patientsAmount, object o, out char checkedIfInsured)
    {
        string inString;
        int count = 3;
        char test;
        Console.Write("Please enter Patients ID number>> ");
        inString = Console.ReadLine();
        int.TryParse(inString, out patientsID);
        Console.Write("Please Enter Name for " + "Patient {0} >> ", patientsID);
        patientsName = Console.ReadLine();
        Console.Write("Please Enter The Age For " + "Patient {0}>> ", patientsName);
        inString = Console.ReadLine();
        int.TryParse(inString, out patientsAge);
        Console.Write("Please Enter The Amount Due For " + "Patient {0}>> ", patientsID);
        inString = Console.ReadLine();
        decimal.TryParse(inString, out patientsAmount);
        Console.WriteLine("-----------------------------------");

        if (o is InsuredPatient)
        {
            Console.WriteLine(" Enter the name of the Patients Insurance Company Code>>");
                for (int x = 0; x < count; ++x)
                    Console.WriteLine("{0,-3} = {1,5}", InsuredPatient.InsurerCharacter[x], InsuredPatient.InsurerName[x]);
            Console.WriteLine(" Enter talent code >> ");
            test = Console.ReadKey().KeyChar;

            for (int i = 0; i < InsuredPatient.InsurerCharacter[i]; ++i)
                if (test == InsuredPatient.InsurerCharacter[i])
                {
                   checkedIfInsured = InsuredPatient.InsurerCharacter[i];
                }
        }


    }
MeggMercer
  • 59
  • 1
  • 3
  • The error means exactly what it says. If the `if` clause in your loop isn't ever true (or if `InsuredPatient.InsurerCharacter[i]` is `<= 0`, `o is InsuredPatient` is false) then `checkIfInsured` is never assigned. – Matt Burland Feb 10 '15 at 21:46
  • 1
    This function is a prime canadate to be refactored to be `static GetDataResults GetData(object o)` and return all of those out statements in a single custom return type. Unless you are P/Invoking in to unmanaged code it is very rare that doing a function with `void` return type and `out` parameters at the same time is a good design choice. – Scott Chamberlain Feb 10 '15 at 21:48
  • 1
    And BTW, using that many `out` parameters seems like really bad design. Just return an object that holds the relevant values. – Matt Burland Feb 10 '15 at 21:48

6 Answers6

0

You are only assigning 'checkedIfInsured' inside an 'if' block. If the condition doesn't hold, it won't get assigned, and that is what the compiler is complaining about.

Make sure you assign 'checkedIfInsured' outside the 'if' block.

CesarGon
  • 15,099
  • 6
  • 57
  • 85
0

You're only assigning the checkIfInsured parameter if o is InsuredPatient. The compiler is telling you that it needs to always be assigned to.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

You only give checkedIfInsured a value in one particular conditional branch. The compiler is telling you that you must give it a value before the method ends, regardless of what conditional branch it takes.

You can avoid this error by setting checkedIfInsured to a default value at the beginning of your method.

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
  • 1
    Interesting. Are you down-voting because the answer is wrong, or because the question isn't a good one? I mean, it isn't, but still... – Chris Mantle Feb 10 '15 at 21:50
  • 2
    I downvoted because you are actively feeding a help vampire by answering this question. Googling the error message posted gives an exact duplicate as the first result, which was actually *answered by a user who answered here*. This is an obvious duplicate, not even a difficult one for someone to find if they searched for five seconds. – eddie_cat Feb 10 '15 at 21:52
  • @eddie_cat: Seems pretty harsh. Nevertheless, if you feel strongly, you can vote to close. Which is what I did. I currently seem to be the only one. – Matt Burland Feb 10 '15 at 21:54
  • @MattBurland I flagged it as duplicate, don't have vote to close powers. – eddie_cat Feb 10 '15 at 21:55
  • @eddie_cat I respect your decision to downvote - you're correct. Voted to close. – Chris Mantle Feb 10 '15 at 22:11
0

If the if is not true, you still need to assign a value in the else. All branches of the code must return a value.

    if (o is InsuredPatient)
    {//...}
    else{
        //default to whatever.
        checkedIfInsured = myDefaultInsuredCheckedValue;
    }
ps2goat
  • 8,067
  • 1
  • 35
  • 68
0

checkedIfInsured may not always have a value as it is inside an if block. What if the criteria of the if is not met?

JammoD
  • 419
  • 5
  • 15
0

The compiler is complaining because there exists a code path where checkedIfInsured hasn't been set, namely the case where the test variable does not equal InsuredPatient.InsurerCharacter[i].

What you could do is set checkedIfInsured to some default character at the beginning of the method to handle the case that test does not equal InsuredPatient.InsurerCharacter[i].

ama1111
  • 569
  • 3
  • 10