By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

creating a compile time error... is it possible?

P: n/a
Hi,
I'd like to create a compile time error in my class... maybe there's a way
already built in in the framework so I can achieve what I want... I have 2
constructors in my class. One of them has mandatory parameters, I mean, they
should not be null nor empty (for strings). So I'd make the validation in
the constructor and generate a compile-time error if the validation does not
match...

Is there a way to achieve this or to specify mandatory parameters? (I'm
using C#)

thanks

ThunderMusic

Aug 21 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
ThunderMusic,

That's easy, just create a syntax error, forget a semi-colon somewhere.

I think what you really want is a run-time error. If you have a
compile-time error, then your code doesn't compile, and it certainly doesn't
run.

Basically, what you want to do is throw an exception. In the case of an
argument being null, you want to do something like this:

public class MyClass
{
public MyClass()
{
}

public MyClass(SomeOtherClass other)
{
// If other is null, throw an exception.
if (other == null)
{
// Throw an exception.
throw new ArgumentNullException();
}
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:uT**************@TK2MSFTNGP03.phx.gbl...
Hi,
I'd like to create a compile time error in my class... maybe there's a
way already built in in the framework so I can achieve what I want... I
have 2 constructors in my class. One of them has mandatory parameters, I
mean, they should not be null nor empty (for strings). So I'd make the
validation in the constructor and generate a compile-time error if the
validation does not match...

Is there a way to achieve this or to specify mandatory parameters? (I'm
using C#)

thanks

ThunderMusic

Aug 21 '06 #2

P: n/a
What I wanted to do is to have the user (developer) of my class have a
compile-time error when it supplies null as the value of a parameter when he
compiles.... like myMethod(param1, null, param3,...) so param2 should
generate a compile-time error because it's explicitly null... Is there a
way to make this validation or I must go with the NullReferenceException?
Sure, runtime exceptions will have to be implemented because param1 or
param3 could also be null without being explicit, but as a first time
validation, I would like to implement compile-time error if possible...

thanks

ThunderMusic
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:e%****************@TK2MSFTNGP05.phx.gbl...
ThunderMusic,

That's easy, just create a syntax error, forget a semi-colon somewhere.

I think what you really want is a run-time error. If you have a
compile-time error, then your code doesn't compile, and it certainly
doesn't run.

Basically, what you want to do is throw an exception. In the case of
an argument being null, you want to do something like this:

public class MyClass
{
public MyClass()
{
}

public MyClass(SomeOtherClass other)
{
// If other is null, throw an exception.
if (other == null)
{
// Throw an exception.
throw new ArgumentNullException();
}
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:uT**************@TK2MSFTNGP03.phx.gbl...
>Hi,
I'd like to create a compile time error in my class... maybe there's a
way already built in in the framework so I can achieve what I want... I
have 2 constructors in my class. One of them has mandatory parameters, I
mean, they should not be null nor empty (for strings). So I'd make the
validation in the constructor and generate a compile-time error if the
validation does not match...

Is there a way to achieve this or to specify mandatory parameters? (I'm
using C#)

thanks

ThunderMusic


Aug 21 '06 #3

P: n/a
Hi,

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:uE**************@TK2MSFTNGP05.phx.gbl...
What I wanted to do is to have the user (developer) of my class have a
compile-time error when it supplies null as the value of a parameter when
he compiles


There is no way to do this. The only moment where you can evaluate your
parameters is at runtime, you cannot know the possible values at
compile-time
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 21 '06 #4

P: n/a
ok, thanks... ;) after your first post, I realized that, but I thought "hey,
what if it's possible in a way I don't know..." ;) I'll use the
exceptions...
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.uswrote
in message news:e9****************@TK2MSFTNGP06.phx.gbl...
Hi,

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:uE**************@TK2MSFTNGP05.phx.gbl...
>What I wanted to do is to have the user (developer) of my class have a
compile-time error when it supplies null as the value of a parameter when
he compiles

There is no way to do this. The only moment where you can evaluate your
parameters is at runtime, you cannot know the possible values at
compile-time
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Aug 21 '06 #5

P: n/a
ThunderMusic,

What you are looking for is a static analysis tool, which will analyze
the possible code paths. Needless to say such tools are difficult to get
right (since they can't possibly get ALL possible code paths for larger,
more complex applications).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:uw**************@TK2MSFTNGP04.phx.gbl...
ok, thanks... ;) after your first post, I realized that, but I thought
"hey, what if it's possible in a way I don't know..." ;) I'll use the
exceptions...
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:e9****************@TK2MSFTNGP06.phx.gbl...
>Hi,

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:uE**************@TK2MSFTNGP05.phx.gbl...
>>What I wanted to do is to have the user (developer) of my class have a
compile-time error when it supplies null as the value of a parameter
when he compiles

There is no way to do this. The only moment where you can evaluate your
parameters is at runtime, you cannot know the possible values at
compile-time
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Aug 21 '06 #6

P: n/a
Spec# does a great job of handling this case :)

Cheers,

Greg
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:ey**************@TK2MSFTNGP02.phx.gbl...
ThunderMusic,

What you are looking for is a static analysis tool, which will analyze
the possible code paths. Needless to say such tools are difficult to get
right (since they can't possibly get ALL possible code paths for larger,
more complex applications).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:uw**************@TK2MSFTNGP04.phx.gbl...
>ok, thanks... ;) after your first post, I realized that, but I thought
"hey, what if it's possible in a way I don't know..." ;) I'll use the
exceptions...
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:e9****************@TK2MSFTNGP06.phx.gbl...
>>Hi,

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:uE**************@TK2MSFTNGP05.phx.gbl...
What I wanted to do is to have the user (developer) of my class have a
compile-time error when it supplies null as the value of a parameter
when he compiles

There is no way to do this. The only moment where you can evaluate your
parameters is at runtime, you cannot know the possible values at
compile-time
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Aug 21 '06 #7

P: n/a
You should probably also look at #error directive, here is the msdn
link:
http://msdn2.microsoft.com/en-us/library/x5hedts0.aspx

Take care
Naveed

Aug 21 '06 #8

P: n/a
The ref keyword might be able to help you here.

"An argument passed to a ref parameter must first be initialized"

Aug 22 '06 #9

P: n/a
Well, if it is null, then it *is* initialized; and the OP was talking about
*explicitely* passing null, in which case the relevant error would be "A ref
or out argument must be an assignable variable", but I personally wouldn't
advocate making this change; it would preclude, for instance, passing
(inline) consts, readonly fields, properties, method results, etc; all sorts
of things.

Marc
Aug 22 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.