Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / October 2006

Tip: Looking for answers? Try searching our database.

Force Compiler Error/Warning Within Custom Control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
philaphan80@yahoo.com - 06 Oct 2006 21:31 GMT
I assume there's a way to do this since I've seen third-party
components act the same way.

How do I force the compiler to throw an error or warning message during
compile or build?  For instance, if a certain property of my custom
component isn't set, how can I force my compile to fail -- and force me
to fix it before compiling successfully?

I've added some pseudo-code below for reference.........

Public Class MyClass
   Inherits Control

   Private _TestString As Label

   <Browsable(True)> _
   Public Property TestString() As String
       Get
           Return _TestString
       End Get
       Set(ByVal value As String)
           _TestString = value
       End Set
   End Property

   <pseudo-code>

   If Me.TestString = "" Then

       <This is where I'd want to throw the error during compilation,
forcing me to set TestString in the Properties window before I can
compile.......>

   End If

   </pseudo-code>

End Class
John Saunders - 06 Oct 2006 22:31 GMT
>I assume there's a way to do this since I've seen third-party
> components act the same way.
[quoted text clipped - 3 lines]
> component isn't set, how can I force my compile to fail -- and force me
> to fix it before compiling successfully?

How would the compiler know if the property wasn't set? That happens at
run-time.

John
philaphan80@yahoo.com - 07 Oct 2006 16:42 GMT
> How would the compiler know if the property wasn't set? That happens at
> run-time.

I'm not sure.  That's what I'm trying to work through in my head at the
moment.

I've evaluated components that throw a compiler error if, say, an
evaluation period has expired or a registration key is missing, etc.
So I thought, maybe they're using some kind of coding technique that I
could utilize within my control.

As it stands right now, my control is working pretty well for my needs.
It displays an error within its Text property (at design time and run
time) if a property isn't set correctly.  I just figured I'd challenge
myself by trying to force a compiler error.  That way, I'd know there's
a problem before the project was even built.

Thoughts?  Anyone?  They're always gratefully appreciated.
John Saunders - 08 Oct 2006 03:26 GMT
>> How would the compiler know if the property wasn't set? That happens at
>> run-time.
[quoted text clipped - 14 lines]
>
> Thoughts?  Anyone?  They're always gratefully appreciated.

I was gently trying to suggest that there is fundamentally no way to do what
you're asking for.

First of all, please define "property wasn't set". Do you mean "property
wasn't set during initialization"? If so, define "initialization", realizing
that you have to define it in a way that you can explain to a compiler.
You'll also have to make the decision to rule out "setting" the property by
some method which is not "initialization".

This would be interesting, since any definition of run-time initialization
is going to have to be understood by the compiler in compile-time terms. For
instance, if you defined "initialization" as "set by an unconditional
statement in the InitializeComponent method", then the compiler could
understand that. It could generate a (hidden) try-catch-finally block around
the InitializeComponent method and check to see if the property had been
"set".

My advice is to punt on this until you find that some other piece of code
has done this.  You really are mistaken about the inherent difficulty in
this.

John
John Saunders - 09 Oct 2006 01:58 GMT
>> How would the compiler know if the property wasn't set? That happens at
>> run-time.
[quoted text clipped - 14 lines]
>
> Thoughts?  Anyone?  They're always gratefully appreciated.

Things are different today than they were 30 years ago when I learned the
difference between compile-time and run-time, so I gave this some more
thought.

The one situation in which I know of a compiler having anything at all to do
with something we typically thing of as runtime is in the case of attributes
in .NET.  When we do something like the following:

[WebService]
public class SomeService
{
}

then we find something like the following in the IL:

 .custom instance void
[System.Web.Services]System.Web.Services.WebServiceAttribute::.ctor() = (
... )

So, I tried placing a throw new Exception("Boom!")  in the constructor of an
attribute I use. No boom.

Are you sure you didn't see these license things happen at design-time and
not runtime?

I suggest you limit yourself to runtime validation of property values. See
the ISupportInitialization interface in the documentation.

John
philaphan80@yahoo.com - 10 Oct 2006 02:33 GMT
> Things are different today than they were 30 years ago when I learned the
> difference between compile-time and run-time, so I gave this some more
> thought.

John,

First, let me thank you for taking time out of your schedule to help me
with this.  I appreciate it.

One of your last comments worries me a bit, though.  I'm afraid I may
have misled you somehow.

> Are you sure you didn't see these license things happen at design-time and
> not runtime?

Actually, that was what I was trying to get at.  I'm sorry if I said
anything inaccurate that may have led you to draw the wrong conclusion.

I'm basically looking for the action (error/warning) to take place
during compile-time, if there is such a thing.  So this would be
between design-time and run-time, when I have manually chosen to
compile (or build) my project.  I'm hoping to produce a compile-time
error within the IDE, similar to the ones that force you to fix them
before recompiling (e.g. paraphrased: "The string 'test' cannot be
converted to type Integer -- testing.vb: line 47").  That way, I
wouldn't have to worry about run-time at all.  I'd have to fix my error
before the project even successfully compiles.

Does that help clear things up at all?

Thanks again.
John Saunders - 10 Oct 2006 12:57 GMT
>> Things are different today than they were 30 years ago when I learned the
>> difference between compile-time and run-time, so I gave this some more
[quoted text clipped - 26 lines]
>
> Does that help clear things up at all?

It does. It can't be done, and probably shouldn't be done.

The compiler couldn't possibly know, for instance, that the property on your
control is being set by a piece of code in another assembly not in the
solution.

The right way to do this is at runtime, using ISupportInitialization.

John

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.