views:

493

answers:

7

I am using a struct to pass to an unmanaged DLL as so -

[StructLayout(LayoutKind.Sequential)]
        public struct valTable
        {
            public byte type;
            public byte map;
            public byte spare1;
            public byte spare2;
            public int par;
            public int min;
            public byte[] name;
            public valTable()
            {
                name = new byte[24];
            }
        }

The code above will not compile because VS 2005 will complain that "Structs cannot contain explicit parameterless constructors". In order to pass this struct to my DLL, I have to pass an array of struct's like so valTable[] val = new valTable[281];

What I would like to do is when I say new, the constructor is called and it creates an array of bytes like I am trying to demonstrate because the DLL is looking for that byte array of size 24 in each dimension.

How can I accomplish this?

A: 

For what you need to do you really need a class I think. A struct already implements a parameterless constructor by default which is why you cant define another one.

pierre
He's passing this struct to a dll.
Adam Robinson
+2  A: 

Struct constructors are similar to class constructors, except for the following differences:

  • Structs cannot contain explicit parameterless constructors. Struct members are automatically initialized to their default values.
  • A struct cannot have an initializer in the form: base (argument-list).

This means that

A default(parameterless) constructor for a struct could set different
values than the all-zeroed state which would be unexpected behavior. The
.Net Runtime therefore prohabits default constructors for struct.

The typical way to get around this scenario is to create a static method that will create your new instance, initialize it the way you want, and return it. This is the way it is done in .NET to get structures initialized with specific values.


ref; Structs cannot contain explicit parameterless constructors. WHY?

Asad Butt
A: 

Not the cleanest fix, but you could just add a parameter and never use it?

[StructLayout(LayoutKind.Sequential)]
    public struct valTable
    {
        public byte type;
        public byte map;
        public byte spare1;
        public byte spare2;
        public int par;
        public int min;
        public byte[] name;
        public valTable(int x)
        {
            name = new byte[24];
        }
    }
applechewer
I voted -1 on this one since it promotes code that is misleading and not very logical. There are cleaner ways to do this (such as using a static create method).
Blixt
+13  A: 

You can use a fixed size buffer - which I suspect you really want anyway, so as to get the data "inline" in the struct (rather than a reference to an array elsewhere).

public fixed byte name[24];

You'll need to declare the struct as unsafe as well though.

Note that any "solution" which requires calling a static method or providing any kind of custom constructor will fail with your explicit goal of being able to create an array of these structs.

Jon Skeet
+1. Nice, I missed that.
Mitch Wheat
http://msdn.microsoft.com/en-us/library/zycewsya(VS.80).aspx
Mitch Wheat
I had no idea that the `fixed` keyword could be used like this, and I have wanted something like this several times. I....I....I love you, man!
Jeffrey L Whitledge
@Jeffrey: Can't say I've ever used it myself, but I had to write about it for C# in Depth :)
Jon Skeet
@Mitch: Thanks, I've added the link into the answer.
Jon Skeet
@Jon Skeet - I have C# in Depth, and I read it and loved it. Somehow I missed that part. I once put nine integer variables in a struct and mocked-up array access using a switch statement, just to have a stack-allocated array!
Jeffrey L Whitledge
@Jeffrey: be careful with it. It's easy to get it wrong. See http://blogs.msdn.com/ericlippert/archive/2009/08/27/what-s-the-difference-between-fixed-and-fixed.aspx for some thoughts on that. Frankly, much of the time I'd probably go with your nine-integer solution. :-)
Eric Lippert
@Eric Lippert - Thanks for the link! I will be careful. :-)
Jeffrey L Whitledge
+3  A: 

i would recommend to write this code.

    [StructLayout(LayoutKind.Sequential)]
    public struct valTable
    {
        public byte type;
        public byte map;
        public byte spare1;
        public byte spare2;
        public int par;
        public int min;
        public byte[] name;

        static public valTable Create()
        {
            valTable vT = new valTable();
            vT.name = new byte[24];
            return vT;
        }
    }
Andrey
+1  A: 

Building on Asad Butt's answer, you can create a static method to perform the work of your constructor like so:

[StructLayout(LayoutKind.Sequential)]
public struct valTable
{
    public byte type;
    public byte map;
    public byte spare1;
    public byte spare2;
    public int par;
    public int min;
    public byte[] name;
    public valTable()

    public static valTable NewTable()
    {
        valTable tbl = new valTable();
        tbl.name = new byte[24];
        return tbl;
    }
}

You'll see classes in the .NET Framework following this pattern already. Guid.NewGuid() immediately springs to mind. There may be others.

Programming Hero
this would have been my first choice but Jon Skeet's seems simpler
0A0D
A: 

but why there is necessity of implementing parameterless constructor in structure.Can Anybody explain with example.

dipak marathe