473,413 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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

Aug 21 '06 #1
9 3477
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
2
by: Abhishek Saksena | last post by:
Is it possible using Boost mpl library:- Assume any class implementing a function "connect" with two arugments of fixed types class protocol1 { connect(T0 & t0, T1 &t1 ){..} //fixed types...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
5
by: Brice Prunier | last post by:
Here under 4 schemas i'm working with ( it may be long: sorry...) The context is the following : Resident.xsd imports Person.xsd and includes Common.xsd ( anonimous schema: no TargetNamespace )...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
13
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default:...
4
by: John Smith | last post by:
Hi I'm porting some C++ code to new platforms and have some 1-byte aligned structures which need a specific size. Since datatypes can vary on different platforms (which I found out the hard way...
9
by: ThunderMusic | last post by:
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...
11
by: Bryan Crouse | last post by:
I am looking a way to do error checking on a string at compile time, and if the string isn't the correct length have then have the compiler throw an error. I am working an embedded software that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.