Note that there are some explanatory texts on larger screens.

plurals
  1. POStruct wrapping value types and giving access with an index
    primarykey
    data
    text
    <p>I need to encapsulate an fixed array of a user defined value type (let's call it struct2 ) inside an other struct (struct1) but fixed arrays can be declared only for native value types. So i thought to create a third struct (struct wrapper) that has to work as an array of the struct2 by defining the [] operator. So </p> <pre><code> struct Struct2 { int a; int b } struct wrapper { Struct2 str0; Struct2 str1; public Struct2 this[int index] { get { switch ( index ) { case 0: return str0; case 1: return str1; default: return str0; } } } } static void Main() { wrapper wr = new wrapper(); wr[0].a = 123; // i would like to do this but this doesnt work //but if we do like this Struct2 [] arr = new Struct2[5]; arr[0].a = 123 ;// this works , we are effectively modifying the object //contained in the array (not just a copy) } </code></pre> <p>Ok, this code doesn't work because Struct2 is a value type and when the operator returns then it returns a copy and not the actual object str0 contained in it. I would like to access that field using an indexer ! is it possible ? Why it's possible with the Array class ? I know that it's possible by returning a pointer but this involve using the fixed keyword inside the operator definition and i would like to avoid that because the 'array' need to be accessed extensively and i would end up by using the fixed statement 2 times (inside and outsid to keep the address fixed). Also i already considered using pointers by just declaring N adiacent Struct2 fields in Struct1 and using a pointer to the first one as an array but i would prefer to use the wrapper to emulate an array behaviour. Agai , is it possible?</p> <p>Edit It seems that it's not possible to implement a custom array that works with value types(as common arrays do). Btw the nearest solution i could find is this, but as i wrote i would have liked to avoid pointers.</p> <pre><code> struct Struct2 { int a; int b } struct Struct1 { public Struct2 * str2arr ; //this can be avoided public Struct2 str0; //wrapper is not needed anymore as Struct1 is wrapping the Struct2 array by itself //and a pointer is used for indexing operations //struct layout needs to be sequential private Struct2 str1; private Struct2 str2; private Struct2 str3; private Struct2 str4; } static void Main() { Struct1 myStruct = new Struct1(); fixed(myStruct.str2arr = &amp;myStruct.str0) { myStruct.str2arr[1] = 123; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload