Clan x86

Technical (Development, Security, etc.) => General Programming => Topic started by: abc on October 23, 2007, 04:23:57 PM

Title: Stumped (C#)
Post by: abc on October 23, 2007, 04:23:57 PM
        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? ???
Title: Re: Stumped (C#)
Post by: iago on October 23, 2007, 04:25:47 PM
By making sure the function returns a value in every possible scenario?
Title: Re: Stumped (C#)
Post by: abc on October 23, 2007, 04:27:01 PM
That was fast!; so you mean I need more than one "return ..."?
Title: Re: Stumped (C#)
Post by: Chavo on October 23, 2007, 04:33:34 PM
What happens when you exit the while loop? :)
Title: Re: Stumped (C#)
Post by: abc on October 23, 2007, 04:34:38 PM
The Stream Reader stops reading.
Title: Re: Stumped (C#)
Post by: Sidoh on October 23, 2007, 04:49:35 PM
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.
Title: Re: Stumped (C#)
Post by: iago on October 23, 2007, 06:17:21 PM
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).
Title: Re: Stumped (C#)
Post by: Warrior on October 23, 2007, 06:19:28 PM
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.
Title: Re: Stumped (C#)
Post by: MyndFyre on October 23, 2007, 06:28:23 PM
I believe you can see the error in your ways by examining a logic flow diagram:
Title: Re: Stumped (C#)
Post by: iago on October 23, 2007, 06:33:30 PM
Are you really bored at work? :)
Title: Re: Stumped (C#)
Post by: Warrior on October 23, 2007, 06:52:32 PM
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:


.........
Title: Re: Stumped (C#)
Post by: MyndFyre on October 23, 2007, 07:20:30 PM
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.
Title: Re: Stumped (C#)
Post by: Blaze on October 23, 2007, 08:28:06 PM
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.. :\
Title: Re: Stumped (C#)
Post by: Camel on October 25, 2007, 12:34:26 AM
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.
Title: Re: Stumped (C#)
Post by: abc on October 25, 2007, 02:25:49 PM
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!