471,627 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

Suppression of incorrect warnings.

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
}
}
}

--
http://www.peterRitchie.com/
Feb 4 '06 #1
4 2615
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
Feb 4 '06 #2
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
Feb 4 '06 #3
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

--------------------
Thread-Topic: Suppression of incorrect warnings.
thread-index: AcYpx8QxEtsl1xq2Rqqnic9B+HGjqw==
X-WBNR-Posting-Host: 69.19.14.25
From: "=?Utf-8?B?UGV0ZXIgUml0Y2hpZQ==?=" <PR****@newsgroups.nospam>
Subject: Suppression of incorrect warnings.
Date: Sat, 4 Feb 2006 12:15:39 -0800
Lines: 29
Message-ID: <D7**********************************@microsoft.co m>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGXA03.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.languages.csharp:383468
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

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 codewith #pragma for warnings that are incorrect or don't apply.

For example, the following code generates an CS0628 because CS0628 makes anincorrect 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
}
}
}

--
http://www.peterRitchie.com/


Feb 5 '06 #4
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.

--
http://www.peterRitchie.com/
"Michael Nemtsev" wrote:
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> }


Feb 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Kylotan | last post: by
3 posts views Thread by Terry Richards | last post: by
30 posts views Thread by prasanna | last post: by
22 posts views Thread by John Fisher | last post: by
10 posts views Thread by Duncan Muirhead | last post: by
6 posts views Thread by pete142 | last post: by
3 posts views Thread by gil | last post: by
1 post views Thread by Robert Singer | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.