Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding intPtr to int generate error on .net framework 3.5

i have this code:

lvItem.pszText = (IntPtr)(lpRemoteBuffer + Marshal.SizeOf(typeof(LV_ITEM)));

it works fine on 4.0.

if i downgrade the project to 3.5 it gives me this error :

Operator '+' cannot be applied to operands of type 'System.IntPtr' and 'int'

any idea how do i fix that to make it work on 3.5

and i dont know why it works in 4.0 ?

thanks in advance

like image 487
Stacker Avatar asked Jul 24 '26 22:07

Stacker


1 Answers

Yup - if you look at the documentation for the Addition property you'll see that operator was only introduced in .NET 4. You shouldn't need the cast, by the way.

On .NET 3.5 you could probably use:

lvItem.pszText = new IntPtr(lpRemoteBuffer.ToInt64() +
                            Marshal.SizeOf(typeof(LV_ITEM)));

Of course you then need to hope you're not on a 32-bit system with a pointer which goes over int.MaxValue :)

like image 111
Jon Skeet Avatar answered Jul 26 '26 11:07

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!