|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
creating a compile time error... is it possible?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 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. -- Show quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message news:uTOLzjSxGHA.3264@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 Show quote "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in message news:e%23KUnpSxGHA.1284@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] > - mvp@spam.guard.caspershouse.com > > "ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message > news:uTOLzjSxGHA.3264@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" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message There is no way to do this. The only moment where you can evaluate your news:uEpL$7SxGHA.1284@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 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... Show quote "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:e9xSI%23SxGHA.5056@TK2MSFTNGP06.phx.gbl... > Hi, > > "ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message > news:uEpL$7SxGHA.1284@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). -- Show quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message news:uwNwFXTxGHA.4460@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:e9xSI%23SxGHA.5056@TK2MSFTNGP06.phx.gbl... >> Hi, >> >> "ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message >> news:uEpL$7SxGHA.1284@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 Show quote "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in message news:eyp3yaTxGHA.4872@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] > - mvp@spam.guard.caspershouse.com > > "ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message > news:uwNwFXTxGHA.4460@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:e9xSI%23SxGHA.5056@TK2MSFTNGP06.phx.gbl... >>> Hi, >>> >>> "ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message >>> news:uEpL$7SxGHA.1284@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 >>> >> >> > > 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 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 |
|||||||||||||||||||||||