[C#] Smart field d'un tableau
Bonjour,
j'aimerais savoir comment je peux définir un 'smart field' sur un tableau d'objets.
J'ai déclaré ce tableau, après dans le constructeur de la classe j'ai créé et j'ai rempli mon tableau, puis j'ai défini un smart field comme suit :
Code:
1 2 3 4
| public int TabA
{
get { return this.a; }
} |
mais évidemment lors de la compilation j'ai eu l'erreur CS0201 (Only assignment, call, increment, decrement, and new object expressions can be used as a statement).
J'ai essayé de changé l'entête du smart field par un , mais ça n'a rien apporté.
Quelqu'un pourrait me dire comment faire et d'où vient l'erreur?
Merci bcp d'avance et bonne journée à tous.
JCBA
Re: [C#] Smart field d'un tableau
Je viens de tester ceci et celà fonctionne parfaitement
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public class truc
{
public truc
{
this.a = new int[4];
for(int i=0; i < 4; i++)
{this.a[i] = i;}
}
public int[] TabA
{
get { return this.a; }
}
private int[] a;
} |
test dans un projet console
Code:
1 2 3 4
| truc t = new truc();
foreach(int i in t.TabA)
{ Console.WriteLine(i.ToString());}
Console.Read(); |
Resultat
Re: [C#] Smart field d'un tableau
idem çà fonctionne toujours parfaitement :lol:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
using System;
using System.Drawing.Imaging;
//...
public class myFormats
{
public myFormats()
{
this.formats = new ImageFormat[]{ImageFormat.Bmp, ImageFormat.Gif, ImageFormat.Jpeg};
}
public ImageFormat[] ImageFormatArray
{
get { return this.formats; }
}
private ImageFormat[] formats;
} |
test dans un projet console
Code:
1 2 3 4 5 6
|
myFormats f = new myFormats();
ImageFormat imageF;
foreach (int imageF in f.ImageFormatArray)
{ Console.WriteLine(imageF.ToString()); }
Console.Read(); |
Resultat