For each dans un For each - Possible ou pas ?

Résolu
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 - 28 oct. 2010 à 12:22
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 - 28 oct. 2010 à 13:40
Merci d'avance, je sèche !

Erreur 1
La variable 'rtNode' masque une variable dans un bloc englobant.

        Friend Function GetRootValue(ByVal strRoot As String) As String
            Dim rtRootNode, rtNode, rtNode2 As Xml.XmlNode
            For Each rtRootNode In xmlDOC.DocumentElement
                If rtRootNode.LocalName = strRoot Then
                    Dim arRoot As String() = strRoot.Split("/")
                    Dim i As Integer
                    For i = 1 To arRoot.Length - 1
                        For Each rtNode As Xml.XmlNode In rtRootNode.ChildNodes
                            If rtNode.LocalName = arRoot(i) Then
                                Return rtNode
                                Exit Function
                            End If
                        Next
                    Next
                    Return rtRootNode.ChildNodes.Item(0).Value
                End If
            Next
            Return Nothing
        End Function

3 réponses

Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
28 oct. 2010 à 13:14
Là c top ^^

        Friend Function GetValue(ByVal strRoot As String, ByVal ValueType As Xml.XmlNodeType) As String
            Dim rtRootNode As Xml.XmlNode
            Dim rtSubRoot As Xml.XmlNode

            rtSubRoot = xmlDOC.SelectSingleNode(strRoot)
            For Each rtRootNode In rtSubRoot.ChildNodes
                If rtRootNode.NodeType = ValueType Then
                    Return rtRootNode.Value
                End If
            Next
            Return Nothing
        End Function
3
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
28 oct. 2010 à 13:12
Pourquoi ce casser la tête MOAUahaha ^^ looky:

        Friend Function GetValue(ByVal strRoot As String, ByVal ValueType As Xml.XmlNodeType) As String
            Dim rtRootNode As Xml.XmlNode
            Dim rtSubRoot As Xml.XmlNode
            Dim arRoot As String() = strRoot.Split("/")
            Dim id As Integer

            rtSubRoot = xmlDOC.SelectSingleNode(strRoot)
            For Each rtRootNode In rtSubRoot.ChildNodes ' xmlDOC.DocumentElement(rtSubRoot.LocalName)
                If rtRootNode.NodeType = ValueType Then
                    Return rtRootNode.Value
                End If
            Next
            Return Nothing
        End Function
0
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
28 oct. 2010 à 13:40
Pour information, le message d'erreur obtenu avec le premier code que tu donne vient du fait que tu déclare 2 fois la variable rtNode :

Dim rtRootNode, rtNode, rtNode2 As Xml.XmlNode
...
       For Each rtNode As Xml.XmlNode In rtRootNode.ChildNodes
...


Soit tu enlevé rtNode du Dim et tu garde le For Each tel quel,
soit tu garde rtNode dans le Dim, dans ce cas tu écrit le For Each de la sorte :
       For Each rtNode In rtRootNode.ChildNodes



[i][b]---- Sevyc64 (alias Casy) ----
[hr]# LE PARTAGE EST NOTRE FORCE #/b/i
0
Rejoignez-nous