9

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

jth41
  • 3,808
  • 9
  • 59
  • 109
  • 3
    Rather than just downvotes. Comments of what I need to clarify would be nice... – jth41 Jul 31 '13 at 13:30
  • 1
    Not seeing any directive that does this. Here's the MSDN page for vb.net directives: http://msdn.microsoft.com/en-us/library/7ah135z7.aspx – Lynn Crumbling Jul 31 '13 at 13:35
  • [This answer](http://stackoverflow.com/questions/5582331/is-ever-legal-in-c-sharp-or-vb-net#5582704) seems pretty sure there isn't any equivalent. – xlecoustillier Jul 31 '13 at 13:36
  • Not sure why there's downvotes and a close vote for being "unclear". This is actually a good, clear, concise question... try googling it, I haven't found anything. – tnw Jul 31 '13 at 13:37
  • To follow @LynnCrumbling idea, comparing [VB directives](http://msdn.microsoft.com/en-us/library/7ah135z7.aspx) and [C# directives](http://msdn.microsoft.com/en-us/library/ed8yd1ha%28v=vs.110%29.aspx) leaves no real doubt about the answer... – xlecoustillier Jul 31 '13 at 13:39
  • 2
    @tnw Agreed -- I can't find anywhere that someone came up with a workable solution for this. There simply is no directive. The best you're going to be able to do, is just type text that doesn't compile... maybe something like: `#if DEBUG (newline) DON'T EVER COMPILE DEBUG BUILDS! (newline) #end if` – Lynn Crumbling Jul 31 '13 at 13:44

3 Answers3

9

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

Jeremy Lin
  • 400
  • 3
  • 13
1

There is no equivalent to

#error 

in VB.Net

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

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
jth41
  • 3,808
  • 9
  • 59
  • 109
1

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
Fil
  • 1,032
  • 13
  • 29
  • 1
    This does cause a compiler error, but the OP wanted the error text to appear in the Error List window in Visual Studio (not to simply produce a compiler error, which could be done in a number of ways). – Caleb Bell Mar 29 '16 at 16:32