c# - Unreachable code detected by using const variables -
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 code unreachable, don't want message appear, since it's configuration on compilation needs change in constant test different settings, , bits per pixel (bpp) change depending on pixel format. there way have 1 variable being constant, deriving other it, not resulting in unreachable code warning? note need both values, on start of camera needs configured proper pixel format, , image understanding code needs know how many bits image in.
so, there workaround, or live warning?
you can replace conditional dictionary
lookup avoid 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"); }
Comments
Post a Comment