Question

What is the proper way to compare an IntPtr’s value to an int?

IntPtr ContainerBaseAdress = ReadmemoryProperply.ReadProcessPointer(Tibia.Handle, ContainerPointer);

if (ContainerBaseAdress.ToInt32() == 51)
{
    //Executable code
}

Or

IntPtr ContainerBaseAdress = ReadmemoryProperply.ReadProcessPointer(Tibia.Handle, ContainerPointer);

if (ContainerBaseAdress == (IntPtr)51)
{
    //Executable code
}

Or

IntPtr ContainerBaseAdress = ReadmemoryProperply.ReadProcessPointer(Tibia.Handle, ContainerPointer);

if (ContainerBaseAdress == new IntPtr(51))
{
    //Executable code
}

Or basically, what is the proper way to compare the value of an IntPtr with an int.

Was it helpful?

Solution

Which one you want to use depends on the behavior your want. The first will throw an OverflowException if the value of ContainerBaseAdress does not fit in a 32-bit integer. This is likely to happen if the IntPtr points to some address and is being run on the 64-bit CLR. If throwing an exception is the desired behavior, then go with that.

The 2nd and 3rd are equivalent. The cast just wraps new. However these will not throw an exception, they will just evaluate the false. Which of these you choose is a matter of stylistic preference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top