L
Laura T.
The following code is driving me so crazy, that I'm thinking it's a
"feature" or a..
....dare I say it... a BUG. I'm using VS 2003.
I've created a new value type (struct) called MyInt. MyInt has
all(?) the conversion routines necessary to convert from Int32 and string.
(see the listing at the end)
When I cast directly from a value of Int32 type, it works:
MyInt v=(MyInt)1 or
MyInt v=(MyInt)varInt32;
When I try to cast a boxed value (object returned from a function),
I get "Specified cast is not valid." System.InvalidCastException:
private object GetInt()
{
return 1
}
MyInt v=(MyInt)GetInt(); <-- Exception.
The IL code generated is the following:
..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01
00 00 00 )
// Code size 24 (0x18)
.maxstack 1
.locals init ([0] valuetype Cast.MyInt z1,[1] valuetype Cast.MyInt z2)
IL_0000: call object Cast.Class1::NewInt()
IL_0005: unbox Cast.MyInt
IL_000a: ldobj Cast.MyInt
IL_000f: stloc.0
IL_0010: ldc.i4.2
IL_0011: call valuetype Cast.MyInt Cast.MyInt:p_Explicit(int32)
IL_0016: stloc.1
IL_0017: ret
} // end of method Class1::Main
Is this something that cannot be done? I've searched everywhere but nothing
states that it cannot be done.
_OR_ am I missing something or doing something awfully wrong?
Please help me!
Laura
------- Copy and paste to a C# Console Application project ----------------
using System;
namespace Cast
{
public struct MyInt
{
private Int32 _my_int;
public MyInt(Int32 value)
{
_my_int=value;
}
public MyInt(object value)
{
_my_int=System.Convert.ToInt32(value);
}
public static explicit operator MyInt(string value)
{
return new MyInt(System.Convert.ToInt32(value));
}
public static explicit operator MyInt(Int32 value)
{
return new MyInt(value);
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
MyInt z1=(MyInt)NewInt();
}
private static object NewInt()
{
return 1;
}
}
}
"feature" or a..
....dare I say it... a BUG. I'm using VS 2003.
I've created a new value type (struct) called MyInt. MyInt has
all(?) the conversion routines necessary to convert from Int32 and string.
(see the listing at the end)
When I cast directly from a value of Int32 type, it works:
MyInt v=(MyInt)1 or
MyInt v=(MyInt)varInt32;
When I try to cast a boxed value (object returned from a function),
I get "Specified cast is not valid." System.InvalidCastException:
private object GetInt()
{
return 1
}
MyInt v=(MyInt)GetInt(); <-- Exception.
The IL code generated is the following:
..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01
00 00 00 )
// Code size 24 (0x18)
.maxstack 1
.locals init ([0] valuetype Cast.MyInt z1,[1] valuetype Cast.MyInt z2)
IL_0000: call object Cast.Class1::NewInt()
IL_0005: unbox Cast.MyInt
IL_000a: ldobj Cast.MyInt
IL_000f: stloc.0
IL_0010: ldc.i4.2
IL_0011: call valuetype Cast.MyInt Cast.MyInt:p_Explicit(int32)
IL_0016: stloc.1
IL_0017: ret
} // end of method Class1::Main
Is this something that cannot be done? I've searched everywhere but nothing
states that it cannot be done.
_OR_ am I missing something or doing something awfully wrong?
Please help me!
Laura
------- Copy and paste to a C# Console Application project ----------------
using System;
namespace Cast
{
public struct MyInt
{
private Int32 _my_int;
public MyInt(Int32 value)
{
_my_int=value;
}
public MyInt(object value)
{
_my_int=System.Convert.ToInt32(value);
}
public static explicit operator MyInt(string value)
{
return new MyInt(System.Convert.ToInt32(value));
}
public static explicit operator MyInt(Int32 value)
{
return new MyInt(value);
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
MyInt z1=(MyInt)NewInt();
}
private static object NewInt()
{
return 1;
}
}
}