Question

You can add a preprocessor directive to cause an error at compile time in C# like this:

#error This will cause a divide by zero

How can I do the same as this in vb.net?

or

Is there another way to create an error that provides custom helpful information in the Errorlist.

TLDR: I want to do something like THIS in VB.NET:

photo taken from C# project

Was it helpful?

Solution

Here is one way that you can achieve what you want. It is not perfect. But it does meet your criteria of:

  • Prevents compiling
  • Puts Custom text in the Error List Window

You first need to declare a variable with the custom text you want displayed with underscores in between each word. (Yes the underscores are annoying but they are necessary)

Dim This_is_as_useful_a_description_as_your_gonna_get as String

This creates an unused variable. which in conjunction with the IDE's ability to treat warnings as errors will give you something close to what you are looking for:

enter image description here

You can turn on treat warnings as errors by going to your project properties and the compile tab. like so:

enter image description here

OTHER TIPS

There is no equivalent to

#error 

in VB.Net

I have found no way to add a meaningful error to prevent compiling.

You can also declare a var with sure compiler error such as:

Dim error As String = "This is as useful a description as your gonna get"

thus avoiding all the underscores. That will result automatically in a compiling error ("Keyword is not valid as an identifier"), not a warning.

Example:

#if _A_DEPRECATED_DEFINE_
DIM error As String = "remove the define: is deprecated!"
#end if
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top