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 9 3374
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
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
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
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
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
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
The ref keyword might be able to help you here.
"An argument passed to a ref parameter must first be initialized"
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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
6 posts
views
Thread by Ben Ingram |
last post: by
|
2 posts
views
Thread by Abhishek Saksena |
last post: by
|
5 posts
views
Thread by Carmine Cairo |
last post: by
|
5 posts
views
Thread by Brice Prunier |
last post: by
|
10 posts
views
Thread by Bart Goeman |
last post: by
|
13 posts
views
Thread by Adam Blair |
last post: by
|
4 posts
views
Thread by John Smith |
last post: by
|
9 posts
views
Thread by ThunderMusic |
last post: by
|
11 posts
views
Thread by Bryan Crouse |
last post: by
| | | | | | | | | | |