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 3501
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(SomeOth erClass other)
{
// If other is null, throw an exception.
if (other == null)
{
// Throw an exception.
throw new ArgumentNullExc eption();
}
}
}
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"ThunderMus ic" <No************ *************@N oSpAm.comwrote in message
news:uT******** ******@TK2MSFTN GP03.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 NullReferenceEx ception?
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.c omwrote in
message news:e%******** ********@TK2MSF TNGP05.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(SomeOth erClass other)
{
// If other is null, throw an exception.
if (other == null)
{
// Throw an exception.
throw new ArgumentNullExc eption();
}
}
}
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"ThunderMus ic" <No************ *************@N oSpAm.comwrote in message
news:uT******** ******@TK2MSFTN GP03.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,
"ThunderMus ic" <No************ *************@N oSpAm.comwrote in message
news:uE******** ******@TK2MSFTN GP05.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.mach in AT dot.state.fl.us wrote
in message news:e9******** ********@TK2MSF TNGP06.phx.gbl. ..
Hi,
"ThunderMus ic" <No************ *************@N oSpAm.comwrote in message
news:uE******** ******@TK2MSFTN GP05.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.co m
"ThunderMus ic" <No************ *************@N oSpAm.comwrote in message
news:uw******** ******@TK2MSFTN GP04.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.mach in AT dot.state.fl.us >
wrote in message news:e9******** ********@TK2MSF TNGP06.phx.gbl. ..
>Hi,
"ThunderMusi c" <No************ *************@N oSpAm.comwrote in message news:uE******* *******@TK2MSFT NGP05.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.mach in 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.c omwrote in
message news:ey******** ******@TK2MSFTN GP02.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.co m
"ThunderMus ic" <No************ *************@N oSpAm.comwrote in message
news:uw******** ******@TK2MSFTN GP04.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.mach in AT dot.state.fl.us > wrote in message news:e9******** ********@TK2MSF TNGP06.phx.gbl. ..
>>Hi,
"ThunderMusic " <No************ *************@N oSpAm.comwrote in message news:uE****** ********@TK2MSF TNGP05.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.machi n 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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 my particular application. One of the
advantages is that the _compiler_ can force inner matrix dimensions used in
multiplication to agree. A _complie-time_ error will be triggered if you
write A * B and the number of coluns in A does not equal the...
|
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 T0 and T1
};
|
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 rightType;
|
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 )
Person.xsd includes Common-Naming.xsd ( anonimous schemas )
Common-Naming.xsd includes common.xsd ( both are anonimous schemas )
Compilation of Resident.xsd raise the following exception:
"System.Xml.Schema.XmlSchemaException: The attribute 'oid'...
|
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
structures are read only)
Ideally one should be able to put the redundant
information there automatically so no mistakes are possible, but in a lot
of case I see no way how to do it.
| |
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: to catch unhandled cases, but of
course this is only at run-time.
Example:
public enum MyEnum
{
one, two, three, four
}
|
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 since longs are not the same size
on win64 and linux x64) I would like to do a check at compile time to make
sure things are correct. This will ease future work.
Is it possible to do something like the following at preprocessing stage:
|
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 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...
|
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 will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |