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 :

convertion static des champs


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Maroc

    Informations forums :
    Inscription : Juillet 2006
    Messages : 98
    Par défaut convertion static des champs
    bonjour

    j'ai un champs text nommé txtvar,l'utilisateur tape un nombre dans ce champ avec lequel je travaille , le probléme c'est que lorsque je fais une declaration de ce type:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    static int nbr_var=convert.toint16(txtvar.text);
    ca me donne l'erreur suivant:
    une référance d'objet est requise pour la propriété,la méthode ou le champ non statique'simplexessaie.frm.txtvar'
    et lorsque j'initialise la variable comme ca
    ca marche,ce que je veux pas
    je serai reconaissant.

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    91
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Avril 2008
    Messages : 91
    Par défaut
    Bonjour,

    Ton erreur provient que si tu met ton TextBox.Text Dans un champs privé, il n'est pas encore initialisé, tu ne peut pas l'utilisé dans une déclaration de champs.

    De plus le Type int equivaut au type Int32 et non Int16 mais ce n'est pas la source de l'erreur.

    tu peux faire cela , ce n'est comme un exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    private static int champs = Convert.ToInt32(DateTime.Now.Day);
    Tous simplement parce que la propriété Now est static, tu ne peux le faire quand il s'agit d'un objet que l'on doit instancié.

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Maroc

    Informations forums :
    Inscription : Juillet 2006
    Messages : 98
    Par défaut
    merci frere de a reponse
    j'ai essayé ca :
    private static int nbr_var=convert.ToInt32(txtvar.text);
    mais reste le meme probleme
    merci

  4. #4
    Membre émérite
    Inscrit en
    Octobre 2006
    Messages
    587
    Détails du profil
    Informations personnelles :
    Âge : 38

    Informations forums :
    Inscription : Octobre 2006
    Messages : 587
    Par défaut
    Normal l'erreur ne vient pas de la conversion.
    Tu peux utiliser un constructeur statique mais il ne sera appelé que lorsque tu voudra accéder à l'état de ta variable statique

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Maroc

    Informations forums :
    Inscription : Juillet 2006
    Messages : 98
    Par défaut
    merci beacoup
    je suis debutant,et j'ai pas bien saisie si possible un exemple pour eclairer ton idée
    je vous serai reconaissant

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Maroc

    Informations forums :
    Inscription : Juillet 2006
    Messages : 98
    Par défaut
    voila si vous voulez un peu de code source :
    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
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
     
     
    namespace simplexessaie
    {
        public partial class frm : Form
        { 
     
            public   frm()
            {
     
                InitializeComponent();
     
             }
                      static  int nbr_con = Convert.ToInt32(txtcon.Value);
                        static int nbr_var =Convert.ToInt32(txtvar.Text);
     
                // decimal[] Simpl;
                decimal Resultat, i, j ;
                 //int m = (nbr_var + 2) * (nbr_con + 1);
                 decimal[] z = new decimal[1];
                 String mot = "";
                  decimal[] Simpl = new decimal[(nbr_var + 2) * (nbr_con + 1)];
                 decimal[] sol = new decimal[nbr_var];
                 decimal[] Donnees = new decimal[(nbr_var + 2) * (nbr_con + 1)];
     
            public void affiche_coefficient(object sender, EventArgs e)
            {
     
                int j = 0;
               int c = ( nbr_con * 30) + 30;
                int k = 0;
                int s = 0;
                int r = 170;
                // lblcoefobj
                Label lblcoefobj = new Label();
                lblcoefobj.Location = new System.Drawing.Point(7, 104);
                lblcoefobj.Size = new System.Drawing.Size(200, 13);
                lblcoefobj.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblcoefobj.Text = "Coefficient de la fonction objectif:";
                this.Controls.Add(lblcoefobj);
                // lblcoefcon
                Label lblcoefcon = new Label();
                lblcoefcon.Location = new System.Drawing.Point(7, 200);
                lblcoefcon.Size = new System.Drawing.Size(200, 13);
                lblcoefcon.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblcoefcon.Text = "Coefficient des contraintes :";
                this.Controls.Add(lblcoefcon);
                // lblcoefsemem
                Label lblcoefsemem = new Label();
                lblcoefsemem.Location = new System.Drawing.Point(7, 130 + c);
                lblcoefsemem.Size = new System.Drawing.Size(200, 13);
                lblcoefsemem.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblcoefsemem.Text = "Coefficient de second membre :";
                this.Controls.Add(lblcoefsemem);
                //
                for (int i = 0; i <nbr_var; i++)
                {
     
     
                    NumericUpDown CofonctiObjec = new NumericUpDown();
                    CofonctiObjec.Location = new Point(230 + j, 100);
                    CofonctiObjec.Size = new System.Drawing.Size(50, 20);
                    CofonctiObjec.DecimalPlaces=2;
                    CofonctiObjec.Name = "CofonctiObjec" + i.ToString();
                    this.Controls.Add(CofonctiObjec);
                    j += 60;
                    //listVariable[i] = Convert.ToInt16( (text1.Text as TextBox).Text);
     
     
                }
     
     
                do
                {
                    r += 60;
                    k = 0;
                    for (int i = 0; i <nbr_con; i++)
                    {
                        NumericUpDown CofContrainte = new NumericUpDown();
                        CofContrainte.Location = new Point(r, 200 + k);
                        CofContrainte.Size = new System.Drawing.Size(50, 20);
                        CofContrainte.DecimalPlaces = 2;
                        CofContrainte.Name = "CofContrainte" + i.ToString();
                        this.Controls.Add(CofContrainte);
                        k += 30;
                    }
     
                    s++;
                }
                while (s <nbr_var);
                bool ok = false;
                do{
                int m = 0;
                r += 60; 
                for (int i = 0; i <nbr_con; i++)
                {
     
                    ComboBox com = new ComboBox();
                    com.Location = new Point(r, 200+m);
                    com.Size = new System.Drawing.Size(50, 21);
                   com.Name = "com"+i.ToString();
                   com.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                   com.FormattingEnabled = true;
                   com.Items.AddRange(new object[] {
                "=>",
                "<=",
                "="});
              /* com.Maximum = new decimal(new int[] {
                1000
               
                });*/
                    com.TabIndex = 13;
                    this.Controls.Add(com);
     
     
                    NumericUpDown CofSecoMembre = new NumericUpDown();
                    CofSecoMembre.Location = new Point(r+60, 200 + m);
                    CofSecoMembre.Size = new System.Drawing.Size(50, 20);
                    CofSecoMembre.DecimalPlaces = 2;
                    CofSecoMembre.Name = "CofSecoMembre" + i.ToString();
                    this.Controls.Add(CofSecoMembre);
                 //  txtContrainte.Name = "txtContrainte" + i.ToString();
                    m += 30;
                }
     
     
                }
           while(ok);
                }   
     
     
     
     
     
     
     
            public void frm_FormClosing(object sender, FormClosingEventArgs e)
            {
                DialogResult key = MessageBox.Show("voulez-vous vraiment quitter l'application", "confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (DialogResult.No == key)
                    e.Cancel = true;
            }
     
     
            public void résoudre_Click_1(object sender, EventArgs e)
            {
     
     
     
               i = 0;
                    while (i < nbr_var)
                    {
                        Simpl[Convert.ToInt16( (nbr_con * nbr_var )+ 2 + i)] =(this.Controls["CofonctiObjec" + i.ToString()] as NumericUpDown).Value;
                        i++;
                    }
                i = 0;
                    while (i <nbr_con)
                    {
                        j = 0;
     
                        while (j < nbr_var)
                        {
                            Simpl[Convert.ToInt16( i * (nbr_var + 2) + j)] =(this.Controls["CofContrainte" + i.ToString()] as NumericUpDown).Value;
     
                            j++;
                        }
     
                    Simpl[Convert.ToInt16( i * (nbr_var + 2) + j + 1)] = -1;
                    try { Simpl[Convert.ToInt16(i * (nbr_var + 2) + j)] = (this.Controls["CofSecoMembre" + i.ToString()] as NumericUpDown).Value; }
                        catch(Exception ){}
     
                    i++;
                }
                 Simplex S = new Simplex(); 
     
                Resultat = S.Simplexe(Simpl, nbr_var, nbr_con, sol, z, mot);
                mot += "\n\t*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-RESULTAT:*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
                if (Resultat != 0)
                {
     
                    mot += "\nLe probleme admet une solution optimale :\t\n -Z =" + z[0] + "\n\n\tavec\t";
                    for (i = 0; i < nbr_var; i++)
                        mot += "\n\n X" + (i + 1) + "=\t" + (sol[Convert.ToInt16(i)]);
                    mot += "\n";
                }
                else
                    mot += "\n\"Le  n'admet pas de solution!!!\"";
                MessageBox.Show(mot, "DONNEES");
     
     
     
            }
     
            public void linkLabel1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("essaie");
            }
     
     
     
        }
    }

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

Discussions similaires

  1. [LG]Vision des champs d'un objet
    Par YéTeeh dans le forum Langage
    Réponses: 3
    Dernier message: 06/11/2003, 21h20
  2. [SQL] Compter des champs indépendement l'un de l'autre
    Par rippey dans le forum Requêtes et SQL.
    Réponses: 11
    Dernier message: 29/10/2003, 15h35
  3. [langage] Comment rajouter des champs dans une liste
    Par toto_titi dans le forum Langage
    Réponses: 4
    Dernier message: 28/08/2003, 14h09
  4. Ordre des champs dans une table
    Par patapetz dans le forum Outils
    Réponses: 5
    Dernier message: 30/07/2003, 06h53
  5. Taille des champs proportionnelle...
    Par Depteam1 dans le forum Composants VCL
    Réponses: 2
    Dernier message: 09/08/2002, 11h48

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