Stacker
Stacker

Reputation: 8237

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

Upvotes: 3

Views: 2449

Answers (2)

Maximilian Mayerl
Maximilian Mayerl

Reputation: 11357

IntPtr didn't support pointer arithmetic prior to .NET 4.0. If you want to work that way with pointers, you have to use real pointers insteat of IntPtr.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500983

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 :)

Upvotes: 8

Related Questions