As Holems said to Watson "It's elementary". ;]
Ok. The reason that you are getting your first error (not all code paths return a value) is because all code paths don't return a value. Simply put, all functions MUST return a value even if it is: return null; but, move so, your code must return a value through all possible branches of your code.
So in this code
public string GetLine()
{
string s1;
try
{
// Create an instance of StreamReader to read from a file
using (StreamReader sr = new StreamReader("expression test.txt"))
{
if(sr.Peek() != -1)
{
s1 = sr.ReadLine();
return s1;
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
you have a conditional statement (your if) that may or may not execute but, consider that the if doesn't execute and there is no error, code execution will continue to the end of your function and never find a return value. Moveover, if your if DOES execute but an execption is thrown inside the if, the return value will be missed, code execution will pass to your catch and, again, will flow to the end of the function without having a value returned.
So you will want to modifiy your code to be something like:
public string GetLine()
{
try
{
// Create an instance of StreamReader to read from a file
using (StreamReader sr = new StreamReader("expression test.txt"))
{
if(sr.Peek() != -1)
{
return sr.ReadLine();
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
//Console.WriteLine("The file could not be read:");
//Console.WriteLine(e.Message);
return e.Message.ToString();
}
return null;
}
if you seriously want to write an exception out to the console you might want to do so as an out parameter such as:
...
int ErrorCode;
string foo = GetLine(out ErrorCode);
if(ErrorCode == 1){Console.WriteLine(foo);}
else if(ErrorCode == -1)(Console.WriteLine("No Data Returned!");}
...
public string GetLine(out int ErrorCode)
{
ErrorCode = 0;
try
{
// Create an instance of StreamReader to read from a file
using (StreamReader sr = new StreamReader("expression test.txt"))
{
if(sr.Peek() != -1)
{
return sr.ReadLine();
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
//Console.WriteLine("The file could not be read:");
//Console.WriteLine(e.Message);
ErrorCode = 1;
return e.Message.ToString();
}
ErrorCode = -1;
return null;
}
so ya, you should be good to go!
hth
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========