Important les byref ?

befadudesert Messages postés 17 Date d'inscription jeudi 22 février 2007 Statut Membre Dernière intervention 28 janvier 2008 - 24 sept. 2007 à 16:43
befadudesert Messages postés 17 Date d'inscription jeudi 22 février 2007 Statut Membre Dernière intervention 28 janvier 2008 - 25 sept. 2007 à 10:26
Bonjour,
j'essaye de faire un proramme avec une variable globale.
J'ai essayé de la déclarer avant toutes procédures ou fonctions mais "type mismatch"

J'ai ensuite essayé de la passer en paramètre de la fonction par référence mais ça ne marche toujours pas.

Pourtant lorsque je fais des petits tests ca fontionne.

Voici deux exemples qui fonctionnent pourtant le second ne devrait pas fonctionner.
Premier exemple :
chaine1 = "première chaine"
chaine2 = "deuxième chaine"

Sub affichage(tab)
    tab = tab & " !!!"
    Wscript.echo tab
End Sub

affichage chaine1
affichage chaine2

Deuxième exemple :
chaine1 = "première chaine"
chaine2 = "deuxième chaine"

Sub affichage(byref tab)
    tab = tab & " !!!"
    Wscript.echo tab
End Sub

affichage chaine1
affichage chaine2

Quelqu'un aurait une explication ??

7 réponses

cs_etniqs Messages postés 201 Date d'inscription mardi 7 octobre 2003 Statut Membre Dernière intervention 10 mai 2016
24 sept. 2007 à 17:09
ben, si il devrait marcher ...
et ton byref ne sert à rien, si tu changes pas la valeur ...
0
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
24 sept. 2007 à 17:13
Les 2 cas devraient marcher, et ça n'a rien à voir avec ByRef.

Dans le cas ou la variable est passée comme ByVal, toute modification interne à la fonction sur la variable ne sortira pas de la fonction puisque effectuée sur une copie de la variable originale. Donc par exemple Chaine1 après appel = Chaine 1 avant appel.

Dans le cas ou la variable est passée comme ByRef, toute modification interne à la fonction sur la variable est immédiatement reportée sur la variable originale puisque c'est bien celle-ci (plus exactement son adresse mémoire) qui est réellement passé à la fonction. Donc dans ce cas là, Chaine 1 après appel est différent de Chaine 1 avant appel.

Par contre dans ton code tu ne type pas tes variables, et ton problème vient surrement de là. En VB6 toute variable non typée est automatiquement typée comme Variant, type entre-autre gros consomateur de mémoire.

Prend l'habitude de typer correctement toutes tes variables

---- Sevyc64  (alias Casy) ---- <hr size ="2" width="100%" /># LE PARTAGE EST NOTRE FORCE #    http://aide-office-vba.monforum.com/index.php
0
Kristof_Koder Messages postés 918 Date d'inscription vendredi 3 août 2007 Statut Membre Dernière intervention 27 octobre 2008 10
24 sept. 2007 à 22:38
D'accord avec les explications de Casy, sauf pour le typage ! Regardes bien Casy, il semble que ce soit du VBScript !
BefaDuDesert < Tes deux exemples fonctionnent en effet correctement. Enfin, tout dépend de ce que tu penses qu'il devrait faire ?!
Pour ton problème à la base, j'avoue ne pas bien voir quel est ton problème ? "Type Mismatch" !! Etrange ! Donne ous donc copie de ton script, on verra ce qui cloche ?
0
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
24 sept. 2007 à 22:44
Vu, pas vu que c'était du script. Effectivement pas de typage alors.

Mais alors, Type Mismatch étant justement une erreur de typage, d'ou vient le problème ???

---- Sevyc64  (alias Casy) ----<hr size="2" width="100%" /># LE PARTAGE EST NOTRE FORCE #    http://aide-office-vba.monforum.com/index.php
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Kristof_Koder Messages postés 918 Date d'inscription vendredi 3 août 2007 Statut Membre Dernière intervention 27 octobre 2008 10
24 sept. 2007 à 22:49
"Type Mismatch", se produit parfois en VBScript, sur des histoire de tableau je crois.
0
befadudesert Messages postés 17 Date d'inscription jeudi 22 février 2007 Statut Membre Dernière intervention 28 janvier 2008
25 sept. 2007 à 10:23
'liste des variables globales
cheminFichierDroits = "C:\tst.txt"
ligne = nbLignesDansFichier (cheminFichierDroits)

Dim tab()
Redim tab(ligne,8)

Dim ligneCourante
ligneCourante = -1

