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

implicit cast operator funny-ness

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 set a property on a class that takes an interface instance.
I have a class that can cast implicit to a class that impliments said
interface,
but the compiler moans and says it cannot cast implicitly like that.

My question is why? Why does this make sense to work like this? Here is
the code:

BTW - the line marked with the **** comment gives this error: error
CS0266: Cannot implicitly convert type 'test_operators.WithOperator' to
'test_operators.IB'. An explicit conversion exists (are you missing a
cast?)

class Program
{
static void Main( string[] args )
{
A a = new A();
WithOperator w = new WithOperator();

a.TheIB = w; // **** - does not compile
a.TheIB = (BIml)w; // works
a.TheIB = new BImpl(); // works
}
}

interface IB
{
int SomeProperty { get; }
}

class A
{
public IB TheIB
{
set
{
Console.WriteLine( value.SomeProperty );
}
}
}

class BImpl : IB
{
#region IB Members

public int SomeProperty
{
get { return 10; }
}

#endregion
}

class WithOperator
{
public static implicit operator BImpl( WithOperator w )
{
return new BImpl();
}
}

Regards,
Pieter Breed

Apr 1 '06 #1
10 2171
Pieter Breed <pi**********@gmail.com> wrote:
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 set a property on a class that takes an interface instance.
I have a class that can cast implicit to a class that impliments said
interface,
but the compiler moans and says it cannot cast implicitly like that.

My question is why? Why does this make sense to work like this?


I suspect the problem is that while there's an implicit conversion from
WithOperator to BImpl, and an implicit conversion from BImpl to BI,
that doesn't mean there's an implicit conversion from WithOperator to
BI. Once you do the cast, there's only one conversion left to do.

I can check the specs in this regard if you want, but I don't guarantee
to be able to do it any time soon - it can be quite dense reading in
terms of conversions.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 1 '06 #2
Thanks Jon,

In my code it makes idiomatic sense to have an implicit cast and it is
nicer to look at than an explicit cast. I just think the whole thing
kind of silly, since you are not allowed to do an implicit cast to an
interface either.

Ah, no harm done, but thanks :)

Pieter

Apr 1 '06 #3
"Pieter Breed" <pi**********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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 set a property on a class that takes an interface instance.
I have a class that can cast implicit to a class that impliments said
interface,
but the compiler moans and says it cannot cast implicitly like that.

My question is why? Why does this make sense to work like this? Here is
the code:


Hi Pieter

*WARNING* I just woke up and everything that I say is suspect until my coffee kicks in.

