explicit operator cast --> Specified cast is not valid

P

Peter

Hello
i use a simple class (or struct) with explicit cast operator:

public class UserNumber
{
private short value;
public UserNumber(short value)
{
this.value = value;
}

public static explicit operator UserNumber(short value)
{
return new UserNumber(value);
}

public static explicit operator short(UserNumber item)
{
return item.value;
}
}

Following code works as expected:
.....
UserNumber b = new UserNumber(5);

short test = (short)b;


-------------------

BUT this one throws Specified cast is not valid.
.....
UserNumber b = new UserNumber(5);
TestMethod(b);
.....

private void TestMethod(object b)
<-- Param is object
{
short test;
test =
(short)b;
<-- Exception
}


=======================================

short s = 5;
TestMethod(s);

Works fine.


Why ?
I use an existing dll that I can not change (as in Example
TestMethod(object value))
I suspect this Method cast all base types (if value.GetType() ==
typeof(short) .....
but of course not UserNumber
What can i do ? I must use TestMethod

Thanks
 
P

Peter

[...]
BUT this one throws Specified cast is not valid.
.....
           UserNumber b = new UserNumber(5);
           TestMethod(b);
.....
  private void TestMethod(object b)
<-- Param is object
  {
        short test;
        test =
(short)b;
<--  Exception
  }

   short s = 5;
   TestMethod(s);
Works fine.

Did you actually post the real code for the TestMethod() method?  If
not, how can you expect anyone to answer the question "why?" the method
does what it does?
I use an existing dll that I can not change (as in Example
TestMethod(object value))
I suspect this Method cast all base types  (if value.GetType() ==
typeof(short) .....
but of course not UserNumber
What can i do ? I must use TestMethod

There's no reason to "suspect" what the TestMethod() does.  Use
Reflector (v6 should still be free, you can pay for v7) or ildasm.exe
(comes with Visual Studio) to inspect the code.

As for fixing the problem, why not just convert (with the explicit cast)
the value before passing to the TestMethod() method?

If the above isn't helpful, then you need to post a concise-but-complete
code example that reliably reproduces the problem.  There's not enough
information in your original question to unambiguously define the problem..

Pete- Zitierten Text ausblenden -

- Zitierten Text anzeigen -


Sorry for my novels:

The problem occurs already in the TestMethod(object b) shown above!
Using existing dll... was only a hint and has nothing to do with the
problem.

The Problem is:

private void TestMethod(object b)
{
short test;
test = (short)b;
}


call TesMethod with a short Value works.

call TesMethod with a UserNumber-Value that implements public
static explicit operator short(UserNumber item) works not.


Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top