Object reference not set to an instance of an object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have this situtaition.
I want to extract the top most byte from a DWORD (uint in C#).

In C++ I would use some macros like HIBYTE( HIWORD(version)))
Or use another way like byte *ptrByte = (byte*)version; iReturn=ptrByte[3];

So I try to do this in C# using the unsafe, but I get this exception:
e.Message "Object reference not set to an instance of an object." string
Any tips how to do this in a C# style? I also fought with the *fixed*
function but that one has problems between uint* and byte*

The code below compiles.

------------------------------
public int GetVersion() {
int iReturn=-1;
try {
uint version=0x12345678;
unsafe {
byte *ptrByte = (byte*)version;
iReturn=ptrByte[3];
}
} catch (Exception e) {
MessageBox.Show(e.Message, "Critical Error!");
}
return iReturn;
}
}
 
You can use System.Collections.Specialized.BitVector32 to emulate HIBYTE
behavior.
 
I have this situtaition.
I want to extract the top most byte from a DWORD (uint in C#).

Right - forget unsafe code to start with. Don't use unsafe code unless
you've got a very, very good reason to do so. Fortunately, it's very
easy to do what you want:

byte topByte = (byte)(value>>24);
 
Andrew Gnenny said:
You can use System.Collections.Specialized.BitVector32 to emulate HIBYTE
behavior.

You could, indeed - but why not just use plain old-fashioned
bitshifting?
 
Back
Top