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

InvalidCastException from explicit casting object to a class

Hi

I try the following program and I get InvalidCastException at the line

MyByte b = (MyByte)obj;

If I change it to

MyByte b = (MyByte)d;

it works fine.

I need to convert the obj to MyByte type. Do you know a way to do it?

Thanks in advance.

using System;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
struct Digit
{
byte value;

public Digit(byte value)
{
if (value > 9) throw new ArgumentException();
this.value = value;
}

public byte ByteValue
{
get
{
return ( this.value );
}
set
{
this.value = value;
}
}
// define implicit Digit-to-byte conversion operator:
public static implicit operator byte(Digit d)
{
Console.WriteLine("conversion occurred");
return d.value;
}
}

struct MyByte
{
byte value;

public MyByte(byte value)
{
if (value > 9) throw new ArgumentException();
this.value = value;
}

// define implicit Digit-to-byte conversion operator:
public static implicit operator MyByte(Digit d)
{
Console.WriteLine("conversion occurred");
return new MyByte(d.ByteValue);
}
}

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
Digit d = new Digit(3);
object obj = d;

MyByte b = (MyByte)obj;
System.Console.WriteLine(b);
}
}
}

Jul 21 '05 #1
4 1388
Kevin <ke**************@hotmail.com> wrote:
I try the following program and I get InvalidCastException at the line

MyByte b = (MyByte)obj;

If I change it to

MyByte b = (MyByte)d;

it works fine.

I need to convert the obj to MyByte type. Do you know a way to do it?


When you're unboxing, you need to unbox to the exact value type and
*then* you can do other conversions. So, you should be able to do:

MyByte b = (MyByte)(Digit) obj;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Well, if 'd' refers to an instance of MyByte, and 'obj' doesn't, then that
is why. You can't convert a type that is not compatible with MyByte to
MyByte.

This post makes no sense, as you didn't say what 'obj' is or what 'd' is.

"Kevin" <ke**************@hotmail.com> wrote in message
news:Of*************@TK2MSFTNGP11.phx.gbl...
Hi

I try the following program and I get InvalidCastException at the line

MyByte b = (MyByte)obj;

If I change it to

MyByte b = (MyByte)d;

it works fine.

I need to convert the obj to MyByte type. Do you know a way to do it?

Thanks in advance.

using System;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
struct Digit
{
byte value;

public Digit(byte value)
{
if (value > 9) throw new ArgumentException();
this.value = value;
}

public byte ByteValue
{
get
{
return ( this.value );
}
set
{
this.value = value;
}
}
// define implicit Digit-to-byte conversion operator:
public static implicit operator byte(Digit d)
{
Console.WriteLine("conversion occurred");
return d.value;
}
}

struct MyByte
{
byte value;

public MyByte(byte value)
{
if (value > 9) throw new ArgumentException();
this.value = value;
}

// define implicit Digit-to-byte conversion operator:
public static implicit operator MyByte(Digit d)
{
Console.WriteLine("conversion occurred");
return new MyByte(d.ByteValue);
}
}

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
Digit d = new Digit(3);
object obj = d;

MyByte b = (MyByte)obj;
System.Console.WriteLine(b);
}
}
}


Jul 21 '05 #3
I am sorry that I don't state my problem clearly. I know that "MyByte b =
(MyByte)(Digit) obj;" works. Unfortunately, this call is in an external
library and I cannot change. I want to know if there is any other way to
make the casting from object to MyByte works.

Thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kevin <ke**************@hotmail.com> wrote:
I try the following program and I get InvalidCastException at the line

MyByte b = (MyByte)obj;

If I change it to

MyByte b = (MyByte)d;

it works fine.

I need to convert the obj to MyByte type. Do you know a way to do it?


When you're unboxing, you need to unbox to the exact value type and
*then* you can do other conversions. So, you should be able to do:

MyByte b = (MyByte)(Digit) obj;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4
Kevin <ke**************@hotmail.com> wrote:
I am sorry that I don't state my problem clearly. I know that "MyByte b =
(MyByte)(Digit) obj;" works. Unfortunately, this call is in an external
library and I cannot change. I want to know if there is any other way to
make the casting from object to MyByte works.


Not if the object in question isn't actually a MyByte, no.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

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

Similar topics

1
by: bob scola | last post by:
I have a csharp, VS 2003 solution for a winform application The application uses an object called a "matter" and the class is defined in matter.cs. I can load matter objects into a combobox ...
4
by: Kevin | last post by:
Hi I try the following program and I get InvalidCastException at the line MyByte b = (MyByte)obj; If I change it to MyByte b = (MyByte)d;
4
by: Chris Li | last post by:
I have the following classes: Public Class BaseClass { ... } Public Class DerivedClass: BaseClass { ...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
2
by: John Richardson | last post by:
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:...
6
by: Jim S | last post by:
I have a .net framework 2.0 client (Pocket PC) and a .net 2.0 webservice that communicate on the same LAN. The Pocket PC has no problem consuming strings returned from the web service methoeds but...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
1
by: =?Utf-8?B?QkpT?= | last post by:
I have written a Generic method that takes an object, a type, T, and returns a System.Nullable (Of T) depending on whether or not the object IsDBNull. It was working fine until I tried to pass a...
4
by: roynevo | last post by:
Hi, I'm trying to deserialize an object on the context of a remoting call (RPC). The deserialization seem to work fine, however when I'm casting the result to the relevant class I get an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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?
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
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...

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.