no "break" in the get switch of indexer: why?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: no "break" in the get switch of indexer: why?

  1. #1
    antine Guest

    no "break" in the get switch of indexer: why?


    I get a compilation error whenever I put "break" on every case of the get
    switch in an indexer. Why is that? How come the set switch has break's? For
    clarity, please refer to the construct below, assume that A,B are declared
    double in the class.

    public double this [uint Index]
    {
    get
    {
    switch (Index)
    {
    case 0:
    return A;
    break; // error
    case 1:
    return B;
    break; // error
    default:
    // throw exception
    }
    }
    set
    {
    switch (Index)
    {
    case 0:
    A = value;
    break;
    case 1:
    B = value;
    break;
    default:
    // throw exception

    }
    }
    }

    Reversing the order of get/set does not answer my question. tnx



  2. #2
    Eric Gunnerson Guest

    Re: no "break" in the get switch of indexer: why?

    The rule on switches is that control can't fall out of one case into the
    next. Since you have a return in the first two cases, fall-through can't
    happen, and the "break" isn't required.

    --
    This posting is provided "AS IS" with no warranties, and confers no rights.

    "antine" <bira_dayok@yahoo.com> wrote in message
    news:3bfbc83e$1@147.208.176.211...
    >
    > I get a compilation error whenever I put "break" on every case of the get
    > switch in an indexer. Why is that? How come the set switch has break's?

    For
    > clarity, please refer to the construct below, assume that A,B are declared
    > double in the class.
    >
    > public double this [uint Index]
    > {
    > get
    > {
    > switch (Index)
    > {
    > case 0:
    > return A;
    > break; // error
    > case 1:
    > return B;
    > break; // error
    > default:
    > // throw exception
    > }
    > }
    > set
    > {
    > switch (Index)
    > {
    > case 0:
    > A = value;
    > break;
    > case 1:
    > B = value;
    > break;
    > default:
    > // throw exception
    >
    > }
    > }
    > }
    >
    > Reversing the order of get/set does not answer my question. tnx
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center