471,325 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

explicit operator for a cast

How does a custom casting operator work? I have a class that defines one,
but doesn't work as expected (or perhaps, the way I want it to).

I have the following example classes:
------------------
public class One : IOne {
....
}//One

public class Two {
public One MyOne;

....

public static explicit operator One (Two value) {
return value.MyOne;
}
}//Two
------------------

Now, I have an ArrayList that I would like to do the following, but it
doesn't work:
-------------------
ArrayList arrOfTwoObjects;
for (int x = 0....) {
IOne obj = arrOfTwoObjects[x] as IOne; //this cast fails, and obj is
null
}
-------------------

Throughout my code, I have the above handling with an arraylist, casting to
an interface. I'm not allowed to have an explicit cast operator to an
interface. Is the only solution to have Two implement IOne as well, and
delegate all the implementation to the child MyOne object? That is going to
be pretty cumbersome... :(

J
Jan 17 '06 #1
2 2544
John,

This happens because the array list returns an object, and the compiler
doesn't know how to cast from object to IOne. However, you can cast to a
Two instance, which knows how to cast to IOne, like so:

IOne obj = (One)(Two) arrofTwoObjects[x];

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

"John Richardson" <j3*********@nospam.ca> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
How does a custom casting operator work? I have a class that defines one,
but doesn't work as expected (or perhaps, the way I want it to).

I have the following example classes:
------------------
public class One : IOne {
...
}//One

public class Two {
public One MyOne;

...

public static explicit operator One (Two value) {
return value.MyOne;
}
}//Two
------------------

Now, I have an ArrayList that I would like to do the following, but it
doesn't work:
-------------------
ArrayList arrOfTwoObjects;
for (int x = 0....) {
IOne obj = arrOfTwoObjects[x] as IOne; //this cast fails, and obj is
null
}
-------------------

Throughout my code, I have the above handling with an arraylist, casting
to an interface. I'm not allowed to have an explicit cast operator to an
interface. Is the only solution to have Two implement IOne as well, and
delegate all the implementation to the child MyOne object? That is going
to be pretty cumbersome... :(

J

Jan 18 '06 #2
John Richardson wrote:
How does a custom casting operator work? I have a class that defines one,
but doesn't work as expected (or perhaps, the way I want it to).
<snip>
public static explicit operator One (Two value) {
return value.MyOne;
<snip>
for (int x = 0....) {
IOne obj = arrOfTwoObjects[x] as IOne; //this cast fails, and obj is
null


<snip>

"as" will never do user defined conversions, only reference conversions
(direct class hierarchy conversions) or boxing operations.

To take advantage of such typecasting operators that you used you must
use a normal typecast, ie. IOne obj = (IOne)twoObject;

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jan 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Stub | last post: by
2 posts views Thread by Nimmi Srivastav | last post: by
9 posts views Thread by Tanmoy Bhattacharya | last post: by
reply views Thread by A. W. Dunstan | last post: by
reply views Thread by Reece Hart | last post: by
3 posts views Thread by /* frank */ | last post: by
31 posts views Thread by Michael C | last post: by

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.