Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

tableau d'objets

6 réponses
Avatar
BR
Bonjour,

Dans une doc, j'ai vu ceci, qui m'interesse :
Il s'agit d'un membre de la classe EMPLOYE, Projet étant une autre classe
définie dans le même espace de nom.
.....
private Projet[] _SesProjets;
public Projet this[int i]
{
get
{ return _SesProjets[i]; }
set
{ _SesProjets[i] = value; }
}
....

Dans mon programme, j'instancie un employe, dans le constructeur,
j'instancie le tableau, mais je n'ai pas accès au tableau...
En fait j'aurais aimé savoir si la syntaxe est bonne et si oui, comment
accéder au tableau ??
Merci

Bonne journée
Benoît

6 réponses

Avatar
zoltix
BR a écrit :
Bonjour,

Dans une doc, j'ai vu ceci, qui m'interesse :
Il s'agit d'un membre de la classe EMPLOYE, Projet étant une autre classe
définie dans le même espace de nom.
.....
private Projet[] _SesProjets;
public Projet this[int i]
{
get
{ return _SesProjets[i]; }
set
{ _SesProjets[i] = value; }
}
....

Dans mon programme, j'instancie un employe, dans le constructeur,
j'instancie le tableau, mais je n'ai pas accès au tableau...
En fait j'aurais aimé savoir si la syntaxe est bonne et si oui, comment
accéder au tableau ??
Merci

Bonne journée
Benoît



Je pense que tu devrais faire la même chose avec les colletions.

.NET 2.0
using System.Collections.Generic;
.....


private List<Projet> _SesProjets= new List<Projet>();

public Projet this[int i]
{
get
{ return _SesProjets[i]; }
set
{ _SesProjets.add = value; }
}
....

.NET 2.1

.....

.NET 1.0 et 1.1
using System.Collections;

private ArrayList _SesProjets= new ArrayList();

public Projet this[int i]
{
get
{ return (Projet) _SesProjets[i]; }
set
{ _SesProjets.add = value; }
}
....


J'ai fait ca de memoire donc il y'a sans doute des fautes mais l'idée est la.
Avatar
BR
Ok, c'est bien, mais je ne sais pas comment utiliser ceci ... Comment un
objet accède au tableau ou à la collection ?
Merci beauoup
Benoît

"zoltix" a écrit :

BR a écrit :
> Bonjour,
>
> Dans une doc, j'ai vu ceci, qui m'interesse :
> Il s'agit d'un membre de la classe EMPLOYE, Projet étant une autre classe
> définie dans le même espace de nom.
> .....
> private Projet[] _SesProjets;
> public Projet this[int i]
> {
> get
> { return _SesProjets[i]; }
> set
> { _SesProjets[i] = value; }
> }
> ....
>
> Dans mon programme, j'instancie un employe, dans le constructeur,
> j'instancie le tableau, mais je n'ai pas accès au tableau...
> En fait j'aurais aimé savoir si la syntaxe est bonne et si oui, comment
> accéder au tableau ??
> Merci
>
> Bonne journée
> Benoît
>
Je pense que tu devrais faire la même chose avec les colletions.

..NET 2.0
using System.Collections.Generic;
.....


private List<Projet> _SesProjets= new List<Projet>();

public Projet this[int i]
{
get
{ return _SesProjets[i]; }
set
{ _SesProjets.add = value; }
}
....

..NET 2.1

.....

..NET 1.0 et 1.1
using System.Collections;

private ArrayList _SesProjets= new ArrayList();

public Projet this[int i]
{
get
{ return (Projet) _SesProjets[i]; }
set
{ _SesProjets.add = value; }
}
....


J'ai fait ca de memoire donc il y'a sans doute des fautes mais l'idée est la.



Avatar
Paul Bacelar
Utilisez "_SesProjets" ???:-)
--
Paul Bacelar
MVP VC++


"BR" wrote in message
news:
Ok, c'est bien, mais je ne sais pas comment utiliser ceci ... Comment un
objet accède au tableau ou à la collection ?
Merci beauoup
Benoît

"zoltix" a écrit :

BR a écrit :
> Bonjour,
>
> Dans une doc, j'ai vu ceci, qui m'interesse :
> Il s'agit d'un membre de la classe EMPLOYE, Projet étant une autre
> classe
> définie dans le même espace de nom.
> .....
> private Projet[] _SesProjets;
> public Projet this[int i]
> {
> get
> { return _SesProjets[i]; }
> set
> { _SesProjets[i] = value; }
> }
> ....
>
> Dans mon programme, j'instancie un employe, dans le constructeur,
> j'instancie le tableau, mais je n'ai pas accès au tableau...
> En fait j'aurais aimé savoir si la syntaxe est bonne et si oui, comment
> accéder au tableau ??
> Merci
>
> Bonne journée
> Benoît
>
Je pense que tu devrais faire la même chose avec les colletions.

