无法将“默认”设置为切换块

时间:2013-11-16 14:26:48

标签: c# switch-statement

我看过类似的问题和答案,但我仍然无法在代码中看到问题。我有一个switch语句,这里是:

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:
                {

                }
        }

问题是,默认块产生错误,说

  

控件不能从一个案例标签('default:')转到另一个

任何人都可以帮我吗?

由于

2 个答案:

答案 0 :(得分:5)

您还应为break;案例添加default

答案 1 :(得分:3)

每个switch案例都必须包含transfer control [breakreturnthrow],因为它不允许fall through。< / p>

break案例添加default(asper your code)beow:

default:
        {

        }
break;