2014R3 : AddressPtrMBS requires conversion from type Ptr to Memoryblock

I have the following code, which works fine in 2014 R2.1 and MBS 14.3:

  dim p as ptr, offset as integer
  dim mb as memoryblock  
  [...]
  mb  = p.AddressPtrMBS(offset)

Under 2014R3, I’m getting the following error:

Extension method _Globals20.AddressPtrMBS requires a conversion from type Ptr to class MemoryBlock; use CType to explicitly convert first

But when I try it:

  mb  = CType(p,MemoryBlock).AddressPtrMBS(offset)  // this works
or
  mb  = MemoryBlock(p).AddressPtrMBS(offset)  // this doesn't work

I’m confused why CType works but casting doesn’t.

AddressMBS returns a ptr, so please use a ptr variable for the result.
Than you can assign to a memoryblock variable which triggers the automatically conversion.

this code works:

[code]dim m as new MemoryBlock(10)
dim p as ptr = m.AddressPtrMBS(0)
dim o as MemoryBlock = p

Break[/code]

The compiler used to automatically do conversion.

sorry, you did it other way. So you have p which is a ptr and call AddressPtrMBS on this. But AddressPtrMBS requires to be called on a memoryblock.
So first assign to memoryblock, than query new ptr:

[code] dim p as ptr, offset as integer
dim mb as memoryblock

dim temp as MemoryBlock = p
mb = temp.AddressPtrMBS(offset)[/code]

Thanks, Christian. I’m interested in why this change was made, as it seems to break code that is otherwise sensible, and the workaround looks somewhat clumsy.

On a side note, is this something you could fix within the plugin? e.g. have overloaded versions of AddressPtrMBS that handle memoryBlock and Ptr ?

I have no idea. We’ll see.

[quote=149890:@Michael Diehr]Under 2014R3, I’m getting the following error:

Extension method _Globals20.AddressPtrMBS requires a conversion from type Ptr to class MemoryBlock; use CType to explicitly convert first [/quote]

This is expected and documented:

CType performs a type conversion but casting basically reinterprets the value differently.

Thanks, Joe. I ran into another situation that’s similar, but different:

dim v as variant = "600x400"
dim w as integer
w = val(v.NthField("x",1))

Which also triggers the “requires a conversion… use CType” message

In this case, I fixed it using .StringValue:

dim v as variant = "600x400"
dim w as integer
w = val(v.StringValue.NthField("x",1))

I guess my overall question here : Is this fixing a bug or some edge case that wouldn’t compile or execute properly? Or is it more just a change in the compiler rules that had to happen for some other reason, but doesn’t really fix anything broken in these particular examples? Or…?

Also, is there any documentation on the difference between CType() and Casting, especially related to Variants and Ptr/MemoryBlocks?