Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Error with custom text that prevents compiling in VB.NET (#error in C#)

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

like image 440
jth41 Avatar asked Jul 31 '13 13:07

jth41


People also ask

How do I get Visual Studio to show errors?

To display the Error List, choose View > Error List, or press Ctrl+\+E.

How do I resolve build errors in Visual Studio?

In Visual Studio, go to the menu bar and choose Help > Send Feedback > Report a Problem, or submit a suggestion by using Help > Send Feedback > Send a Suggestion.

What is Run Time Error in VB?

Run-time errors are those that appear only after you compile and run your code. These involve code that may appear to be correct in that it has no syntax errors, but that will not execute. For example, you might correctly write a line of code to open a file.


3 Answers

There is no equivalent to

#error 

in VB.Net

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

like image 44
jth41 Avatar answered Oct 14 '22 15:10

jth41


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
like image 40
Fil Avatar answered Oct 14 '22 16:10

Fil


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

like image 135
Jeremy Lin Avatar answered Oct 14 '22 16:10

Jeremy Lin