473,466 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2639
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();...
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
Oralloy
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,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.