Compile error when compiling with "ref" keyword

G

Guest

I get the following error messages when compiling my code:

The best overloaded method match for 'ByteArrayToStructure(byte[], ref
object)' has some invalid arguments Form1.cs
Argument '2': cannot convert from 'ref RESPONSE_STRUCT' to 'ref object'
Form1.cs

My code is as follows:

public struct RESPONSE_STRUCT
{
public byte Format;
public short sNumBytes;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public byte[] szDateTime;
}

private static void ByteArrayToStructure(byte[] bytearray, ref object obj)
{
//code here
}

RESPONSE_STRUCT pResponseStruct = new RESPONSE_STRUCT();
byte[] pBuffer = new byte[Marshal.SizeOf(pResponseStruct)];

ByteArrayToStructure(pBuffer, ref pResponseStruct); << Error is because
something is wrong here

Thanks for any help.
Terry
 
P

Peter Duniho

TNCoder said:
I get the following error messages when compiling my code:

The best overloaded method match for 'ByteArrayToStructure(byte[], ref
object)' has some invalid arguments Form1.cs
Argument '2': cannot convert from 'ref RESPONSE_STRUCT' to 'ref object'
Form1.cs

The problem is exactly as the error says it is: you are trying to pass a
"RESPONSE_STRUCT" to a method that requires an object. The parameter
passed must be of exactly the type "object".

I'm sure your next question is "why must it be exactly the type
"object"?" and the answer is that if you were allowed to pass something
other than an "object", then the semantics of the method would not be
precise. That is, the method could change the reference to be something
other than the type you passed in.

It can still do that, of course, since it can assign any other type back
to the "object". But the semantics of the method are precise, in that
it's clear that the only guarantee you have is that you are getting back
an instance of an "object". If you want to put the reference back into
a variable of type "RESPONSE_STRUCT", then at the point in the code
where that happens, the compiler once again has sufficient type
information to do the necessary type validation and throw an exception
if it's illegal.

Pete
 
G

Guest

Peter Duniho said:
TNCoder said:
I get the following error messages when compiling my code:

The best overloaded method match for 'ByteArrayToStructure(byte[], ref
object)' has some invalid arguments Form1.cs
Argument '2': cannot convert from 'ref RESPONSE_STRUCT' to 'ref object'
Form1.cs

The problem is exactly as the error says it is: you are trying to pass a
"RESPONSE_STRUCT" to a method that requires an object. The parameter
passed must be of exactly the type "object".

I'm sure your next question is "why must it be exactly the type
"object"?" and the answer is that if you were allowed to pass something
other than an "object", then the semantics of the method would not be
precise. That is, the method could change the reference to be something
other than the type you passed in.

It can still do that, of course, since it can assign any other type back
to the "object". But the semantics of the method are precise, in that
it's clear that the only guarantee you have is that you are getting back
an instance of an "object". If you want to put the reference back into
a variable of type "RESPONSE_STRUCT", then at the point in the code
where that happens, the compiler once again has sufficient type
information to do the necessary type validation and throw an exception
if it's illegal.

Pete

Thanks, what I did is:

object tempObject = (object)pResponseStruct;

ByteArrayToStructure(pBuffer, ref tempObject);

pResponseStruct = (RESPONSE_STRUCT) tempObject;

Terry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top