Solved

Create a compile-time error?

Posted on 2006-07-07
7
246 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
ID: 17058054
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
ID: 17058056
Throw an exception:
   throw new ArgumentException("Argument invalid");

Bob
0
 
LVL 5

Accepted Solution

by:
bastibartel earned 250 total points
ID: 17058287
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 11

Assisted Solution

by:vo1d
vo1d earned 250 total points
ID: 17058920
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
ID: 17060439
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
ID: 17060559
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
ID: 17061628
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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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