Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Controlling Compiler Warnings
 

Controlling Compiler Warnings

I want to switch on or switch off certain compiler warnings likein VB.NET 2.0 like we are able to do it for C#.

I want to toggle these warnings either at the VB file level (i.e. for the complete code file), or for a single statement in a file.
Is there any way to do it???

Rahul Singla  Monday, September 15, 2008 7:17 AM
Okay, after some more searching, I found out that C# type warning control for compilers (through the pragma directive is not available in VB.NET 2005???

Is it available in VB.NET 2008???
Rahul Singla  Monday, September 15, 2008 7:26 AM

Hi Rahul Singla
please read this article which could help you.

How to: Enable or Disable Compiler Warnings
http://msdn.microsoft.com/en-us/library/edzzzth4.aspx

The Visual Basic compiler can detect potential problems in your code and issue warnings so that you can review them. By default, all compiler warnings are added to the Task List during compilation. You can change the behavior of the compiler so that it does not issue warnings, or so that it will treat warnings as compilation errors that must be fixed immediately.

How to: Set Compiler Options
http://msdn.microsoft.com/en-us/library/c553zwz0.aspx
The Visual Basic compiler provides several options for checking your code at compile time. Option Explicit determines whether variables must be explicitly declared. Option Strict determines whether explicit narrowing conversions and late binding are allowed. Option Infer enables type inference for member-level (local) variables. Option Compare specifies the method that is used for string comparisons: binary (case-sensitive) or text (case-insensitive).

If you have any issue, please feel free to tell us.
best wish
xingwei


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Xingwei Hu  Friday, September 19, 2008 11:06 AM
I was NOT talking about those Project Options. Those are the ones I have used regularly before, and am aware of.

What I was talking about was C# like functionality. You know those options do not list all possible warnings the compiler can generate. C# allows to turn on/off any warning with a Warning code. Is that possible in VB??

And more importantly for me is the ability to selectively switch off a warning for a code file or a particular function.
If I turn-off these warnings from the Project Properties dialog, they get turned off for the entire project.

However, I believe everyone (including me) faces situation, where he/she is forced to do a task that generates a warning (like making a late-bound call). However, these situations occur once in a while. If I turn-off a warning from Project property, this turns it off for the entire project, making it hard to figure out coding goof-ups.

However, the ability to switch off for a file or function would allow these goof-ups to be caught, while not showing any warning for a situation where you are sure you actually wanted to do this.

I have a Library project, which I reference from many of my Application projects. That library project is large, and makes only 2 late bound calls in the entire project. As a result, all Application projects always show warnings. What I want is to simply eliminate them, without turning it off for the entire library project, as any mistake in that project would cascade as bugs to multiple application projects.

I believe C# allows this with a pragma directive. Is it possible in VB???
Rahul Singla  Friday, September 19, 2008 2:30 PM
VB is not C#.

You can't turn of the warnings selectively, because of the way VB operates it isn't really necessary. Are you saying that a DLL with a late bound call generates a warning in your calling application?


Stephen J Whiteley
SJWhiteley  Saturday, September 20, 2008 3:39 AM
SJWhiteley said:

Are you saying that a DLL with a late bound call generates a warning in your calling application?


Stephen J Whiteley



Its not DLL. I am referencing the Library project itself in the Application solution, and that's why the warnings are being generated. How can a referenced DLL generate late bound call warnings in an application???
Rahul Singla  Saturday, September 20, 2008 5:37 AM
Far be it from me to state the obvious, but just code that part in c#.
WaywardMage  Saturday, September 20, 2008 5:48 AM
Rahul Singla said:

SJWhiteley said:

Are you saying that a DLL with a late bound call generates a warning in your calling application?


Stephen J Whiteley



Its not DLL. I am referencing the Library project itself in the Application solution, and that's why the warnings are being generated. How can a referenced DLL generate late bound call warnings in an application???


Well, it wasn't exactly clear what you were trying to do. I've never once come across the need to do what you want in VB (C++, sure). It wasn't clear why you were trying to coerce VB to do something native to the C-style languages, when you have C# readily available.

As WaywardMage says - Do it in C#.


Stephen J Whiteley
SJWhiteley  Monday, September 22, 2008 10:27 AM
I had a situation where I needed to make a late-bound call. This generated an error or warning depending upon the setting in the Visual Basic Project Properties dialog.

However, I did not want to switch off this option entirely, as it helps in finding out subtle errors that can generate Runtime exceptions.
I wanted VB to ignore the Warning just this one time, as I was sure here that this runtime bound call would not generate an Exception.

As the major part of the assembly has already been coded in VB, moving to C# is NOT an option. There are internal variables and other issues that would arise on moving to a new Assembly.

I expected to here the Solution here, but anyhow I found it myself while playing around. You can still control Compiler warnings in a VB project at a Source Code file level.

So, what I have done is to have the Compiler warnings on in the Project Properties dialog.

But I have placed an
Option Strict Off

directive at the beginning of the Source Code file where the late bound call is being made. This way I have the compiler watch out for my Goof-ups but ask it not to do so, where I was sure about what I was doing.

In a nut-shell, just place an
Option -- Off

directive at the beginning of the Source Code file, if you want Compiler to ignore some situations for only a part of the whole assembly.
Rahul Singla  Tuesday, October 07, 2008 6:58 AM

You can use google to search for other answers

Custom Search

More Threads

• Webbrowser control copy problem
• execution of vb.net statements stored in variables at runtime
• How is this OO Design?
• How do I write a program for checking pi?
• starting interactive process from windows service
• How to recognize shapes and characters in image
• VB.NET Datagrid - Easy way to change selected row based on user key press?
• radiobuttons don't function as expected on forms that have databound fields
• Copy DataGridViewRow Form-To the same DataGridView
• Speaker beep code