Generate a compiler error?  
Author Message
weirdbeardmt





PostPosted: Visual Basic Language, Generate a compiler error? Top

Hi all,

I've created a custom component which can be dropped from the toolbox to any form. It interacts with any form that has a treelist on it. After you drop it on to the form, you must set a property of the component which is treelist it applies to. At the moment if you forget to set the property, the app builds, but as soon as you attempt to use the component, you get a nullreferenceexception.

What I'd like to know is whether it's possible to force a compiler/build error if that property has not been set at compile time i.e., if one of our other developers uses the component on his form but forgets to set the property in design mode, then I'd like it to not build and show an error message in the error list.

This is VB.NET in VS2005 on v2.0.

Any thoughts

Thanks.

 




Visual Basic4  
 
 
DMan1





PostPosted: Visual Basic Language, Generate a compiler error? Top

If IsNothing(MyProperty) then

Throw New System.Exception("An exception has occurred.")


End If



 
 
weirdbeardmt





PostPosted: Visual Basic Language, Generate a compiler error? Top

That's not what I want to do, I know I can throw an exception.

What I'm asking is whether I can create say "custom error messages" or similar which will appear in the standard error list window and cause a build failure.


 
 
spotty





PostPosted: Visual Basic Language, Generate a compiler error? Top

You cant create custom compiler errors - these are coded into the compiler.  There is no way for the user to generate there own compiler errors .

 

But if you want to create specific exceptions in you application at runtime then you can create yourself a custom exception which you can use.

http://www.developer.com/net/vb/article.php/3590931

http://msdn.microsoft.com/msdnmag/issues/02/11/NETExceptions/

Basically create your own exception class and inherit from system.exception

 


 
 
Squire James





PostPosted: Visual Basic Language, Generate a compiler error? Top

I think the best you can do is add code to check for it within the Load event of your main form, and exit application if it is not set. You still waste the build time, but at least it fails early.

I suppose Visual Studio may have some fancy feature to set up a custom compiler error, but unless you plan on writing a LOT of programs with this control I doubt it would be worth the time to set it up. I suspect something like this is possible, though.


 
 
nobugz





PostPosted: Visual Basic Language, Generate a compiler error? Top

Perhaps another approach is to make the component aware of it being manipulated in the designer. You could make it obvious to the programmer that the component is not yet ready to be used in the finished program. Or automatically find a suitable treeview in the form. To do this, you need to implement IComponentChangeService interface. Look here for an example.



 
 
spotty





PostPosted: Visual Basic Language, Generate a compiler error? Top

You cant stop the build but you can put in 'TODO comments which will show up in the task list.


 
 
weirdbeardmt





PostPosted: Visual Basic Language, Generate a compiler error? Top

Thanks for the tips. I had this idea when writing this particular component, but there are loads of custom controls and components in our solution(s) so if it were easy to create then we'd use them/it all over. I'll keep digging, but it seems an exception is about the best I'll manage. Thanks.