473,395 Members | 1,595 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.

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 2636
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
2
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents a rather good discussion of typecast operator overloading. I am presenting...
2
by: RR | last post by:
I'm sure this has been answered before but two hours of searching and reading hasn't answered this question for me. Here's a test program: ******************************** class base {...
9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
0
by: A. W. Dunstan | last post by:
I'm porting some code to Visual C++ and have run into a problem - the compiler won't use a user-written cast operator. The code uses an envelope-letter approach to passing (potentially) large...
0
by: Reece Hart | last post by:
Here's the basic issue: PostgreSQL doesn't use indexes unless a query criterion is of exactly the same type as the index type. This occurs even when a cast would enable the use of an index and...
3
by: /* frank */ | last post by:
Explicit conversion is made by mean of a cast i.e. float a; int b; .... b = (int) a; But the implicit conversion? What is?
2
by: babylon | last post by:
I have an enum public enum MyEnum : int { X, Y } I have to do int a = (int) MyEnum.X; can i overload the operator or other means to do something like
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.