1

here is my main method, and when i call to Print status. I set the params for PrintStatus and I'm getting an error about eligibility not being used and I cannot figure it out. I'm new to passing arguments, we just went over it in class.

"Error CS0165 Use of unassigned local variable 'eligibility' Program10 I:\Program10\Program10\Program10.cs 150 Active "

static void Main()
    {
        int id, age, exp;
        double avgAge, avgExp;
        char type;
        string eligibility;



        OpenFiles();
        PrintReportHeadings();
        while ((lineIn = fileIn.ReadLine()) != null)
        {
            ParseLineIn(out id, out type, out age, out exp);
            PrintStatus(type, age, exp, eligibility);
            PrintDetailLine(id, type, age, exp);
        }
        CloseFiles();
    }

not sure how to fix this..

static void PrintStatus(char type, int age, int exp, string eligibility)
    {

        switch (type)
        {
            case 'm':
            case 'M':

                if (age < 55 && exp < 20)
                    eligibility = ("lack of experience age");
                else if (age >= 55 && exp >= 20)
                    eligibility = ("can retire");
                else if (age >= 55 && exp < 20)
                    eligibility = ("lack of experience");
                else if (age < 55 && exp >= 20)
                    eligibility = ("underage");
                else
                    eligibility = ("Your entry is invalid");
                break;

            case 'w':
            case 'W':

                if (age < 63 && exp < 25)
                    eligibility = ("lack of exp age");
                else if (age >= 63 && exp >= 25)
                    eligibility = ("can retire");
                else if (age >= 63 && exp < 25)
                    eligibility = ("lack of exp");
                else if (age < 63 && exp >= 25)
                    eligibility = ("lack age");
                else
                    eligibility = ("Your entry is invalid");
                break;

            case 's':
            case 'S':

                if (age < 60 && exp < 24)
                    eligibility = ("lack of exp age");
                else if (age >= 60 && exp >= 24)
                    eligibility = ("can retire");
                else if (age >= 60 && exp < 24)
                    eligibility = ("lack of exp");
                else if (age < 60 && exp >= 24)
                    eligibility = ("underage");
                else
                    eligibility = ("Your entry is invalid");
                break;
        }
    }
4
  • You should give a default value to eligibility for example eligibility=""; Commented Apr 27, 2016 at 19:57
  • 3
    Do you understand that changes to the parameter named eligibility in PrintStatus don't affect your local variable named eligibility in Main? It sounds like your PrintStatus method should be called GetEligibility, and return a string... Commented Apr 27, 2016 at 19:58
  • thats what i want to do actually, Im struggling to figure out how to get it to return the string though Commented Apr 27, 2016 at 20:09
  • Since your question has nothing to do with what you apparently need to achieve it is hard to provide good answer. Reading any book/tutorial on C# would be generally benecial for you, but stackoverflow.com/questions/20803073/… should give you enough of an answer (and approximate score for this type of question). Commented Apr 27, 2016 at 20:13

4 Answers 4

1

If you want to initialize the string eligibility with the code of PrintStatus then the simplest way to do it is to return the string and assign it to eligibility on return from PrintStatus

static string PrintStatus(char type, int age, int exp)
{
    string result = "";
    switch (type)
    {
        case 'm':
        case 'M':

            if (age < 55 && exp < 20)
                result = ("lack of experience age");
            else if (age >= 55 && exp >= 20)
                result = ("can retire");
            else if (age >= 55 && exp < 20)
                result = ("lack of experience");
            else if (age < 55 && exp >= 20)
                result = ("underage");
            else
                result = ("Your entry is invalid");
            break;

        // etc other case
    }
    return result:
}

At this point you call PrintStatus in this way

....
eligibility = PrintStatus(type, age, exp);
....

To understand why your actual code cannot change the eligibility string you should search around about the concepts of passing parameters by value and passing by reference. A good explanation is from this famous article

Parameter passing in C#

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

4 Comments

this does work for me, however when running the file, it has no output of the assigned string to the data file. even after static void PrintDetailLine(int id, char type, int age, int exp, string eligibility) { fileOut.WriteLine("{0} {1} {2} {3}", id, age, exp, eligibility); }
Probably you need to pass the value of eligibility to PrintDetails and then, inside that method, do whatever is required to print the string.
Weird, if that is the updated code then I can't see why it doesn't work.
it wasn't saving the solution. had to re-open file. it does work. thank you.
0

Because the variables are local, you need to assign a default value to them:

    int id = 0, age, exp;
    double avgAge = 0, avgExp;
    char type;
    string eligibility = "";

Do the same for all local variable.

Comments

0

The error message tells well already. Your local variable eligibility is not assigned a value, before using it.

string eligibility = "whatever";

shall remove the error message.

Also your function shall be defined like:

static void PrintStatus(char type, int age, int exp, out string eligibility);

3 Comments

the thing about it is... in this program, im reading in information and based on type i need it to place the correct string in eligibility and output it based on the type.
Yes, put a "out" before eligibility shall also fix the problem. You must tell function that this variable is for output, not need to initialize.
Also, use string as the return type is more readable: static string PrintStatus(char type, int age, int exp) then at the end of your function, add return eligibility. To use it eligibility = PrintStatus(type, age, exp)
0

It looks like you want to get a value for your eligibility. Assuming this is the case I have re-written your PrintStatus method and renamed it GetPrintStatus.

 static string GetPrintStatus(char type, int age, int exp)
    {
        switch (char.ToLower(type))
        {
            case 'm':
                if (age < 55 && exp < 20)
                    return ("lack of experience age");
                if (age >= 55 && exp >= 20)
                    return ("can retire");
                if (age >= 55 && exp < 20)
                    return ("lack of experience");
                if (age < 55 && exp >= 20)
                    return ("underage");
                return ("Your entry is invalid");
            case 'w':
                if (age < 63 && exp < 25)
                    return ("lack of exp age");
                if (age >= 63 && exp >= 25)
                    return ("can retire");
                if (age >= 63 && exp < 25)
                    return ("lack of exp");
                if (age < 63 && exp >= 25)
                    return ("lack age");
                return ("Your entry is invalid");
            case 's':

                if (age < 60 && exp < 24)
                    return ("lack of exp age");
                if (age >= 60 && exp >= 24)
                    return ("can retire");
                if (age >= 60 && exp < 24)
                    return ("lack of exp");
                if (age < 60 && exp >= 24)
                    return ("underage");
                return ("Your entry is invalid");
        }
        return string.Empty;
    }

To use it you would have to include the following line in your main method. Replace this;

PrintStatus(type, age, exp, eligibility);

whit this;

eligibility =  GetPrintStatus(type, age, exp);

type, age and exp must be given values like this;

age = 35;

This must be done before you call the GetPrintStatus method.

Since you are learning I'll explain some of the changes; next to GetPrintStatus is the word "string", this means that the function will return a string (previously you called it eligibility) and char.ToLower(type) make the character you pass in lower case so it no longer matters whether its M or m. This can be simplified further but it would not look much like the code you gave.

Good luck.

2 Comments

i fixed the eligibility not returning a value problem, but it's not writing it to the output file Employee Report ID Age Exp Eligibility ---- --- --- ----------- 1235 45 20 2536 55 21 5894 60 30 4597 75 35 2597 35 10 5689 40 20 5489 55 39 5872 60 40 5569 55 25 5566 80 20 8865 59 35 5598 65 35
If you describe what your application is supposed to I might be able to help

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.