`Variant.PtrValue` hard crash

I may be misunderstanding the use of Variant.PtrValue so can somebody please explain what’s going on here?

This code hard crashes:

Var v As Variant = 5
Var i As Integer = v.PtrValue.Integer

And this code raises a TypeMismatchException (with no explanation for the exception in the debugger):

Var v As Variant = DateTime.Now
Var i As Integer = v.PtrValue.Integer

What I’m trying to do is get the actual memory address of a variant. Why? I’m trying to use it as a substitute for create a unique hash code for objects in my physics engine and I figured that using the actual memory location would be unique within a running app.

Is there anyway to get the memory address of a variable at runtime?

Is the Variant.Hash method insufficient?

Taking pointer value of an integer 5 would give you an invalid pointer.
Better try hash function.

How did I not know about this method? Thats’s awesome - why am I trying to reinvent the wheel?.

Thanks @Kem_Tekinay (and @Christian_Schmitz)

1 Like