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 :

initialisation à la déclaration : marche pas :(


Sujet :

C#

  1. #1
    Membre très actif Avatar de Ragmaxone
    Homme Profil pro
    Responsable systèmes de distribution
    Inscrit en
    Août 2007
    Messages
    441
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Pologne

    Informations professionnelles :
    Activité : Responsable systèmes de distribution

    Informations forums :
    Inscription : Août 2007
    Messages : 441
    Par défaut initialisation à la déclaration : marche pas :(
    bonjour,

    voici l'objet de mon malheur :
    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
        public class DailyData
        {
            public String h01 { get; set; }
            public String h02 { get; set; }
            public String h03 { get; set; }
            public String h04 { get; set; }
            public String h05 { get; set; }
            public String h06 { get; set; }
            public String h07 { get; set; }
            public String h08 { get; set; }
            public String h09 { get; set; }
            public String h10 { get; set; }
            public String h11 { get; set; }
            public String h12 { get; set; }
            public String h13 { get; set; }
            public String h14 { get; set; }
            public String h15 { get; set; }
            public String h16 { get; set; }
            public String h17 { get; set; }
            public String h18 { get; set; }
            public String h19 { get; set; }
            public String h20 { get; set; }
            public String h21 { get; set; }
            public String h22 { get; set; }
            public String h23 { get; set; }
            public String h24 { get; set; }
     
            public static DailyData newHoursHeader()
            {
                return new DailyData()
                    {
                        h01 = "1",
                        h02 = "2",
                        h03 = "3",
                        h04 = "4",
                        h05 = "5",
                        h06 = "6",
                        h07 = "7",
                        h08 = "8",
                        h09 = "9",
                        h10 = "10",
                        h11 = "11",
                        h12 = "12",
                        h13 = "13",
                        h14 = "14",
                        h15 = "15",
                        h16 = "16",
                        h17 = "17",
                        h18 = "18",
                        h19 = "19",
                        h20 = "20",
                        h21 = "21",
                        h22 = "22",
                        h23 = "23",
                        h24 = "24"
                    };
            }
     
            public DailyData() { }
     
            public DailyData(List<String> list)
            {
                if (list != null && list.Count == 25)
                #region bug1
                //new DailyData()
                //    {
                //        h01 = list[1],
                //        h02 = list[2],
                //        h03 = list[3],
                //        h04 = list[4],
                //        h05 = list[5],
                //        h06 = list[6],
                //        h07 = list[7],
                //        h08 = list[8],
                //        h09 = list[9],
                //        h10 = list[10],
                //        h11 = list[11],
                //        h12 = list[12],
                //        h13 = list[13],
                //        h14 = list[14],
                //        h15 = list[15],
                //        h16 = list[16],
                //        h17 = list[17],
                //        h18 = list[18],
                //        h19 = list[19],
                //        h20 = list[20],
                //        h21 = list[21],
                //        h22 = list[22],
                //        h23 = list[23],
                //        h24 = list[24]
                //    };
                #endregion
            }
        }
    me renvoie bien un objet correctement initialisé, mais me renvoie un objet avec toutes ses propriétés sont à "null"... (sachant que "list" est "bien comme il faut")
    je n'arrive pas à voir où est mon erreur

    merci de m'aider.

  2. #2
    Membre chevronné
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2008
    Messages
    337
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2008
    Messages : 337
    Par défaut
    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
     
        public class DailyData
        {
            public String h01 { get; set; }
            public String h02 { get; set; }
            public String h03 { get; set; }
            public String h04 { get; set; }
            public String h05 { get; set; }
            public String h06 { get; set; }
            public String h07 { get; set; }
            public String h08 { get; set; }
            public String h09 { get; set; }
            public String h10 { get; set; }
            public String h11 { get; set; }
            public String h12 { get; set; }
            public String h13 { get; set; }
            public String h14 { get; set; }
            public String h15 { get; set; }
            public String h16 { get; set; }
            public String h17 { get; set; }
            public String h18 { get; set; }
            public String h19 { get; set; }
            public String h20 { get; set; }
            public String h21 { get; set; }
            public String h22 { get; set; }
            public String h23 { get; set; }
            public String h24 { get; set; }
     
            public static DailyData newHoursHeader()
            {
                return new DailyData()
                    {
                        h01 = "1",
                        h02 = "2",
                        h03 = "3",
                        h04 = "4",
                        h05 = "5",
                        h06 = "6",
                        h07 = "7",
                        h08 = "8",
                        h09 = "9",
                        h10 = "10",
                        h11 = "11",
                        h12 = "12",
                        h13 = "13",
                        h14 = "14",
                        h15 = "15",
                        h16 = "16",
                        h17 = "17",
                        h18 = "18",
                        h19 = "19",
                        h20 = "20",
                        h21 = "21",
                        h22 = "22",
                        h23 = "23",
                        h24 = "24"
                    };
            }
     
            public DailyData() { }
     
            public DailyData(List<String> list)
            {
                if (list != null && list.Count == 25)
                {
                       this.h01 = list[1];
                       this.h02 = list[2];
                       this.h03 = list[3];
                       this.h04 = list[4];
                       this.h05 = list[5];
                       this.h06 = list[6];
                       this.h07 = list[7];
                       this.h08 = list[8];
                       this.h09 = list[9];
                       this.h10 = list[10];
                       this.h11 = list[11];
                       this.h12 = list[12];
                       this.h13 = list[13];
                       this.h14 = list[14];
                       this.h15 = list[15];
                       this.h16 = list[16];
                       this.h17 = list[17];
                       this.h18 = list[18];
                       this.h19 = list[19];
                       this.h20 = list[20];
                       this.h21 = list[21];
                       this.h22 = list[22];
                       this.h23 = list[23];
                       this.h24 = list[24];
                }
            }
        }
    essai comme ceci

  3. #3
    Membre très actif Avatar de Ragmaxone
    Homme Profil pro
    Responsable systèmes de distribution
    Inscrit en
    Août 2007
    Messages
    441
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Pologne

    Informations professionnelles :
    Activité : Responsable systèmes de distribution

    Informations forums :
    Inscription : Août 2007
    Messages : 441
    Par défaut
    il me dit :
    Cannot initialize type 'InterfejsPobieranieDanAKU.DailyData' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

  4. #4
    Membre chevronné Avatar de MetalGeek
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    412
    Détails du profil
    Informations personnelles :
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Octobre 2008
    Messages : 412
    Par défaut
    Salut, si j'ai bien compris, dans le constructeur qui prend une liste, tu passes une list avec tous les "h" ? Si c'est bien ça, en fait ton premier code devrait marcher, seulement quand tu fais

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    (list != null && list.Count == 25)
    ça renvoie false et il ne passe pas dans le statement suivant : ta liste contient 24 entrées, et pas 25.

    Ensuite quand tu l'initialises, tu commences à l'index 1, alors qu'en .NET les indexeurs sont toujours de base 0 :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    #region bug1
                //new DailyData()
                //    {
                //        h01 = list[0],
                //        h02 = list[1],
    au lieu de

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    #region bug1
                //new DailyData()
                //    {
                //        h01 = list[1],
                //        h02 = list[2],
    A moins bien sûr que cela soit voulu, et que la liste que tu passes contienne une entrée supplémentaire à l'index 0.

    Bref, sinon, pour l'erreur, à mon avis tu as dû mal recopier. Essaie juste d'utiliser la syntaxe normale :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    var toto = new DailyData();
    toto.h1 = "1";
    toto.h2 = "2";
    ...

  5. #5
    Membre très actif Avatar de Ragmaxone
    Homme Profil pro
    Responsable systèmes de distribution
    Inscrit en
    Août 2007
    Messages
    441
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Pologne

    Informations professionnelles :
    Activité : Responsable systèmes de distribution

    Informations forums :
    Inscription : Août 2007
    Messages : 441
    Par défaut
    Citation Envoyé par MetalGeek Voir le message
    ...
    • la liste a bien 25 entrées et je commence bien à 0, c'est juste que je n'ai pas encore "implémenté" la première valeur,
    • la "syntaxe normale" c'est la roue de secours que j'ai trouvé, mais C# gère bien l'initialisation telle que j'essaie de le faire puisque ce la marche pour la méthode "newHoursHeader"

    j'ai eu le fin mot de l'histoire :
    ce genre de déclaration "tout-en-une" ne peut pas se faire dans un constructeur...
    affaire résolue

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

Discussions similaires

  1. [XL-2003] la déclaration des variables désactivée ne marche pas
    Par zanoubya dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 30/08/2010, 15h37
  2. 'SHOW TABLES' marche pas sous postgresql !?
    Par fet dans le forum PostgreSQL
    Réponses: 4
    Dernier message: 13/05/2004, 09h28
  3. Réponses: 9
    Dernier message: 07/05/2003, 12h57
  4. [GifDecoder] marche pas dans applet avec IE
    Par formentor dans le forum Applets
    Réponses: 2
    Dernier message: 06/05/2003, 10h43
  5. Sysdate qui marche pas ??
    Par StouffR dans le forum Langage SQL
    Réponses: 4
    Dernier message: 28/08/2002, 13h23

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