First, Comments on the design.
-------------------------------------------------
Your design may not act like you think it will act.
An implicit/Explicit conversion always creates a new object.
This makes your class act like a struct (value semantics).
Any changes you make to TheIB inside A will not get propagated back to w.
If this is what you want....Go ahead.
I personally think an Explicit ConvertTo method would be more appropriate so as to not confuse
future maintainers of your code.
If I saw
a.TheIB = (BIml)w
I would "Assume" that w implemented the BIml interface (Or one of it's ancestors did)
I would "Assume" that changes to BIml inside of a would be reflected inside w.

Anyway, on to your real problem ... It's a typo.
Add "using System;" to get it to compile
and I get
error CS0246: The type or namespace name 'BIml' could not be found (are you missing a using
directive or an assembly reference?)

So I Change BIml to BImpl (Typo) and compile.
It works.
10
10
10

I do not get the same error you are getting.

Here, try compiling this
-------------------------------------------------------------
using System;
class Program
{
static void Main( string[] args )
{
A a = new A();
WithOperator w = new WithOperator();

a.TheIB = w; // **** - does not compile
a.TheIB = (BImpl)w; // works
a.TheIB = new BImpl(); // works
}
}

interface IB
{
int SomeProperty { get; }
}

class A
{
public IB TheIB
{
set
{
Console.WriteLine( value.SomeProperty );
}
}
}

class BImpl : IB
{
#region IB Members

public int SomeProperty
{
get { return 10; }
}

#endregion
}

class WithOperator
{
public static implicit operator BImpl( WithOperator w )
{
return new BImpl();
}
}



Apr 1 '06 #4
Bill Butler <qw****@asdf.com> wrote:

<snip>
Anyway, on to your real problem ... It's a typo.


I don't think so (typos aside). I think there's a difference in the
conversion behaviour between 1.1 and 2.0. The code you provided
compiles in 1.1, but not in 2.0. Odd... I'll investigate this, as I
suspect it's not intended behaviour.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 1 '06 #5
Pieter,

This is more a comment on the use of an implicit cast operator to
perform this conversion. I would advise against it. It will cause the code
to be difficult to read. If anything, make it an explicit cast operator, or
better yet, have a method which will do it for you. Your code will be much
easier to maintain as a result.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Pieter Breed" <pi**********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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 set a property on a class that takes an interface instance.
I have a class that can cast implicit to a class that impliments said
interface,
but the compiler moans and says it cannot cast implicitly like that.

My question is why? Why does this make sense to work like this? Here is
the code:

BTW - the line marked with the **** comment gives this error: error
CS0266: Cannot implicitly convert type 'test_operators.WithOperator' to
'test_operators.IB'. An explicit conversion exists (are you missing a
cast?)

class Program
{
static void Main( string[] args )
{
A a = new A();
WithOperator w = new WithOperator();

a.TheIB = w; // **** - does not compile
a.TheIB = (BIml)w; // works
a.TheIB = new BImpl(); // works
}
}

interface IB
{
int SomeProperty { get; }
}

class A
{
public IB TheIB
{
set
{
Console.WriteLine( value.SomeProperty );
}
}
}

class BImpl : IB
{
#region IB Members

public int SomeProperty
{
get { return 10; }
}

#endregion
}

class WithOperator
{
public static implicit operator BImpl( WithOperator w )
{
return new BImpl();
}
}

Regards,
Pieter Breed

Apr 1 '06 #6
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
This is more a comment on the use of an implicit cast operator to
perform this conversion. I would advise against it. It will cause the code
to be difficult to read. If anything, make it an explicit cast operator, or
better yet, have a method which will do it for you. Your code will be much
easier to maintain as a result.


I'd certainly agree with this - I've never really liked implicit
conversions, personally...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 1 '06 #7
Hi all and thanks for cool responses!

My example was pedantic. What was actually going on in my code was that
I had to do a conversion between different border types. So I do think
an implicit cast would be appropriate since it is idiomatic. (meaning
asigning one kind of border to another should make sense)

But whether to use an implicit cast vs explicit cast is more a question
of style though, isn't it? My question was really about "Why oh why
does this not do what I expect it to do", than "do you all think my
coding style is ok" ;)

Regards,
Pieter

Apr 2 '06 #8
Pieter Breed <pi**********@gmail.com> wrote:
Hi all and thanks for cool responses!

My example was pedantic. What was actually going on in my code was that
I had to do a conversion between different border types. So I do think
an implicit cast would be appropriate since it is idiomatic. (meaning
asigning one kind of border to another should make sense)

But whether to use an implicit cast vs explicit cast is more a question
of style though, isn't it? My question was really about "Why oh why
does this not do what I expect it to do", than "do you all think my
coding style is ok" ;)


Ah, right. In that case, diving into the specs to see what's changed is
entirely the right way of going :)

I see four possibilities:

1) This was a bug in 1.1 - it should never have compiled.
2) This is a bug in 2.0 - it should compile.
3) The spec changed accidentally, and both compilers are accurate.
4) The spec changed deliberately, and both compilers are accurate.

I'll let you know what I find out...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 2 '06 #9
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Ah, right. In that case, diving into the specs to see what's changed is
entirely the right way of going :)

I see four possibilities:

1) This was a bug in 1.1 - it should never have compiled.
2) This is a bug in 2.0 - it should compile.
3) The spec changed accidentally, and both compilers are accurate.
4) The spec changed deliberately, and both compilers are accurate.

I'll let you know what I find out...


It seems that option 1 is the correct one here - it shouldn't have
compiled, ever. Apparently the idea is that there aren't any user-
defined conversions involved implicitly in a conversion to an interface
type.

I suspect the way the spec achieves that is somewhat convoluted though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 6 '06 #10
Thanks for the trouble Jon, I appreciate it.

I suppose it is one of those things that make sense if you think about
it... The compiler does not know when of the million possible implicit
conversions that might exist to classes that all might impliment the
specific interface you are thinking of, to use:

interface IB
{
}

class B1 : IB
{
}

class B2 : IB
{
}

class A
{
public static implicit B1( A a ) { };
public static implicit B2( A a ) { };
}

so assigning A to an IB will cause who knows what, right?

Regards,
Pieter

Apr 6 '06 #11

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)...
7
by: pnsteiner | last post by:
i want to use the << operator defined for ostream with an object that itself knows nothing of the operator, but is castable to ostream&. it seems that C++ doesn't do implicit casts before invoking...
10
by: Peter Drese | last post by:
Hello, I have to write an exam and I am not sure about implicit typecasting. Can somebody please tell me of what type the following c-statements are? And why they are of this type? It is also...
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...
2
by: Jon Shemitz | last post by:
How come I can write code like "if (L)" but NOT code like "if (L == true)" when L is a class with an implicit operator bool? /////////// List L = new List(); public class List { private...
8
by: GlennDoten | last post by:
I just happened to be looking through the implementation of the System.Version class in the SSCLI and one of the constructors starts like this: public Version(String version) { if ((Object)...
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...
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...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
1
by: not_a_commie | last post by:
I have an Angle class that I store angles in. It's basically just a bunch of fancy functions for manipulating a double. It has implicit casts for converting to/from double. Due to the project...
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
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
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.