1

I have looked at the similar questions and answers, but i still cannot see the problem in my code. I have a switch statement and here it is:

switch (header)
        {
            case Headers.Queue:
                {
                    int id = pr.ReadInt32();
                    string fileName = pr.ReadString();
                    long length = pr.ReadInt64();

                    TransferQueue queue = TransferQueue.CreateDownloadQueue(this, id, Path.Combine(downloadDirectory,
                        Path.GetFileName(fileName)), length);

                    _transfers.Add(id, queue);

                    if (Queued != null)
                    {
                        Queued(this, queue);
                    }
                }
                break;
            case Headers.Start:
                {
                    int id = pr.ReadInt32();

                    if (_transfers.ContainsKey(id))
                    {
                        _transfers[id].Start();
                    }
                }
                break;
            case Headers.Chunk:
                {
                    int id = pr.ReadInt32();
                    long index = pr.ReadInt64();
                    int size = pr.ReadInt32();
                    byte[] buffer = pr.ReadBytes(size);

                    TransferQueue queue = _transfers[id];

                    queue.Write(buffer, index);

                    queue.Progress = (int)((queue.Transferred * 100) / queue.Length);
                    if (queue.LastProgress < queue.Progress)
                    {
                        queue.LastProgress = queue.Progress;

                        if (ProgressChanged != null)
                        {
                            ProgressChanged(this, queue);
                        }

                        if (queue.Progress == 100)
                        {
                            queue.Close();

                            if (Complete != null)
                            {
                                Complete(this, queue);
                            }
                        }
                    }
                }
                break;
            default:
                {

                }
        }

The problem is, the default block produces an error saying that

Control cannot fall through from one case label ('default:') to another

Can anyone help me with this?

Thanks

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
yrazlik
  • 10,411
  • 33
  • 99
  • 165
  • 1
    take a look at how the switch statement works http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.90).aspx – Ric Nov 16 '13 at 14:29
  • 1
    Add a break for the default statement – MKS Nov 16 '13 at 14:29
  • possible duplicate of [Switch statement fallthrough in C#?](http://stackoverflow.com/questions/174155/switch-statement-fallthrough-in-c) – Sriram Sakthivel Nov 16 '13 at 14:31
  • I suggest that you refactor the code into a switch which simply calls a different method for each case. That switch itself is over 60 lines long. This will make it easier for you to debug and maintain. Also, refactoring is fun! If you don't want to pollute you class namespace with three additionally functions, you can use nested functions or a dispatch dictionary. – Aluan Haddad Nov 16 '13 at 15:07

2 Answers2

5

You should add break; for the default case as well.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
3

Each switch case is required to have transfer control [break,return,throw] as it does not allow fall through.

add break (asper your code) for your default case too as beow:

default:
        {

        }
break;
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67