Solved

Create a compile-time error?

Posted on 2006-07-07
7
249 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

626 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