Function nbLignesDansFichier(cheminFichier)
    'déclaration file system object
    Dim FSO
    'instanciation
    Set FSO = CreateObject("Scripting.FileSystemObject")
    'on instance le fichier texte
    Set Ftxt = FSO.OpenTextFile(cheminFichier)
    Dim cpt
    cpt = 0
    'on parcours chaque ligne du fichier texte
    Do While Not Ftxt.AtEndOfStream
        MaVariable = Ftxt.Readline
        if MaVariable <> "" Then
            cpt=cpt+1
            traitementLigne MaVariable,tab
        End If
    Loop
    nbLignesDansFichier = cpt
    Ftxt.Close
End Function

'_________________________LECTURE d'un fichier_____________________________
'déclaration file system object
Dim FSO
'instanciation
Set FSO = CreateObject("Scripting.FileSystemObject")
'on instancie le fichier texte
Set Ftxt = FSO.OpenTextFile(cheminFichierDroits)
'on parcours chaque ligne du fichier texte
Do While Not Ftxt.AtEndOfStream
      MaVariable = Ftxt.Readline
      if MaVariable <> "" Then
        traitementLigne MaVariable, tab
    End If
Loop
Ftxt.Close

'Entête tab
tab(0,0) = "chemin"
tab(0,1) = "utilisateur"
tab(0,2) = "heritage"
tab(0,3) = "lecture"
tab(0,4) = "ecriture"
tab(0,5) = "modification"
tab(0,6) = "execution"
tab(0,7) = "appropriation"

'__________________________Traitement de chaque ligne_________________________
Sub traitementLigne(ligne, byref tableau)
    'si ligne normale
    WScript.echo tableau(0,0)
    If left(ligne,1) <> " " Then
        'on passe à une nouvelle ligne
        ligneCourante = ligneCourante + 1
        chemin = motChoisi(ligne," ",1)
        cheminRelatif = "\" + NomMachine + right(chemin, len(chemin)-2)
        tab(ligneCourante,0) = cheminRelatif
        repertoireCourant = cheminRelatif
        'on crée une nouvelle ligne
        'on remplit la premiere ligne à savoir le chemin relatif
        reste = right(ligne,len(ligne)-len(cheminRelatif)-1)
        util = motChoisi(motChoisi(reste,"",2),":",1)
        'on remplit la 2è colonne
        tab(ligneCourante,1) = util
        domaineEtUtil=motChoisi(reste,":",1)
        droits = right(reste,len(reste)-len(domaineEtUtil)-1)
    
        'On s'occupe maintenant des droits
        If Instr(droits,"(CI)") <> 0 OR Instr(droits,"(IO)") <> 0 OR Instr(droits,"(OI)") <> 0 Then
            tab(ligneCourante,2) = "X"
        End If
        If Instr(droits,"F") <> 0 Then
            tableau(ligneCourante,3) = "X"
            tableau(ligneCourante,4) = "X"
            tableau(ligneCourante,5) = "X"
            tableau(ligneCourante,6) = "X"
            tableau(ligneCourante,7) = "X"
        Else If Instr(droits,"C") <> 0 Then
                tableau(ligneCourante,3) = "X"
                tableau(ligneCourante,4) = "X"
                tableau(ligneCourante,5) = "X"
                tableau(ligneCourante,6) = "X"
            Else If Instr(droits,"(accŠs sp‚cialÿ") <> 0 Then
                WScript.echo "ligne suivante"
                End If
            End If
        End If
    'pas ligne normale donc soit ligne utilisateur sup. soit ligne de droits
    Else
        'dans ce cas la nous sommes dans le cas d'une ligne d'un nouvel utilisateur
        If Instr(ligne,"") <> 0 OR Instr(ligne,"Tout le monde") <> 0 OR Instr(ligne,"CREATEUR PROPRIETAIRE") <> 0 Then
            WScript.echo "ligneCourante " & ligneCourante
            If ligneCourante <> 0 Then
                tableau(ligneCourante,0) = tableau(ligneCourante-1,0)
            End If
            ligne=sansEspaces(ligne)
            'on vérifie les utilisateurs spéciaux
            If Instr(ligne,"Tout le monde") <> 0 Then
                util = "Tout le monde"
            Else If Instr(ligne,"CREATEUR PROPRIETAIRE") <> 0 Then
                    util = "CREATEUR PROPRIETAIRE"
                Else util=motChoisi(motChoisi(ligne,"",2),":",1)
                End If
            End If
            tab(ligneCourante,1) = util
            domaineEtUtil=motChoisi(reste,":",1)
            droits = motChoisi(ligne,":",2)
            'heritage
            If Instr(droits,"(CI)") <> 0 OR Instr(droits,"(IO)") <> 0 OR Instr(droits,"(OI)") <> 0 Then
                tableau(ligneCourante,2) = "X"
            End If
            
            If Instr(ligne,"F") <> 0 Then
                tableau(ligneCourante,3) = "X"
                tableau(ligneCourante,4) = "X"
                tableau(ligneCourante,5) = "X"
                tableau(ligneCourante,6) = "X"
                tableau(ligneCourante,7) = "X"
            Else
                If Instr(ligne,"C") <> 0 Then
                    tableau(ligneCourante,3) = "X"
                    tableau(ligneCourante,4) = "X"
                    tableau(ligneCourante,5) = "X"
                    tableau(ligneCourante,6) = "X"
                Else
                    If Instr(ligne,"(accŠs sp‚cialÿ") <> 0 Then
                        'il faut analyser les lignes suivantes afin de connaitre les droits de cet utilisateur
                        WScript.echo "cas special"
                    End If
                End If
            End If
        'ligne de droits
        Else
            'ici nous sommes dans les cas d'une ligne de droit pure
            ligneM=sansEspaces(ligne)
            If ligneM = "" Then
                If ligneM = "READ_CONTROL" Then
                    tableau(ligneCourante,3) = "X"
                Else
                    If ligneM = "FILE_WRITE_DATA" Then
                        tableau(ligneCourante,4) = "X"
                    Else
                        If ligneM = "DELETE" Then
                            tableau(ligneCourante,5) = "X"
                        Else
                            If ligneM = "FILE_EXECUTE" Then
                                tableau(ligneCourante,6) = "X"
                            Else
                                If ligneM = "WRITE_OWNER" Then
                                    tableau(ligneCourante,7) = "X"
                                End If
                            End If
                        End If
                    End If    
                End If
            End If
        End If
    End If
