Force Compiler Error/Warning Within Custom Control

Discussion in 'ASP .Net Building Controls' started by philaphan80@yahoo.com, Oct 6, 2006.

  1. Guest

    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
     
    , Oct 6, 2006
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    >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?


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

    John
     
    John Saunders, Oct 6, 2006
    #2
    1. Advertising

  3. Guest

    > 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.
     
    , Oct 7, 2006
    #3
  4. <> wrote in message
    news:...
    >> 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.


    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, Oct 8, 2006
    #4
  5. <> wrote in message
    news:...
    >> 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.


    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
     
    John Saunders, Oct 9, 2006
    #5
  6. Guest

    > 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.
     
    , Oct 10, 2006
    #6
  7. <> wrote in message
    news:...
    >> 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?


    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
     
    John Saunders, Oct 10, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonah Olsson
    Replies:
    1
    Views:
    590
    Scott Allen
    Apr 5, 2005
  2. J R M
    Replies:
    2
    Views:
    442
    Charlie@CBFC
    Jun 1, 2006
  3. olduncleamos
    Replies:
    1
    Views:
    352
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Mar 10, 2007
  4. Matt Sokol
    Replies:
    2
    Views:
    409
    Theo Tillotson
    Aug 7, 2003
  5. olduncleamos

    Custom controls to emit compiler warning/error?

    olduncleamos, Mar 10, 2007, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    142
    Peter Bromberg [C# MVP]
    Mar 10, 2007
Loading...

Share This Page