Behrooz
Behrooz

Reputation: 1734

Cannot take the address of, get the size of, or declare a pointer to a managed type ('type name')

this error stops compiling if i have one or more System.String in my structs
is there any other way to store strings?

i have tried things like this:

private long _B_ID;
private byte[] _C_Name;
private byte[] _C_Address;
private byte[] _C_Telephone;

but it is not seeming to work.

Upvotes: 3

Views: 6561

Answers (3)

CodesInChaos
CodesInChaos

Reputation: 108830

You can only get the address of a struct if it is blittable, i.e. it doesn't contain references.
See Blittable and Non-Blittable Types(MSDN)

Upvotes: 5

Swanand
Swanand

Reputation: 4115

You can not declare a Pointer to Non-fixed types even if you use "unsafe". To remove this error, You HAVE to specify size of the arrays along with keyword fixed.

For Example, _C_Name contains 25 bytes. So declare it as:

private fixed byte _C_Name[25];

Strings are Fixed typed so you was not getting error when you declare them as String. Hope it works for you!

Regards!

Upvotes: 1

dalle
dalle

Reputation: 18527

Try the MarshalAs attribute:

struct Foo
{
    [MarshalAs(UnmanagedType.LPStr]
    string s;
}

Upvotes: 0

Related Questions