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

T

ThunderMusic

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
 
N

Nicholas Paldino [.NET/C# MVP]

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

ThunderMusic

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 said:
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]
- (e-mail address removed)

ThunderMusic said:
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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

ThunderMusic said:
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
 
T

ThunderMusic

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

Nicholas Paldino [.NET/C# MVP]

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).
 
G

Greg Young

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

Cheers,

Greg
Nicholas Paldino said:
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]
- (e-mail address removed)

ThunderMusic said:
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...
 
T

Tigger

The ref keyword might be able to help you here.

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

Marc Gravell

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
 

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

Top