Reputation: 71
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;
}
}
Upvotes: 1
Views: 73
Reputation: 5487
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.
Upvotes: 0
Reputation: 216353
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
Upvotes: 1
Reputation: 251
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);
Upvotes: 0
Reputation: 2201
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.
Upvotes: 0