i'm having a syntax error when trying to use switch condition:
control cannot fall through from one case label to another...

my code is

public override Boolean intersect(Shape s)
        {
            switch (s.GetType().Name)
            {
                case "RealPoint":
                   {
                        s = (RealPoint)s;

                        if (!(this.gspos > s.gspos) && !(this.gspos < s.gspos))
                            return true;
                    }

                case "Segment":
                    {
                        Size tsize;
                        tsize = s.gspos - this.gspos;
                        s = (Segment)s;

                        if (tsize.gsy == 0 && this.gspos.pgsx >= s.gspos.pgsx && this.gspos.pgsx <= (s.gspos.pgsx + s.gsize.gsx))
                            return true;
                    }

            }
                    return false;
        }

how can i fix ?

Recommended Answers

All 2 Replies

The reason is that the compiler can see that there is the possibility that the condiotnal statement could be false, and therefore it can fall through to the next case statement.. which is illigal. So to fix the problem, always be sure that the case either returns, or does a break; as the last statement in each case statement.

i added a break and now it is ok.
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.