Autor Beitrag
Schulteatq
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 16



BeitragVerfasst: Do 06.04.06 19:41 
Moin,

eine kleine Frage bezüglich Array Indizierung. Ist es möglich ein Array-Feld durch anderes Typen als int zu indizieren. In meinem konkreten Fall möchte ich ein Array durch ein Point-ähnliches struct indizieren um mehrdimensionale Arrays zu meiden.

Ich habe schon einiges über Indexer gelesen, jedoch gelten die nur für ganze Klassen und nicht für einzelne Felder, scheinen hier also nicht nützlich zu sein. :-/

mfg und danke im Vorraus
Schulteatq
JayK
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1013



BeitragVerfasst: Do 06.04.06 19:45 
Ich weiß nicht, vielleicht hilft dir SortedList? siehe msdn2.microsoft.com/...ms132319(VS.80).aspx
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 06.04.06 19:49 
Schau Dir mal die Dictionary<,>-Klasse an :-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Robert_G
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416


Delphi32 (D2005 PE); Chrome/C# (VS2003 E/A, VS2005)
BeitragVerfasst: Do 06.04.06 20:03 
nene, ein Array ist in .Net wird immer mit einem int indexiert.

Wenn du _wirklich_ über Point als Index zugriefen willst, könntest du dir einen Wrapper bauen, der zuweisungskompatibel ist (implizite Umwandlung).
Hier mal ein abstrakter Bleistift:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
struct ArrayWrapper<T>
{
  T[,] array;

  public T this[Point index]
  {
    get { return array[index.X, index.Y]; }
    set { array[index.X, index.Y] = value; }
  }

  public T this[int index1, int index2]
  {
    get { return array[index1, index2]; }
    set { array[index1, index2] = value; }
  }

  ArrayWrapper(T[,] array)
  {
    this.array = array;
  }

  public static implicit operator ArrayWrapper<T>(T[,] value)
  {
    return new ArrayWrapper<T>(value);
  }

  public static implicit operator T[,](ArrayWrapper<T> value)
  {
    return value.array;
  }
}

class Program
{
  static void Main(string[] args)
  {

    ArrayWrapper<int> wrapper = new int[31];

    Point point00 = new Point(00);
    Point point01 = new Point(01);
    Point point10 = new Point(10);
    Point point11 = new Point(11);

    wrapper[point00] = 1;
    wrapper[point01] = 2;
    wrapper[point10] = 3;
    wrapper[point11] = 4;

    wrapper[30] = 5;
    wrapper[31] = 6;

    int[,] values = wrapper;


    foreach (int i in values)
      Console.WriteLine(i);
  }
}

Für eine wirklich gute Idee halte ich es aber nicht...

@christian
Dictionary<Point, int> würde sich zwar nicht genauso verhalten, aber genau genug. Boah hatte wohl die sprichwörtichen Tomaten auf den Augen :eyecrazy: :lol: