Suppression of incorrect warnings.

G

Guest

Does anyone know how to suppress a specific warning for a line or block of
code in C#? C++ has a nice facility to disable a warning for a block of code
with #pragma for warnings that are incorrect or don't apply.

For example, the following code generates an CS0628 because CS0628 makes an
incorrect assumption that "protected" applies only to inheritance:

public sealed class Class
{
EmbeddedClass utility = new EmbeddedClass();

protected static void StaticFunction() // CS0628
{
}
protected void InstanceMethod() // CS0628
{
}
private class EmbeddedClass
{
public EmbeddedClass()
{
Class.StaticFunction(); // call to "uncallable" function
new Class().InstanceMethod(); // call to "uncallable" function
}
}
}
 
M

Michael Nemtsev

Hello Peter,

What's the reason to use protected members in that case?

PR> Does anyone know how to suppress a specific warning for a line or
PR> block of code in C#? C++ has a nice facility to disable a warning
PR> for a block of code with #pragma for warnings that are incorrect or
PR> don't apply.
PR>
PR> For example, the following code generates an CS0628 because CS0628
PR> makes an incorrect assumption that "protected" applies only to
PR> inheritance:
PR>
PR> public sealed class Class
PR> {
PR> EmbeddedClass utility = new EmbeddedClass();
PR> protected static void StaticFunction() // CS0628
PR> {
PR> }
PR> protected void InstanceMethod() // CS0628
PR> {
PR> }
PR> private class EmbeddedClass
PR> {
PR> public EmbeddedClass()
PR> {
PR> Class.StaticFunction(); // call to "uncallable" function
PR> new Class().InstanceMethod(); // call to "uncallable"
PR> function
PR> }
PR> }
PR> }
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
M

Michael Nemtsev

Hello Peter,

You can use
#pragma warning disable 0628
#pragma warning restore
at least


PR> Does anyone know how to suppress a specific warning for a line or
PR> block of code in C#? C++ has a nice facility to disable a warning
PR> for a block of code with #pragma for warnings that are incorrect or
PR> don't apply.
PR>
PR> For example, the following code generates an CS0628 because CS0628
PR> makes an incorrect assumption that "protected" applies only to
PR> inheritance:
PR>
PR> public sealed class Class
PR> {
PR> EmbeddedClass utility = new EmbeddedClass();
PR> protected static void StaticFunction() // CS0628
PR> {
PR> }
PR> protected void InstanceMethod() // CS0628
PR> {
PR> }
PR> private class EmbeddedClass
PR> {
PR> public EmbeddedClass()
PR> {
PR> Class.StaticFunction(); // call to "uncallable" function
PR> new Class().InstanceMethod(); // call to "uncallable"
PR> function
PR> }
PR> }
PR> }
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
T

TerryFei

Hi,
Welcome to MSDN Newsgroup!

There is a " #pragma warning" C# Preprocessor Directive in C# 2.0 , we
could use it to disable specified warning. You could refer to the following
article:
Title: #pragma warning (C# Reference)
URL:http://msdn2.microsoft.com/441722ys.aspx

I hope the above information is helpful for you. Thanks and have a nice day!

Best Regards,

Terry Fei [MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security



--------------------
 
G

Guest

Sorry, somehow I got in my head that in-line classes could only access
protected members of the containing class.

Ignore me. Switching to private from protected gets rid of the warning and
avoids the inheritance implication.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top