Author Topic: Stumped (C#)  (Read 4339 times)

0 Members and 1 Guest are viewing this topic.

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Stumped (C#)
« on: October 23, 2007, 04:23:57 pm »
Code: [Select]
        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? ???

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Stumped (C#)
« Reply #1 on: October 23, 2007, 04:25:47 pm »
By making sure the function returns a value in every possible scenario?

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: Stumped (C#)
« Reply #2 on: October 23, 2007, 04:27:01 pm »
That was fast!; so you mean I need more than one "return ..."?

Offline Chavo

  • x86
  • Hero Member
  • *****
  • Posts: 2219
  • no u
    • View Profile
    • Chavoland
Re: Stumped (C#)
« Reply #3 on: October 23, 2007, 04:33:34 pm »
What happens when you exit the while loop? :)

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: Stumped (C#)
« Reply #4 on: October 23, 2007, 04:34:38 pm »
The Stream Reader stops reading.

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Stumped (C#)
« Reply #5 on: October 23, 2007, 04:49:35 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.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Stumped (C#)
« Reply #6 on: October 23, 2007, 06:17:21 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).

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: Stumped (C#)
« Reply #7 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:

Code: [Select]
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.

Code: [Select]
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

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Stumped (C#)
« Reply #8 on: October 23, 2007, 06:28:23 pm »
I believe you can see the error in your ways by examining a logic flow diagram:
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Stumped (C#)
« Reply #9 on: October 23, 2007, 06:33:30 pm »
Are you really bored at work? :)

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: Stumped (C#)
« Reply #10 on: October 23, 2007, 06:52:32 pm »
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

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Stumped (C#)
« Reply #11 on: October 23, 2007, 07:20: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.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: Stumped (C#)
« Reply #12 on: October 23, 2007, 08:28:06 pm »
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...

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Stumped (C#)
« Reply #13 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.

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

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: Stumped (C#)
« Reply #14 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!