..NET 2.0
using System.Collections.Generic;
.....


private List<Projet> _SesProjets= new List<Projet>();

public Projet this[int i]
{
get
{ return _SesProjets[i]; }
set
{ _SesProjets.add = value; }
}
....

..NET 2.1

.....

..NET 1.0 et 1.1
using System.Collections;

private ArrayList _SesProjets= new ArrayList();

public Projet this[int i]
{
get
{ return (Projet) _SesProjets[i]; }
set
{ _SesProjets.add = value; }
}
....


J'ai fait ca de memoire donc il y'a sans doute des fautes mais l'idée est
la.





Avatar
zoltix
BR a écrit :
Ok, c'est bien, mais je ne sais pas comment utiliser ceci ... Comment un
objet accède au tableau ou à la collection ?
Merci beauoup
Benoît

"zoltix" a écrit :

BR a écrit :
Bonjour,

Dans une doc, j'ai vu ceci, qui m'interesse :
Il s'agit d'un membre de la classe EMPLOYE, Projet étant une autre classe
définie dans le même espace de nom.
.....
private Projet[] _SesProjets;
public Projet this[int i]
{
get
{ return _SesProjets[i]; }
set
{ _SesProjets[i] = value; }
}
....

Dans mon programme, j'instancie un employe, dans le constructeur,
j'instancie le tableau, mais je n'ai pas accès au tableau...
En fait j'aurais aimé savoir si la syntaxe est bonne et si oui, comment
accéder au tableau ??
Merci

Bonne journée
Benoît



Je pense que tu devrais faire la même chose avec les colletions.

..NET 2.0
using System.Collections.Generic;
.....


private List<Projet> _SesProjets= new List<Projet>();

public Projet this[int i]
{
get
{ return _SesProjets[i]; }
set
{ _SesProjets.add = value; }
}
....

..NET 2.1

.....

..NET 1.0 et 1.1
using System.Collections;

private ArrayList _SesProjets= new ArrayList();

public Projet this[int i]
{
get
{ return (Projet) _SesProjets[i]; }
set
{ _SesProjets.add = value; }
}
....


J'ai fait ca de memoire donc il y'a sans doute des fautes mais l'idée est la.





Comprends pas trop.......... mais pour accéder l'intérieur de la classe _SesProjets..... et de l'extérieur avec le nom de
l'instance de ta classe.
Avatar
BR
Je reprends le problème :
class Employe
{
private int num;
...
private Projet[] _SesProjets;
public Projet this[int i]
{
get
{ return _SesProjets[i]; }
set
{ _SesProjets[i] = value; }
...
}
J'ai aussi une classe projet.
instanciation d'un employe
Employe UnEmploye = new Employe(.....)
c'est ensuite que je n'arrive pas à accéder à ses projets :
UnEmploye.???? /// que dois-je faire pour
ou encore :
foreach (Projet P in ???)
Je ne vois pas la collection !!!
j'ai aussi essayé :
UnEmploye[0] = new Projet("P1", "Test");
... mais ça plante à l'exe !!!
merci pour les réponses
BR
Avatar
zoltix
BR a écrit :
Je reprends le problème :
class Employe
{
private int num;
...
private Projet[] _SesProjets;
public Projet this[int i]
{
get
{ return _SesProjets[i]; }
set
{ _SesProjets[i] = value; }
...
}
J'ai aussi une classe projet.
instanciation d'un employe
Employe UnEmploye = new Employe(.....)
c'est ensuite que je n'arrive pas à accéder à ses projets :
UnEmploye.???? /// que dois-je faire pour
ou encore :
foreach (Projet P in ???)
Je ne vois pas la collection !!!
j'ai aussi essayé :
UnEmploye[0] = new Projet("P1", "Test");
... mais ça plante à l'exe !!!
merci pour les réponses
BR




Je ne comprends pourquoi tu obstines a travailler avec des array.


C'est pas Top ...... mais voila

class Projet
{
}
class Employe
{
private List<Projet> _SesProjet = new List<Projet>();
public List<Projet> getSesProjet()
{
return _SesProjet;
}
public Projet this[int i]
{
get
{ return (Projet)_SesProjet[i]; }
set
{ _SesProjet.Add(value); }
}
}














List<Projet> _SesProjets = new List<Projet>();
Employe nEmploye = new Employe();
Projet prj0 = new Projet();
Projet prj1 = new Projet();
Projet prj2 = new Projet();

nEmploye[0] =prj0;
nEmploye[1] = prj1;
nEmploye[2] = prj2;

List<Projet> prjs = nEmploye.getSesProjet();
foreach (Projet currentPrj in prjs)
{
Console.Write(currentPrj.ToString());
}