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

conversion of value types and boxing

Hello,
I have class (let's say A) wrapping Int32 type and have added implicit
conversion operators for converting between int and A.
Now I would like to have also explicit operator for conversion from boxed
int to A. This seems to be problem, because I don't know the exact type of
boxed int.

For following code snippet

A a = (A)GetX(3);
....
object GetX(int opCode) { //this method can return different types depending
on value of opCode
...
if (opCode == 3) return new int(5);
}

when returning from GetX(), int value is boxed to object and conversion from
boxed int to A fails with 'Specified cast is not valid' because unboxing
occures first which is by itself implicit conversion. I would have to use
A a = (A)(int)GetX(3);
which is annoying.

I think that the conversion I would like to have is not possible in C#, but
I'd like to be sure. Do you agree with me? Or do you have solution for me?

Thanks,
eXavier
Nov 16 '05 #1
3 1215

I suppose you could add a conversion from object, and then in the
conversion method check if ( obj is int )

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
I cannot add conversion to object. Conversion operator cannot be added
for conversion between types S, T where S is inherited from T or T is
inherited from S and object is base type of all types, even value types
:-(

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
> I think that the conversion I would like to have is not possible in C#, but
I'd like to be sure. Do you agree with me? Or do you have solution for me?


I think I agree with you.

Unboxing is an explicit conversion and it must be done with the exact type
of the thing in the box. If you have a boxed int, you have to unbox
explicitly with (int), if it is a boxed double, you have to use (double), and
so on.
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
19
by: steve | last post by:
// What I want to do Use enumerated types with the Interlocked.Exchange methods Suggestions please // My estimation of problem Seems like Interlocked.Exchange only works with ints,...
4
by: apoc69 | last post by:
hi folks, is that possible in c#? : i have for example a simple field class, which contains a value, like this: public class Field { private object value;
11
by: .Net Sports | last post by:
I need to convert some C# ?Conditionals over to vb.net (most likely in vb.net using If..Then statements), but the C#2VBNETconverter by KamalPatel isn't converting them: string PurchaseType =...
11
by: santosh | last post by:
Hello all, Conversion macros along the name of INT8_C, INT16_C etc, are defined in stdint.h to convert their argument into suitable representations for their corresponding types, i.e. int8_t,...
5
by: Pavils Jurjans | last post by:
Hello, I am somewhat lost in the implicit/expicit possible/impossible type casting in C#... I need to write a class, which among other things, must have wat to read a numeric value type, and...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
6
by: Håkan Johansson | last post by:
error CS0445: Cannot modify the result of an unboxing conversion I'm quite new to C# and can't really see how to get rid of the above error. I have an ArrayList instance to which I add two...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.