How to declare fixed-size arrays?  
Author Message
Vile





PostPosted: Visual C# Language, How to declare fixed-size arrays? Top

I need to declare fixed size array within structure. Like this was in C++

struct SomeStruct

{

int SensorReaderID;

....

float SensorValues[10]; // presume I have 10 sensors

};

Is it possible Or I should use 10 variables like this:

struct SomeStruct

{

int SensorReaderID;

....

// presume I have 10 sensors

float SensorValue0;

float SensorValue1;

float SensorValue2;

....

float SensorValue8;

float SensorValue9;

};



Visual C#12  
 
 
David Anton





PostPosted: Visual C# Language, How to declare fixed-size arrays? Top

Within a struct, you would initialize the array in a constructor.

Within a class, there is no issue. Are you sure you can't use a C# class Remember that a C++ struct is identical to a C++ class except that the default access modifier of members is public, so a C# class with the use of public access modifiers is closer to a C++ struct than a C# struct.

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: VB to Python converter



 
 
Vile





PostPosted: Visual C# Language, How to declare fixed-size arrays? Top

Yes, I know about classes. And, unfortunately, I can't define default constructor for structure (impossible in C#)

Main point to use structures in this case - get them passed to functions by value not by reference. And second point is to force this array to be fixed size. (I need it to be exactly this size - not more/not less and prevent its size from changing).

It's also related to conversion from raw data (byte array, array of structures in binary representation) .


 
 
Mike Danes





PostPosted: Visual C# Language, How to declare fixed-size arrays? Top

Not sure if this helps but actually there is a way you can have a fixed size array in a structure. The problem with it is that it works only in an unsafe context:

[StructLayout(LayoutKind.Sequential)]

unsafe struct Test

{

public int id;

public fixed float values[10];

}

class Program

{

static void Main(string[] args)

{

unsafe

{

Test t = new Test();

t.values[0] = 1.0f;

Console.WriteLine(Marshal.SizeOf(t)); // you get 44 bytes for size

}

}

}


 
 
Vile





PostPosted: Visual C# Language, How to declare fixed-size arrays? Top

public fixed float values[10];

Is it C#

I suppose it is impossible to have fixed size arrays in C#. So probably I will stick to classes and write procedures for data conversion.

Thanks.


 
 
Paul Louth





PostPosted: Visual C# Language, How to declare fixed-size arrays? Top

It is C#, but it's primarily there for interop between COM objects and C#. It would be advisable to write the relevant conversion methods rather than using 'fixed'.
 
 
David Anton





PostPosted: Visual C# Language, How to declare fixed-size arrays? Top

From the C# documentation:

The fixed statement prevents the garbage collector from relocating a movable variable. The fixed statement is only permitted in an unsafe context. Fixed can also be used to create fixed size buffers.

So, it's not appropriate to use "fixed" in the way you did (it's also not doing what you think it is).

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: VB to Python converter



 
 
Mike Danes





PostPosted: Visual C# Language, How to declare fixed-size arrays? Top

"it's also not doing what you think it is"

Well, it does not do what I think, it does what the docs say:

"C# 2.0 adds the ability to embed an array of fixed size in a struct when used in an unsafe code block."

That's from the link named "fixed size buffers" from you quote.

And it happens to be exactly what Vile requested. And it also happens to be what I think it does.


 
 
David Anton





PostPosted: Visual C# Language, How to declare fixed-size arrays? Top

You're right. I just saw it in isolation in the post I replied to. In the context of your original 'unsafe' example it makes sense.

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: VB to Python converter