End Sub

Function motChoisi(chaine,separateur,numMot)
    chaineU=chaine
    cpt = 1
    nb = -1
    Do While nb <> 0
        'nb renvoie le premier caractère " " trouvé dans la chaine
        nb=Instr(chaineU,separateur)
        'si aucun espace a été trouvé
        If nb <> 0 Then
            'un espace est trouvé comme mot
            If nb = 1 Then
                cpt = cpt-1
                chaineU = right(chaineU, len(chaineU)-1)
            Else
                mot_courant = left(chaineU, nb-1)
                If numMot=cpt Then
                    motChoisi=mot_courant
                End If
            End If
            chaineU=right(chaineU, len(chaineU) - nb+1)
            cpt = cpt+1
        Else
            If numMot=cpt Then
                motChoisi=chaineU
            End If
        End If
    Loop
End Function

Function sansEspaces(mot)
    Do While left(mot,1) = " "
            mot=right(mot,len(mot)-1)
    Loop
    sansEspaces=mot
End Function

Sub affichage (byref tab)
    ligneTemp = 0
    colonneTemp = 0
    Do While ligneTemp < ligne
        Do While colonneTemp < 8
            WScript.echo tab(ligneTemp, colonneTemp)
            colonneTemp = colonneTemp + 1
        Loop
        ligneTemp = ligneTemp + 1
        colonneTemp = 0
    Loop
End Sub

Avec le C:\tst.txt,
E:\ BUILTIN\Administrateurs:(OI)(CI)F
    AUTORITE NT\SYSTEM:(OI)(CI)C

    CREATEUR PROPRIETAIRE:(OI)(CI)(IO)F
0
befadudesert Messages postés 17 Date d'inscription jeudi 22 février 2007 Statut Membre Dernière intervention 28 janvier 2008
25 sept. 2007 à 10:26
En effet je m'étais planté sur mes exemples les voici corrigés à tête reposé. En effet se prendre pendant une journée ça n'aide pas à avoir les idées clairs ^^.

Premier exemple
chaine1 = "premiere chaine"
chaine2 = "deuxième chaine"

Function modification(tab)
    tab = tab & " !!!"
    affichage = tab
End Function

WScript.echo affichage(chaine1)
WScript.echo affichage(chaine2)

chaine1 = "premiere chaine"
chaine2 = "deuxième chaine"

Deuxième exemple :
Function modification(byref tab)
    tab = tab & " !!!"
    affichage = tab
End Function

WScript.echo affichage(chaine1)
WScript.echo affichage(chaine2)
0
Rejoignez-nous