Link to home
Start Free TrialLog in
Avatar of nate0187
nate0187

asked on

VB.NET: How to throw a compiler error?

Simple question. How do you force the VB compiler to throw a compilation error, like you do in C#?

Example in C#:

You can use a diagnostic directive:

#error Oops. This is an error.

Open in new window

                                 
or for just a warning:

#warning This is just a warning.

Open in new window

Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

I don't know how it can be done in VB but I am curious about your requirement in having this?
Avatar of nate0187
nate0187

ASKER

@emoreau Most of or staff are hard core C# users, a few people still VB. We have to convert code a lot of times for different projects. The #error or #warring doesn't convert from C# to VB, It would be nice to use the same code with out having to modify it.
I agree that it should convert. I was just asking in which scenario these directives are useful.
There is no equivalent. The closest equivalent I can think of is just to write something that won't compile. Like
Oops_this_is_an_error 'Should say something like "Oops_this_is_an_error is not defined"

If you don't use Option Explicit, you could do something like
err=Oops_this_is_an_error

Anyway, the idea is just to write something that will BE an error that has the information you want to communicate in it somewhere.

It's not as clean, but it gets the job done.

We always treat warnings as errors so we don't need the distinction, but you should be able to use the same idea for warnings as well.
@TommySzalapski You are right that example is not clean at all. That is why I like the C# way of sending an error to the compiler freely, without having to throw an exception.
@emoreau These directives are useful are very useful in debugging, and sending errors to the compiler in a programmatic way from the end user on the fly.
ASKER CERTIFIED SOLUTION
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The link was useful.