Hi, C# Gurus,
I'm switching from MFC/C++ to .Net/C# recently.
Really miss the POINTER, you can convert any pointer to any type easily.
I have a case need some help, please.
Here's the C++ code :
I have a byte array myArr[100] contains data read in from COM1.
myArr = {1,2,3,11,12,13,. . . . . . . . . . . .}
and classes CX, CY, CZ . . . . . . . .
class CX
{
byte a;
byte b;
byte c;
.
.
};
I can convert the byte array to any of these classes :
CX *pCX = (CX * )myArr ;
and start refer the data in the array :
pCX->a, pCX->c . . . . . .
later on for new data in myArr, I can convert the byte array to aanother class :
CZ *pCZ = (CZ * )myArr ;
pCZ->a, pCZ->b . . . . . .
Now in C# :
I convert it this way :
CX oCX = Convert.ChangeType(myArr, typeof(myArr));
compiler clean, but when I run it, I got exception.
"An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll
Additional information: Object must implement IConvertible."
If anybody can help will be greatly appreciated, Thank You.
Steed

Convert array to Class
tomazek
Thanks, Michael
You are exactly right, Actually I simplified my example, my class CX, CY... inherit structures with bytes, ints, chars,
string, and each struct contains different members.
That's why I try to convert (more precisely TYPECAST) the byte array, so I can easily to process the incoming data.
I don't want to get an instance of class X. The myArr is 2000 bytes. If I copy myArr to class X, like byte a = [0],
char b = [1]*256+[2], Buffer.BlockCopy (myArr,29, string str, 0, 49), I might as well just go straight to use
myArr[0] . . . myArr[29],[30],[31]...
How about TypeConverter any insightful info.
Thanks.
Steed
Dan Ferguson
The behavior you see in C++ only works because of how the class is laid out in memory and technically it could fail at some future point. You are relying on the fact that your class will be represented in memory as byte, byte, byte which is probably true today but might not be true tomorrow given any number of reasons such as padding by the compiler, introduction of a base class, virtual table changes, etc. In other words your C++ code is really dangerous. Technically you would use a struct instead which (although identical to a class) gives others some clue as to what you are trying to do.
Nevertheless this won't work in C# as the layout of objects in memory is completely arbitrary and can change even between compilations. You must write the code that takes a byte array and converts it to a CX instance. The preferred approach (unlike C++) is to write a method that does the conversion explicitly (such as CX.ConvertFromByteArray). This is sort of why Convert exists for the base types. You could technically implement the IConvertible interface but honestly that interface is designed for converting between related types not for converting between memory representations. Finally you could consider using serialization (binary in this case) to convert objects to and from binary.
Here's my attempt at your ConvertFromByteArray() method:
public class X //Note that denoting classes with "C" is non-standard in .NET
{
private:
byte a, b, c;
public:
public void ConvertFromByteArray ( byte[] bytes )
{
//Handle case where bytes is not long enough
...
a = bytes[0];
b = bytes[1];
c = bytes[2];
}
}
Michael Taylor - 6/9/06
Anonym0us
TypeConverter is designed to convert between type not really for typecasting. You could probably get it to work but I'm not sure it is the best approach. I think binary serialization is the best way for you to go if possible. It solves all your problems.
I'm afraid the typecasting trick of C++ won't work reliably in .NET. However if you are willing to stick with raw structures then you can mark the structure as StructLayout(LayoutKind.Sequential) then it'll have the exact format you specified. You then might be able to cast the byte array directly as an instance of the structure. I haven't tried that exact scenario but it seems like it'd work.
[StructLayout(LayoutKind.Sequential)]
public struct CX
{
byte a;
byte b;
byte c;
}
//Later on
CX cx = (CX)byteArray;
To emulate inheritance you could use nested structures like so:
public struct Base
{
byte baseMember;
}
public struct Derived
{
Base base;
byte derivedMember;
}
You'll want to put a wrapper around this structure stuff to expose it to .NET clients as a normal class but I think it'd work if you wanted to do it that way.
Michael Taylor - 6/9/06
Armoghan
imatureStudent
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2834417&SiteID=1&mode=1
Justin20009
Thanks, Michael, I didn't see your helpful e-mail till Monday morning back to office.
I'll try your method.
Appriciate your help.
Steed 6/12/2006