Solved

Create a compile-time error?

Posted on 2006-07-07
7
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

710 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