Calcule des notes

Résolu
lemacho - Modifié par BunoCS le 22/04/2016 à 15:37
vb95 Messages postés 3418 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 31 mai 2023 - 22 avril 2016 à 17:11
Bonjour,

J'aimerais savoir comment je peux remplacer les code suivante par une fonction, pour faire le code moins long.

merci

Private Sub Calcule(e As GridViewRowEventArgs)
        Const BAREME_FINAL As Single = 0.4
        Dim ntTp1 As String = e.Row.Cells(3).Text
        Dim ntTp2 As String = e.Row.Cells(4).Text
        Dim ntTp3 As String = e.Row.Cells(5).Text
        Dim ntIntra As String = e.Row.Cells(6).Text
        Dim ntFinal As String = e.Row.Cells(7).Text
        Dim ntTotalTp, ntTotal As Integer
        Dim ntTotalFinal, moyPond As Decimal


        ntTotalFinal = CInt(ntFinal) * BAREME_FINAL
        'Traitement du moyenne pondérée
        moyPond = ntTotalFinal
        'Traitement de la note finale sans les travaux practiques si le seuil n'est pas atteint 
        e.Row.Cells(8).Text = ntTotal
        If (ntTotal < 50) Then
            e.Row.Cells(9).Text = "Échec."
        ElseIf (moyPond < 24) And (ntTotalTp < 40) Then
            ntTotal = moyPond
            e.Row.Cells(8).Text = ""
            e.Row.Cells(9).Text = "Échec: seuil non respecté."
        ElseIf (moyPond >= 24) And (ntTotalTp = 40) Then
            ntTotal = ntTotal
            e.Row.Cells(8).Text = ntTotal
            e.Row.Cells(9).Text = ""
        End If
    End Sub

    Private Sub CalculeNoteR(e As GridViewRowEventArgs)
        Const BAREME_INTRA As Single = 0.2
        Const BAREME_FINAL As Single = 0.4
        Const NOTE_R As String = "R"
        Dim ntTp1 As String = e.Row.Cells(3).Text
        Dim ntTp2 As String = e.Row.Cells(4).Text
        Dim ntTp3 As String = e.Row.Cells(5).Text
        Dim ntIntra As String = e.Row.Cells(6).Text
        Dim ntFinal As String = e.Row.Cells(7).Text
        Dim ntTotalTp As Integer
        Dim ntTotalIntra As Decimal

        If ntTp1 = NOTE_R And ntTp2 <> NOTE_R And ntTp3 <> NOTE_R And ntIntra <> NOTE_R Then
            ntTp1 = ntTp2
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntIntra) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 <> NOTE_R And ntTp2 = NOTE_R And ntTp3 <> NOTE_R And ntIntra <> NOTE_R Then
            ntTp2 = ntTp1
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntIntra) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 <> NOTE_R And ntTp2 <> NOTE_R And ntTp3 = NOTE_R And ntIntra <> NOTE_R Then
            ntTp3 = (CInt(ntTp1) + CInt(ntTp2)) / 20
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntIntra) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 = NOTE_R And ntTp2 = NOTE_R And ntTp3 <> NOTE_R And ntIntra <> NOTE_R Then
            ntTp1 = (CInt(ntTp3) / 20) * 10
            ntTp2 = CInt(ntTp1)
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntIntra) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 = NOTE_R And ntTp2 <> NOTE_R And ntTp3 = NOTE_R And ntIntra <> NOTE_R Then
            ntTp1 = CInt(ntTp2)
            ntTp3 = (CInt(ntTp1) + CInt(ntTp2) / 20) * 20
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntIntra) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 <> NOTE_R And ntTp2 = NOTE_R And ntTp3 = NOTE_R And ntIntra <> NOTE_R Then
            ntTp2 = CInt(ntTp1)
            ntTp3 = CInt(ntTp1) + CInt(ntTp2)
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntIntra) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 = NOTE_R And ntTp2 = NOTE_R And ntTp3 = NOTE_R And ntIntra <> NOTE_R Then
            ntTotalTp = (CInt(ntIntra) * BAREME_INTRA) + (CInt(ntFinal) * BAREME_FINAL) / 60 * 40
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntIntra) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 = NOTE_R And ntTp2 <> NOTE_R And ntTp3 <> NOTE_R And ntIntra = NOTE_R Then
            ntTp1 = ntTp2
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntFinal) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 <> NOTE_R And ntTp2 = NOTE_R And ntTp3 <> NOTE_R And ntIntra = NOTE_R Then
            ntTp2 = ntTp1
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntFinal) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 <> NOTE_R And ntTp2 <> NOTE_R And ntTp3 = NOTE_R And ntIntra = NOTE_R Then
            ntTp3 = (CInt(ntTp1) + CInt(ntTp2)) / 20
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntFinal) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 = NOTE_R And ntTp2 = NOTE_R And ntTp3 <> NOTE_R And ntIntra = NOTE_R Then
            ntTp1 = (CInt(ntTp3) / 20) * 10
            ntTp2 = CInt(ntTp1)
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntFinal) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 = NOTE_R And ntTp2 <> NOTE_R And ntTp3 = NOTE_R And ntIntra = NOTE_R Then
            ntTp1 = CInt(ntTp2)
            ntTp3 = CInt(ntTp1) + CInt(ntTp2)
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntFinal) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 <> NOTE_R And ntTp2 = NOTE_R And ntTp3 = NOTE_R And ntIntra = NOTE_R Then
            ntTp2 = CInt(ntTp1)
            ntTp3 = CInt(ntTp1) + CInt(ntTp2)
            ntTotalTp = (CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3))
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntFinal) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        ElseIf ntTp1 <> NOTE_R And ntTp2 <> NOTE_R And ntTp3 <> NOTE_R And ntIntra = NOTE_R Then
            ntTotalTp = CInt(ntTp1) + CInt(ntTp2) + CInt(ntTp3)
            'Traitement de la note examen intra
            ntTotalIntra = CInt(ntFinal) * BAREME_INTRA
            'Traitement de la note examen Final
            Calcule(e)
        End If
    End Sub


EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ICI

Merci d'y penser dans tes prochains messages.

1 réponse

vb95 Messages postés 3418 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 31 mai 2023 165
Modifié par vb95 le 22/04/2016 à 17:14
Bonjour
Quelques bonnes pratiques:

Option Strict On
Option Explicit On
et désactiver la référence à Microsoft.VisualBasic
Ensuite corrige les erreurs .

Ensuite pourquoi définir 2 fois les mêmes variables dans les 2 procédures
Ensuite dans chaque séquence ElseIF les 2 dernières lignes se répètent
 'Traitement de la note examen intra
ntTotalIntra = CInt(ntIntra) * BAREME_INTRA
'Traitement de la note examen Final
Calcule(e)

La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi. 
0