I'm getting the error "Not all code paths return a value". I know what the error means but I am buggered if I can see why it is happening in my code - something is returned whether the IF statement evaluates to true or false.
private Server loadServer()
{
string[] filenames = Directory.GetFiles(Application.StartupPath + @"\XML\Server\", "*.xml");
foreach (string filename in filenames)
{
if (filename.Contains(selectedBranch))
{
return GlobalFunctions.LoadServer(filename);
}
else
{
MessageBox.Show("Server Information Not Found!!");
return null;
}
}
}
and for clarity, this is the method called by GlobalFunctions.LoadServer
public static Server LoadServer(string filename)
{
try
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
XmlSerializer serialiser = new XmlSerializer(typeof(Server));
XmlReader reader = XmlReader.Create(new FileStream(filename, FileMode.Open), settings);
Server server = (Server)serialiser.Deserialize(reader);
reader.Close();
return server;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
I added return null immediately after the foreach loop exits and the code now compiles
I'm thinking that it's possible for the string array to be empty, so the foreach loop will not run?
This post has been edited by AdoTheLimey: 26 September 2011 - 02:07 PM

New Topic/Question
Reply




MultiQuote





|