Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You cannot achieve what you want to do without having at least a root class containing your data. Here is a solution which works for a stripped-down version of your problem, namely being able to access field <code>a</code> of your <code>Struct2</code> in your pseudo-array, such as:</p> <pre><code>wrapper wr = new wrapper (); // needs to be a class wr[0].a = 123; wr[1].a = 456; System.Console.WriteLine ("wr[0].a = {0}", wr[0].a); // displays 123 System.Console.WriteLine ("wr[1].a = {0}", wr[1].a); // displays 456 </code></pre> <p>Your wrapper must return a reference type if you want to be able to modify its contents, or else you will always hit your head against the value-type copying taking place when you access structs. But your wrapper may still be storing its data internally as a series of structs.</p> <p>Here is my solution:</p> <pre><code>struct Struct2 { public int a; } class wrapper // sorry, cannot use 'struct' here ... { Struct2 str0; Struct2 str1; public helper this[int index] { get { return new helper (this, index); } } int GetValueA(int index) { switch (index) { case 0: return str0.a; case 1: return str1.a; default: throw new System.IndexOutOfRangeException (); } } void SetValueA(int index, int value) { switch (index) { case 0: str0.a = value; break; case 1: str1.a = value; break; } } public class helper { public helper(wrapper host, int index) { this.host = host; this.index = index; } public int a { get { return this.host.GetValueA (index); } set { this.host.SetValueA (index, value); } } private readonly wrapper host; private readonly int index; } } </code></pre> <p>As your concern seems to be speed, then no wrapper will make you happy. I would reconsider the whole problem and, if at all possible, write a class to manage your data structure.</p> <p>If all your data can be represented as <code>int</code>, maybe you should consider using a huge array of integers and then add classes which access that one central array to locate the fields you want to manipulate, by indexing into the proper item.</p> <pre><code>class Wrapper { ... int[] data; public StructWrapper1 this[int index] { get { return new StructWrapper1 (this, index); } } public class StructWrapper1 { public StructWrapper1(Wrapper wrapper, int index) { this.wrapper = wrapper; this.index = index; } public int A { get { return this.wrapper[this.index*2+0]; } set { this.wrapper[this.index*2+0] = value; } } public int B { get { return this.wrapper[this.index*2+1]; } set { this.wrapper[this.index*2+1] = value; } } private readonly Wrapper wrapper; private readonly int index; } } </code></pre> <p>If you need to represent various data types, you could consider using one array for every field type.</p>
 

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