Structure Layout

J

jack

Hi,
Is there anyway to have such a structure in c# (safe and managed)?

struct MyStruct1
{
Byte data[10];
}

i.e., I would like to define a structure that's 10 bytes long.

Now, let's look at this struct:

struct MyStruct2
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
Byte[] data;
}

What does it mean?

Thanks
Jack
 
D

David Anton

Are you sure you need a 'struct' and not a class? Remember, in C++ (your
original sample is C++) a struct is virtually identical to a C++ class.
The following will work fine, unless you really need the value type
semantics of a struct:
public class MyStruct1
{
public byte[] data = new byte[10];
}
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++
 
D

David Anton

Alternatively, you could use a struct, and create the array in the constructor.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++


David Anton said:
Are you sure you need a 'struct' and not a class? Remember, in C++ (your
original sample is C++) a struct is virtually identical to a C++ class.
The following will work fine, unless you really need the value type
semantics of a struct:
public class MyStruct1
{
public byte[] data = new byte[10];
}
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++


jack said:
Hi,
Is there anyway to have such a structure in c# (safe and managed)?

struct MyStruct1
{
Byte data[10];
}

i.e., I would like to define a structure that's 10 bytes long.

Now, let's look at this struct:

struct MyStruct2
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
Byte[] data;
}

What does it mean?

Thanks
Jack
 
G

Göran Andersson

David said:
Alternatively, you could use a struct, and create the array in the constructor.

However, be adviced that using a struct that contains a mutable object
can be tricky...
 

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