473,396 Members | 2,111 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,396 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 6503
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

1
by: A | last post by:
Hi, I created an array of user defined objects dynamically in a class Foo's constructor. class Foo{ Foo(){ Bar* b = new Bar(); }
8
by: Michael Gaab | last post by:
How would I force the compiler to throw an error for the following: function signature - void foo(short); function call - foo('d'); My compiler does not complain when I call foo() with a...
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...
9
by: Finger.Octopus | last post by:
I dont know whats terribly going wrong with this, well I know there's some memory allocation with this but I tried hard to fix it and when it gets fixed it doesn't shows up the desired value, this...
2
by: KiranJyothi | last post by:
Can anyone please tell me how can I rectify my compile time error in this program. /********************************HEADER FILE***********************************/ #ifndef POLYNOMIAL_H //...
5
by: fimarn | last post by:
I am trying to get rid of compile time error that I am getting only in RHEL5 (not in RHEL4) apparently due to the changes in the stl_list.h file. The error that I am getting is coming from the...
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...
4
by: cpptutor2000 | last post by:
Could someone please help me? I have the following C struct. typedef struct _FIREWALL_RULE_INFO { ULONG Precedence; BOOL bEnabled; BOOL ...
5
by: NitinSawant | last post by:
Hi, Is it possible to validate property of a ASP.NET UserControl at compile time.. e.g. ID="1" shows an error I have a public property called 'UploadDirectory' I want to show error message...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.