473,416 Members | 1,877 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,416 software developers and data experts.

Absolutely no implicit casting allowed!

I would like to know if there is a way to stop the runtime from implicitly
casting values. For exampel, I would like the following code to crash:

byte someByte = 5;
int someInt = someByte;

I would like the assignment of the variable "someByte" to the variable
"someInt" to crash because there is an implicit conversion from Byte to Int
and I DO NOT want that.

I have reasons why I would like this behavior. Any way this can be done?
Thanks
Nov 17 '05 #1
6 2810
I don't know of any way to force the compiler to create an error in that
case, but I bet you could create a custom FxCop rule to analyze your
code and generate errors. You could then incorporate FxCop analysis as
part of your build (using post build events, or a more flexible build
tool like NANT).

John Robbins wrote a couple good articles about writing custom FxCop
rules that you might find helpful. He references them in this article:
http://wintellect.com/WEBLOGS/wintel...11/09/619.aspx

Hope that helps.

Joshua Flanagan
http://flimflan.com/blog

Gecko wrote:
I would like to know if there is a way to stop the runtime from implicitly
casting values. For exampel, I would like the following code to crash:

byte someByte = 5;
int someInt = someByte;

I would like the assignment of the variable "someByte" to the variable
"someInt" to crash because there is an implicit conversion from Byte to Int
and I DO NOT want that.

I have reasons why I would like this behavior. Any way this can be done?
Thanks

Nov 17 '05 #2
Thanks Josh, I heard about FxCop some time ago, I actually downloaded it but
have not gotten around to try it. I will definitely be taking a look into it
now.

Thanks again.

"Joshua Flanagan" <jo**@msnews.com> wrote in message
news:O6**************@TK2MSFTNGP15.phx.gbl...
I don't know of any way to force the compiler to create an error in that
case, but I bet you could create a custom FxCop rule to analyze your code
and generate errors. You could then incorporate FxCop analysis as part of
your build (using post build events, or a more flexible build tool like
NANT).

John Robbins wrote a couple good articles about writing custom FxCop rules
that you might find helpful. He references them in this article:
http://wintellect.com/WEBLOGS/wintel...11/09/619.aspx

Hope that helps.

Joshua Flanagan
http://flimflan.com/blog

Gecko wrote:
I would like to know if there is a way to stop the runtime from
implicitly casting values. For exampel, I would like the following code
to crash:

byte someByte = 5;
int someInt = someByte;

I would like the assignment of the variable "someByte" to the variable
"someInt" to crash because there is an implicit conversion from Byte to
Int and I DO NOT want that.

I have reasons why I would like this behavior. Any way this can be done?
Thanks


Nov 17 '05 #3
Gecko <na**@nada.com> wrote:
I would like to know if there is a way to stop the runtime from implicitly
casting values. For exampel, I would like the following code to crash:

byte someByte = 5;
int someInt = someByte;

I would like the assignment of the variable "someByte" to the variable
"someInt" to crash because there is an implicit conversion from Byte to Int
and I DO NOT want that.

I have reasons why I would like this behavior. Any way this can be done?


Without writing your own compiler, you won't be able to force your code
not to compile.

I'm intrigued though - out of interest, why do you want to prevent
implicit conversions?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
There are two types of conversions:

An implicit (widening) coercion shall not lose any magnitude or precision.
These should be
provided using a method named op_Implicit

An explicit (narrowing) coercion may lose magnitude or precision. These
should be provided
using a method named op_Explicit

Byte to integer is a widening convertion and there is an implicit convertion
for that, so I would
limit the crashing to narrowing convertions.

Best Regards,
Alejandro Lapeyre

"Jon Skeet [C# MVP]" <sk***@pobox.com> escribió en el mensaje
news:MP************************@msnews.microsoft.c om...
Gecko <na**@nada.com> wrote:
I would like to know if there is a way to stop the runtime from
implicitly
casting values. For exampel, I would like the following code to crash:

byte someByte = 5;
int someInt = someByte;

I would like the assignment of the variable "someByte" to the variable
"someInt" to crash because there is an implicit conversion from Byte to
Int
and I DO NOT want that.

I have reasons why I would like this behavior. Any way this can be done?


Without writing your own compiler, you won't be able to force your code
not to compile.

I'm intrigued though - out of interest, why do you want to prevent
implicit conversions?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5
"Alejandro Lapeyre" <Al**************@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
There are two types of conversions:

An implicit (widening) coercion shall not lose any magnitude or precision.
These should be
provided using a method named op_Implicit

An explicit (narrowing) coercion may lose magnitude or precision. These
should be provided
using a method named op_Explicit

Byte to integer is a widening convertion and there is an implicit
convertion for that, so I would
limit the crashing to narrowing convertions.


Isn't that what C# does by default?

Michael
Nov 17 '05 #6
On Wed, 15 Jun 2005 20:33:51 -0500, "Gecko" <na**@nada.com> wrote:
I would like to know if there is a way to stop the runtime from implicitly
casting values. For exampel, I would like the following code to crash:

byte someByte = 5;
int someInt = someByte;

I would like the assignment of the variable "someByte" to the variable
"someInt" to crash because there is an implicit conversion from Byte to Int
and I DO NOT want that.

I have reasons why I would like this behavior. Any way this can be done?
Thanks


As others have pointed out, since the cast does not lose either
magnitude or precision, the compiler will do it automatically.

You might try replacing one or other of the types involved with a very
simple struct, so you can define which casts are explicit and which
are implicit. I suggest a struct since your example uses value types
rather than reference types; a class would work equally well.

struct NoCastInt {
int m_value;
}
static void Main() {
byte someByte = 5;
int someInt = someByte; // Compiles OK
NoCastInt someNCInt = someByte; // Compile error
}

rossum
The ultimate truth is that there is no ultimate truth
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Simon Ford | last post by:
Hi All, I'm having trouble understanding exactly how I can do some specific implicit casting. There are two problems here; does anyone know what I should be doing? //---------- // (1)...
9
by: Simon | last post by:
Hi All, Is it possible to disallow implicit casting for an operand of a function written in C? i.e. void foo(int a) {..} short b; foo(b) // error without explicit cast
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
14
by: Yurik | last post by:
A question to the C# language experts: Why isn't this code valid? static void Foo( out string s ) { s = "test"; } static void Main( ) { object s; // *** Accept any out type!
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
10
by: Pieter Breed | last post by:
Hi All, Please excuse me, but the bulk of my post will be a code post. It describes some weirdness with regards to the implicit casting operator. The crux of the problem is this: I want to...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
5
by: johanatan | last post by:
Does anyone know the reasons for the lack of an implicit casting operator in any greater depth than: A. Automatic conversion is believed to be too error prone. (from the FAQ at the bottom of:...
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: 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
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...
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.