Solved

Create a compile-time error?

Posted on 2006-07-07
7
244 Views
Last Modified: 2008-02-01
Is there a way to generate a compile-time error in .NET?   Here's the situation:  I'd like to generate a compile-time error when a person drops a control onto one of the Winforms in my project and does NOT add that control to my "internal controls" collection.    When they try to compile I'd like to issue an error stating "txtCustomer is not a member of myInternalControlsCollection - please see InternalControls.cs for instructions".    Is this possible?   Alternative solutions would be appreciated if it's not.  Thanks!
0
Comment
Question by:nespa
7 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
How would your code know that it had not been added? In this particular case I am curious why you are not just going to the main container and reading controls directly as they are being added to the container automatically (or they are not working).
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Throw an exception:
   throw new ArgumentException("Argument invalid");

Bob
0
 
LVL 5

Accepted Solution

by:
bastibartel earned 250 total points
Comment Utility
Hi nespa,

If it is supposed to be a compile time error:

#ifdef NO_GOOD
#error "This is no good - man"
#endif

Now the question is, do you have a way of telling if it is "no good" at compile.
Are their any defines that are being set via the Winform. I don't know.
 
If not, than you'll have to throw an exception - at runtime,of course.

Cheers!
Sebastian
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 11

Assisted Solution

by:vo1d
vo1d earned 250 total points
Comment Utility
you can use an attribut for the other collection, which shall not been used instead of your collection:

[System.Obsolete("Use the ...collection instead", true)]

this attribut has to be defined above the other collection property, so maybe you will have to override that collection property and place that attribut there.

on compile time, it will produce an error.
0
 
LVL 1

Author Comment

by:nespa
Comment Utility
Experts,

Thanks for your answers so far.  I should have been more clear in my question.  The first part was answered by bastibartel/Sebastian, so I'll make sure to give points to him.  

After researching more, I'm not sure what I'm looking to do is possible -- initially I figured there might be a way to tweak the preprocessor directives or something (#if, etc.) and/or specify a compilation cmd ("on compile, run this cmd...") to iterate thru all controls on my Winform, find any that [aren't tagged for example; myExistingTextbox.Tag = "alreadyAdded"] and throw a compile-time error (instead of at runtime - which I was trying to avoid).

If no one has any other ideas I'll divvy up points to the best options - so far bastibartel & vo1d.

Thanks for your help on this - let me know if I should clarify further.
0
 
LVL 11

Expert Comment

by:vo1d
Comment Utility
the Obsolete attribut is for compiling warnings or errors, depending on the bool flag.
false is warning, true is compile error.
0
 
LVL 1

Author Comment

by:nespa
Comment Utility
All, thanks for your comments and ideas - I'm not sure it can be done; again, I figured I could use preprocessor directives or other compilation conditions to test for valid controls on the form but everything I tried doesn't work.  

I'll award points to the two answers that gave the best solutions, since they do solve the first part of what I was trying to do.  
Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now