Solved

Create a compile-time error?

Posted on 2006-07-07
7
245 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

11 Experts available now in Live!

Get 1:1 Help Now