Unreachable code detected by using const variables
Asked Answered
F

4

6

I have following code:

private const FlyCapture2Managed.PixelFormat f7PF = FlyCapture2Managed.PixelFormat.PixelFormatMono16;

public PGRCamera(ExamForm input, bool red, int flags, int drawWidth, int drawHeight) {
   if (f7PF == FlyCapture2Managed.PixelFormat.PixelFormatMono8) {
      bpp = 8;  // unreachable warning
   }
   else if (f7PF == FlyCapture2Managed.PixelFormat.PixelFormatMono16){
      bpp = 16;
   }
   else {
      MessageBox.Show("Camera misconfigured");  // unreachable warning
   }
}

I understand that this code is unreachable, but I don't want that message to appear, since it's a configuration on compilation which just needs a change in the constant to test different settings, and the bits per pixel (bpp) change depending on the pixel format. Is there a good way to have just one variable being constant, deriving the other from it, but not resulting in an unreachable code warning? Note that I need both values, on start of the camera it needs to be configured to the proper Pixel Format, and my image understanding code needs to know how many bits the image is in.

So, is there a good workaround, or do I just live with this warning?

Fizgig answered 1/7, 2013 at 10:15 Comment(1)
Not sure the overall approach is the best one, but you could use a switch statement and have the error in the default: block.Lamellicorn
H
2

You can replace the conditional with a Dictionary lookup to avoid the warning:

private static IDictionary<FlyCapture2Managed.PixelFormat,int> FormatToBpp =
    new Dictionary<FlyCapture2Managed.PixelFormat,int> {
        {FlyCapture2Managed.PixelFormat.PixelFormatMono8, 8}
    ,   {FlyCapture2Managed.PixelFormat.PixelFormatMono16, 16}
    };
...
int bpp;
if (!FormatToBpp.TryGetValue(f7PF, out bpp)) {
    MessageBox.Show("Camera misconfigured");
}
Hooves answered 1/7, 2013 at 10:21 Comment(1)
This may not be the most efficient one, but it sure is the most elegant one.Fizgig
M
11

The best approach would be to disable the warning at the top of the file:

#pragma warning disable 0162

An alternative is converting your const into a static readonly.

private static readonly FlyCapture2Managed.PixelFormat f7PF = 
                        FlyCapture2Managed.PixelFormat.PixelFormatMono16;

However, if performance is important for your code, I would suggest keeping it a const and disabling the warning. Although const and static readonly are functionally equivalent, the former allows better compile-time optimizations that might be otherwise lost.

Maytime answered 1/7, 2013 at 10:18 Comment(1)
I second the static readonly option. Performance in this case wouldn't seem to be an issue.Chatter
R
6

For reference, you can turn it off via:

#pragma warning disable 162

..and re-enable with:

#pragma warning restore 162
Retrorse answered 1/7, 2013 at 10:19 Comment(0)
H
2

You can replace the conditional with a Dictionary lookup to avoid the warning:

private static IDictionary<FlyCapture2Managed.PixelFormat,int> FormatToBpp =
    new Dictionary<FlyCapture2Managed.PixelFormat,int> {
        {FlyCapture2Managed.PixelFormat.PixelFormatMono8, 8}
    ,   {FlyCapture2Managed.PixelFormat.PixelFormatMono16, 16}
    };
...
int bpp;
if (!FormatToBpp.TryGetValue(f7PF, out bpp)) {
    MessageBox.Show("Camera misconfigured");
}
Hooves answered 1/7, 2013 at 10:21 Comment(1)
This may not be the most efficient one, but it sure is the most elegant one.Fizgig
P
1

It is possible, just add

#pragma warning disable 0162

before your field. To restore put this at end

#pragma warning restore 0162. More info here MSDN

Phosphorite answered 1/7, 2013 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.