In many occasions I found code like this (especially in Microsoft's code)

object obj1
num1 = 0
[...
if ((obj1 as short) != 0

num1 = ((short) obj1)


But when I try to do it I get an error on (obj1 as short) saying that "The as operator must be used with a reference type ('short' is a value type)

So how come it works for others but not for me

Thanx

Re: Using 'as' for value types... by Mattias

Mattias
Sat Apr 17 12:04:23 CDT 2004

Andrew,

>In many occasions I found code like this (especially in Microsoft's code):

I don't think I've ever seen such code. Got any pointers to it?


>But when I try to do it I get an error on (obj1 as short) saying that "The as operator must be used with a reference type ('short' is a value type)"
>
>So how come it works for others but not for me?

It doesn't work. Are you sure you're not confusing it with

if (obj is short) ...



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.