Force Compiler Error/Warning Within Custom Control

P

philaphan80

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
 
J

John Saunders

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
 
P

philaphan80

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.
 
J

John Saunders

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
 
J

John Saunders

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
 
P

philaphan80

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.
 
J

John Saunders

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.


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top