IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

comment implémenter []


Sujet :

C#

  1. #1
    Membre actif Avatar de 5:35pm
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    201
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 201
    Points : 217
    Points
    217
    Par défaut comment implémenter []
    Bonjour, le titre n'est peut être pas très explicite,
    j'ai un tableau: Property[] properties.
    J'aurais besoin de pouvoir obtenir un élément du tableau à partir d'un string, comme un Dictionnary:

    Property maPropriete = properties["discriminationPositive"];

    si j'ai bien compris je dois créer une collection PropertyCollection?
    pouvez vous m'éclairer merci =)

  2. #2
    Membre du Club Avatar de Yodabis
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2007
    Messages
    55
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 55
    Points : 58
    Points
    58
    Par défaut
    Bonjour, question innocente :
    es-tu obligé de partir d'un tableau?

    Avec une List<Property> ou un dictionnary tu as directement plus de possibilités ...

  3. #3
    Membre actif Avatar de 5:35pm
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    201
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 201
    Points : 217
    Points
    217
    Par défaut
    es-tu obligé de partir d'un tableau?
    justement non, pour le moment j'ai un tableau, mais j'ai l'intention d'utiliser une collection personnalisé, (PropertyCollection) notamment parce que j'aimerai que cette collection implémente des opérations sur toute les Property.

  4. #4
    Membre éclairé Avatar de ZaaN
    Inscrit en
    Novembre 2005
    Messages
    819
    Détails du profil
    Informations forums :
    Inscription : Novembre 2005
    Messages : 819
    Points : 661
    Points
    661
    Par défaut
    j avais un truc comme ca pour acceder un tableau de ce style.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    protected myType[,] myArray;

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public myType this[uint _uiRow, uint _uiLine]
            {
                set
                {
                   //verifier index
                       //verifier le nouvel elem
                           //renregistrer le nouvel elem
                }
                get
                {
                    //verifier index
                         //retourner l elem
                }
            }
    Pour les details, cherche tout seul !

  5. #5
    Membre actif Avatar de 5:35pm
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    201
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 201
    Points : 217
    Points
    217
    Par défaut
    ok j'ai trouvé, je dois dériver PropertyCollection de DictionaryBase.

  6. #6
    Membre du Club Avatar de Yodabis
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2007
    Messages
    55
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 55
    Points : 58
    Points
    58
    Par défaut
    Ok, moi je ferais :

    List<Property> properties = new List<Property>();

    et appliquer une méthode à ts les items via

    foreach(Property p in properties)
    Méthode(p);

    je peux faire un find avec
    Property myP =
    properties.Find(delegate(Property p)
    { return p.StringId == "discriminationPositive"; }

  7. #7
    Membre actif Avatar de 5:35pm
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    201
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 201
    Points : 217
    Points
    217
    Par défaut
    Citation Envoyé par Yodabis Voir le message
    Ok, moi je ferais :

    List<Property> properties = new List<Property>();

    et appliquer une méthode à ts les items via

    foreach(Property p in properties)
    Méthode(p);

    je peux faire un find avec
    Property myP =
    properties.Find(delegate(Property p)
    { return p.StringId == "discriminationPositive"; }
    oui, je connais bien ça.
    mais en faisant comme ça, d'une part ce n'est pas un conteneur associatif (et faudra systematiquement iterer pour trouver un élément ou faire un predicate et utiliser find), d'autre part toute les opérations de groupe sont a l'extérieur de la collection.

    Mais j'ai trouvé encore mieux que DictionaryBase (qui n'est pas générique).
    c'est l'interface IDictionnary<key,value>, ce qui donne:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
        public class PropertyCollection : IDictionary<string,AbstractProperty>
        {
     
            #region IDictionary<string,AbstractProperty> Membres
     
     
            public bool ContainsKey(string key)
            {
                throw new Exception("The method or operation is not implemented.");
            }
     
            ICollection<string> IDictionary<string, AbstractProperty>.Keys
            {
                get { throw new Exception("The method or operation is not implemented."); }
            }
     
            public bool Remove(string key)
            {
                throw new Exception("The method or operation is not implemented.");
            }
     
            public bool TryGetValue(string key, out AbstractProperty value)
            {
                throw new Exception("The method or operation is not implemented.");
            }
     
            public ICollection<AbstractProperty> Values
            {
                get { throw new Exception("The method or operation is not implemented."); }
            }
     
            #endregion
     
            #region ICollection<KeyValuePair<string,AbstractProperty>> Membres
     
            public void Add(KeyValuePair<string, AbstractProperty> item)
            {
                throw new Exception("The method or operation is not implemented.");
            }
     
            public bool Contains(KeyValuePair<string, AbstractProperty> item)
            {
                throw new Exception("The method or operation is not implemented.");
            }
     
            public void CopyTo(KeyValuePair<string, AbstractProperty>[] array, int arrayIndex)
            {
                throw new Exception("The method or operation is not implemented.");
            }
     
            public bool IsReadOnly
            {
                get { throw new Exception("The method or operation is not implemented."); }
            }
     
            public bool Remove(KeyValuePair<string, AbstractProperty> item)
            {
                throw new Exception("The method or operation is not implemented.");
            }
     
            #endregion
     
            #region IEnumerable<KeyValuePair<string,AbstractProperty>> Membres
     
            public new IEnumerator<KeyValuePair<string, AbstractProperty>> GetEnumerator()
            {
                throw new Exception("The method or operation is not implemented.");
            }
     
            #endregion
        }

  8. #8
    Membre actif Avatar de 5:35pm
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    201
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 201
    Points : 217
    Points
    217
    Par défaut
    Quoi que finalement DictionnaryBase est pas plus mal:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
        public class PropertyCollection : DictionaryBase
        {
            public AbstractProperty this[string key]
            {
                get { return (AbstractProperty)this.Dictionary[key]; }
     
                set { this.Dictionary[key] = value; }
            }
            //add a phone number based on key 
     
            public void Add(string key, AbstractProperty property)
            {
                this.Dictionary.Add(key, property);
            }
            //see if collection contains an entry corresponding to key
     
            public bool Contains(string key)
            {
                return this.Dictionary.Contains(key);
            }
     
            //we will get to this later
     
            public ICollection Keys
            {
                get { return this.Dictionary.Keys; }
            }
        }
    cette solution est seulement casse couille pour les foreach...
    mmm je me tate...

  9. #9
    Membre du Club Avatar de Yodabis
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2007
    Messages
    55
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 55
    Points : 58
    Points
    58
    Par défaut
    Et une HashTable ?

  10. #10
    Membre émérite Avatar de Guulh
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    2 160
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Septembre 2007
    Messages : 2 160
    Points : 2 925
    Points
    2 925
    Par défaut
    Et un Dictionary<TKey, TValue> ?
    ಠ_ಠ

  11. #11
    Membre actif Avatar de 5:35pm
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    201
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 201
    Points : 217
    Points
    217
    Par défaut
    pas essayé, mais je tiens la solution, la vrai, la simple, la performante:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     
        public class PropertyCollection : System.Collections.Generic.SortedDictionary<string, AbstractProperty>
        {
             //opérations de sur les properties
        }
    j'ai pas trouvé le bouton "doublement résolu"

  12. #12
    Membre actif Avatar de 5:35pm
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    201
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2006
    Messages : 201
    Points : 217
    Points
    217
    Par défaut
    Et un Dictionary<TKey, TValue> ?
    bien vu =)
    c'était pourtant évident non?

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Comment implémenter lemonldap?
    Par Aldo dans le forum Apache
    Réponses: 7
    Dernier message: 25/01/2007, 21h32
  2. [VB.Net]Comment implémenter un Simulateur de combats?
    Par stargatejojo dans le forum Windows Forms
    Réponses: 19
    Dernier message: 20/04/2006, 16h04
  3. Réponses: 4
    Dernier message: 07/04/2006, 18h08
  4. Réponses: 2
    Dernier message: 02/12/2005, 17h22
  5. Comment implémenter un Datawarehouse ?
    Par raslain dans le forum Alimentation
    Réponses: 2
    Dernier message: 20/10/2005, 11h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo