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

Dotnet Discussion :

Création et utilisation d'une fonction


Sujet :

Dotnet

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Août 2007
    Messages : 8
    Points : 5
    Points
    5
    Par défaut Création et utilisation d'une fonction
    Bonjour à tous

    Je débute avec Visual Studio 2005, et je cherche à créer une fonction pour éviter de répéter un même code dans plusieurs pages différentes. Pour info, mes pages fonctionne parfaitement en répétant le code dans chaque page.

    Voici une partie de "MaPage.aspx.cs" :

    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
     
     
    protected void gvFamily_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
     
     
                int Total_Line1 = 0;
                int Total_Grf2 = 0;
                int Total_Line2 = 0;
                int Total_Grf3 = 0;
                int Total_Line3 = 0;
                int Total_Grf4 = 0;
                int Total_Line4 = 0;
                int Total_Grf5 = 0;
                int Total_Line5 = 0;
                int Total_Grf_All = 0;
     
                Total_Line1 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_BBB").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_BBB").ToString()));
                Total_Grf2 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_BBB").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_BBB").ToString()));
                Total_Line2 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_CCC").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_CCC").ToString()));
                Total_Grf3 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_CCC").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_CCC").ToString()));
                Total_Line3 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_CCC_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_CCC_DDD").ToString()));
                Total_Grf4 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_CCC_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_DDD").ToString()));
                Total_Line4 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_DDD").ToString()));
                Total_Grf5 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_DDD").ToString()));
                Total_Line5 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_DDD").ToString()));
                Total_Grf_All += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_DDD").ToString()));
     
                }
                else if (e.Row.RowType == DataControlRowType.Footer)
                {
                    e.Row.Cells[0].Text = "Total :";
                    // for the Footer, display the running totals
                    e.Row.Cells[1].Text = Total_Line1.ToString("d");
                    e.Row.Cells[2].Text = Total_Grf2.ToString("d");
                    e.Row.Cells[3].Text = Total_Line2.ToString("d");
                    e.Row.Cells[4].Text = Total_Grf3.ToString("d");
                    e.Row.Cells[5].Text = Total_Line3.ToString("d");
                    e.Row.Cells[6].Text = Total_Grf4.ToString("d");
                    e.Row.Cells[7].Text = Total_Line4.ToString("d");
                    e.Row.Cells[8].Text = Total_Grf5.ToString("d");
                    e.Row.Cells[9].Text = Total_Line5.ToString("d");
                    e.Row.Cells[10].Text = Total_Grf_All.ToString("d");
     
                    e.Row.HorizontalAlign = HorizontalAlign.Center;
                    e.Row.Font.Bold = true;
                }
            }
    Et voici la partie de qui doit être utilisée dans plusieurs autre page :

    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
                int Total_Line1 = 0;
                int Total_Grf2 = 0;
                int Total_Line2 = 0;
                int Total_Grf3 = 0;
                int Total_Line3 = 0;
                int Total_Grf4 = 0;
                int Total_Line4 = 0;
                int Total_Grf5 = 0;
                int Total_Line5 = 0;
                int Total_Grf_All = 0;
     
                Total_Line1 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_BBB").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_BBB").ToString()));
                Total_Grf2 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_BBB").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_BBB").ToString()));
                Total_Line2 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_CCC").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_CCC").ToString()));
                Total_Grf3 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_CCC").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_CCC").ToString()));
                Total_Line3 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_CCC_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_CCC_DDD").ToString()));
                Total_Grf4 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_CCC_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_DDD").ToString()));
                Total_Line4 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_DDD").ToString()));
                Total_Grf5 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_DDD").ToString()));
                Total_Line5 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_DDD").ToString()));
                Total_Grf_All += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_DDD").ToString()));

    Dans le dossier "Common", j'ai donc créer une fonction du type :

    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
            public void SumFooter()
            {
    
                int Total_Line1 = 0;
                int Total_Grf2 = 0;
                int Total_Line2 = 0;
                int Total_Grf3 = 0;
                int Total_Line3 = 0;
                int Total_Grf4 = 0;
                int Total_Line4 = 0;
                int Total_Grf5 = 0;
                int Total_Line5 = 0;
                int Total_Grf_All = 0;
    
                // add the total_line_1 and total_line_2 to the running total variable
                Total_Line1 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_BBB").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_BBB").ToString()));
                Total_Grf2 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_BBB").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_BBB").ToString()));
                Total_Line2 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_CCC").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_CCC").ToString()));
                Total_Grf3 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_CCC").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_CCC").ToString()));
                Total_Line3 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_CCC_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_CCC_DDD").ToString()));
                Total_Grf4 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_CCC_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_DDD").ToString()));
                Total_Line4 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_BBB_DDD").ToString()));
                Total_Grf5 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_BBB_DDD").ToString()));
                Total_Line5 += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_THEO_AAA_DDD").ToString()));
                Total_Grf_All += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_DDD").ToString() == "" ? "0" : DataBinder.Eval(e.Row.DataItem, "NB_REAL_AAA_DDD").ToString()));
            }

    Et je fais donc appel à cette fonction dans "MaPage.aspx.cs" de la facon suivante :

    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
            protected void gvFamily_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    // add the total_line_1 and total_line_2 to the running total variable
                    SumFooter();
                }
                else if (e.Row.RowType == DataControlRowType.Footer)
                {
                    e.Row.Cells[0].Text = "Total :";
                    // for the Footer, display the running totals
                    e.Row.Cells[1].Text = Total_Line1.ToString("d");
                    e.Row.Cells[2].Text = Total_Grf2.ToString("d");
                    e.Row.Cells[3].Text = Total_Line2.ToString("d");
                    e.Row.Cells[4].Text = Total_Grf3.ToString("d");
                    e.Row.Cells[5].Text = Total_Line3.ToString("d");
                    e.Row.Cells[6].Text = Total_Grf4.ToString("d");
                    e.Row.Cells[7].Text = Total_Line4.ToString("d");
                    e.Row.Cells[8].Text = Total_Grf5.ToString("d");
                    e.Row.Cells[9].Text = Total_Line5.ToString("d");
                    e.Row.Cells[10].Text = Total_Grf_All.ToString("d");
    
                    e.Row.HorizontalAlign = HorizontalAlign.Center;
                    e.Row.Font.Bold = true;
                }
            }
    Voici malheureusement les erreurs :



    Les parties soulignées en bleu :






    Merci de votre aide précieuse.
    Images attachées Images attachées    

  2. #2
    Membre régulier

    Profil pro
    Développeur Java
    Inscrit en
    Mars 2003
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2003
    Messages : 6
    Points : 72
    Points
    72
    Par défaut
    Les erreurs sont assez explicites:

    _ Il te faut passer GridViewRowEventArgs e en paramètre de ta fonction. En effet ta fonction SumFooter ne sait pas à quoi correspond ce e.

    _ Ta fonction SumFooter fait des choses sur les entiers Total_XXX mais ne renvoie rien donc tu peux pas les utiliser dans ta fonction void gvFamily_RowDataBound(object sender, GridViewRowEventArgs e)

    Je te conseillerais néanmoins de reprendre certaines bases niveau programmation.

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Août 2007
    Messages : 8
    Points : 5
    Points
    5
    Par défaut
    Désolé mais je n'ai pas une formation en informatique/programmation.

    J'essaye donc d'apprendre "sur la tas".

    Je vais donc essayer de suivre tes conseils dès demain et je vous tiendrai informé.

    Merci en tout cas pour la réponse

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Août 2007
    Messages : 8
    Points : 5
    Points
    5
    Par défaut
    Merci à vous, le problème est résolu.

    Voici comment j'ai déclaré la fonction :

    public void SumFooter(GridViewRowEventArgs e)

    L'appel à la fonction dans la page : SumFooter(e);

    A très bientôt

  5. #5
    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
    Tant que tu y es à créer des méthodes pour éviter la redondance de code, tu pourrais aussi en faire une qui s'occupe de convertir en Int32 ton DataItem :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    private void ToInt(object dataItem, string nom)
    {
      return Convert.ToInt32((DataBinder.Eval(dataItem, nom).ToString() == "" ? "0" : DataBinder.Eval(dataItem, nom).ToString()));
    }
     
    protected void gvFamily_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                int Total_Line1 = ToInt(e.Row.DataItem, "NB_THEO_AAA_BBB");
                int Total_Grf2 = ToInt(e.Row.DataItem, "NB_REAL_AAA_BBB");
                // ...
                }
             }
    Ca rend le tout nettement plus lisible, et donc maintenable
    ಠ_ಠ

  6. #6
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Août 2007
    Messages : 8
    Points : 5
    Points
    5
    Par défaut
    Super merci de ton aide

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

Discussions similaires

  1. Réponses: 8
    Dernier message: 29/06/2006, 15h37
  2. Réponses: 3
    Dernier message: 29/04/2006, 13h02
  3. Réponses: 3
    Dernier message: 14/04/2006, 19h36
  4. Utilisation d'une fonction Transact-SQL ds une requête SQL
    Par Fl0ppeur dans le forum Langage SQL
    Réponses: 1
    Dernier message: 21/02/2006, 13h42
  5. Utilisation d'une fonction dans une procedure
    Par MaxiMax dans le forum MS SQL Server
    Réponses: 3
    Dernier message: 09/08/2005, 15h51

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