J
John Richardson
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
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