News:

Facebook killed the radio star. And by radio star, I mean the premise of distributed forums around the internet. And that got got by Instagram/SnapChat. And that got got by TikTok. Where the fuck is the internet we once knew?

Main Menu

Stumped (C#)

Started by abc, October 23, 2007, 04:23:57 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

abc

        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? ???

iago

By making sure the function returns a value in every possible scenario?

abc

That was fast!; so you mean I need more than one "return ..."?

Chavo

What happens when you exit the while loop? :)

abc

The Stream Reader stops reading.

Sidoh

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.

iago

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).

Warrior

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.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

MyndFyre

I believe you can see the error in your ways by examining a logic flow diagram:
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

iago

Are you really bored at work? :)

Warrior

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:


.........
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

MyndFyre

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: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Blaze

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.. :\
And like a fool I believed myself, and thought I was somebody else...

Camel

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.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

abc

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!