public string ReadGroup(string Group)
{
StreamReader strRead = new StreamReader(Path);
//for (int i = 0; i < strRead.ReadToEnd().Length; i++)
// {
string line;
int charIndex = 0;
while ((line = strRead.ReadLine()) != null)
{
if (line.StartsWith(Group))
{
if (line.Contains("{"))
{
charIndex = line.IndexOf("{");
//Console.WriteLine("Group: " + line.Remove(charIndex, 1) + " was added!");
}
else if (line.Contains("}"))
{
//Console.WriteLine("Group closed!");
}
else
{
//string key = line.Remove(charIndex
//string value = "";
charIndex = line.IndexOf(",");
//Console.WriteLine("Variable: " + line.Remove(charIndex, 1));
return line.Remove(charIndex, 1);
}
}
}
}
I'm getting a "Not all code paths return a value error". How do I fix it? ???
By making sure the function returns a value in every possible scenario?
That was fast!; so you mean I need more than one "return ..."?
What happens when you exit the while loop? :)
The Stream Reader stops reading.
Quote from: dlStevens on October 23, 2007, 04:34:38 PM
The Stream Reader stops reading.
That's not what he was asking. The error means exactly what it says. Given any possible dataset, your method will not necessarily return a value. If the while loop exits without something returning, you still are required to return something.
Quote from: dlStevens on October 23, 2007, 04:27:01 PM
That was fast!; so you mean I need more than one "return ..."?
Either you need more than one return, or you need to re-write the code so that the function can only return in a single place (ie, at the bottom).
ALL methods which have a return-type other than "void" need to return SOMETHING for every possible scenario.
Example:
public int wtf(int blah)
{
if (blah == 3)
{
return 3;
}
}
The above would yield and error because not all possible scenarios of the member return a value.
What happens when blah IS NOT 3?
Nothing. Nothing? That's exactly the problem.
public int wtf(int blah)
{
if (blah == 3)
{
return 3;
}
return 0;
}
would compile correctly.
Now, your solution lies in adding an "Else" switch to your first If-Statement in your while loop. Return a default value there, or even null if you want and all should be fine.
I believe you can see the error in your ways by examining a logic flow diagram:
Are you really bored at work? :)
Quote from: MyndFyrex86/64] link=topic=10527.msg133940#msg133940 date=1193178503]
I believe you can see the error in your ways by examining a logic flow diagram:
.........
Quote from: iago on October 23, 2007, 06:33:30 PM
Are you really bored at work? :)
I am. It's either feast or famine, and I've been brushing up on my security protocols. I'm on a new assignment - security consultant for a client who's transitioning to credit card processing.
Quote from: MyndFyrex86/64] link=topic=10527.msg133940#msg133940 date=1193178503]
I believe you can see the error in your ways by examining a logic flow diagram:
I don't get it.. :\
You might want to throw an exception to indicate that the group wasn't found. Otherwise, it's probably most logical to return null after reaching EOF.
mm, I'm just about finished, I'm taking Camels advice and adding a few exceptions such as null file/group not found.
Thanks guys!