Bonjour,
J'ai un problème avec la compréhension sur les scopes
Dans le code ci-dessous mon Test1 retourne une erreur, alors que le Test2 fonctionne.
Je ne comprends pas pourquoi il ne trouve pas la "ligne 1" puisque à chaque fois j'importe le DT de ma Class dans un nouveau DT disponible dans le scope courant (DT_1, DT_2, DT_3), qui lui ne devrait pas être vidé.
Merci de votre aide
myClass.vb
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 Public Class myClass Implements IDisposable Public msgErr As String Public DT As New DataTable Public clearDT As Boolean = True Public Sub Dispose() Implements IDisposable.Dispose msgErr = Nothing If clearDT Then DT.Clear() DT = Nothing End If GC.SuppressFinalize(Me) End Sub Public Function fct1() As Boolean Dim output As Boolean = False DT.Clear() Try DT.Columns.Add("id", GetType(Integer)) DT.Columns.Add("title", GetType(String)) DT.Columns.Add("descibe", GetType(String)) DT.Columns.Add("Date", GetType(DateTime)) DT.Rows.Add(1, "fct1() : title #1", "describe #1", DateTime.Now) DT.Rows.Add(2, "fct1() : title #2", "describe #2", DateTime.Now) DT.Rows.Add(3, "fct1() : title #3", "describe #3", DateTime.Now) output = True Catch ex As Exception msgErr = ex.ToString End Try Return output End Function Public Function fct2() As Boolean Dim output As Boolean = False DT.Clear() Try DT.Columns.Add("id", GetType(Integer)) DT.Columns.Add("title", GetType(String)) DT.Columns.Add("descibe", GetType(String)) DT.Columns.Add("Date", GetType(DateTime)) DT.Rows.Add(1, "fct2() : title #1", "describe #1", DateTime.Now) DT.Rows.Add(2, "fct2() : title #2", "describe #2", DateTime.Now) DT.Rows.Add(3, "fct2() : title #3", "describe #3", DateTime.Now) output = True Catch ex As Exception msgErr = ex.ToString End Try Return output End Function Public Function fct3() As Boolean Dim output As Boolean = False DT.Clear() Try DT.Columns.Add("id", GetType(Integer)) DT.Columns.Add("title", GetType(String)) DT.Columns.Add("descibe", GetType(String)) DT.Columns.Add("Date", GetType(DateTime)) DT.Rows.Add(1, "fct3() : title #1", "describe #1", DateTime.Now) DT.Rows.Add(2, "fct3() : title #2", "describe #2", DateTime.Now) DT.Rows.Add(3, "fct3() : title #3", "describe #3", DateTime.Now) output = True Catch ex As Exception msgErr = ex.ToString End Try Return output End Function End Class
maPage.aspx.vb
TEST 1 - HS
Retour de TEST 1 - HS
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 Private Sub test1() Dim rslt As String = "" Dim err As String = "" Dim mc As New myClass Try If mc.fct1() Then Dim DT_1 As New DataTable DT_1 = mc.DT For i As Integer = 0 To DT_1.Rows.Count - 1 rslt &= "-<b>id :</b> " & DT_1.Rows(i)("id") & " - <b>title :</b> " & DT_1.Rows(i)("title") & "<br/>" If mc.fct2() Then Dim DT_2 As New DataTable DT_2 = mc.DT For j As Integer = 0 To DT_2.Rows.Count - 1 rslt &= "--<b>id :</b> " & DT_2.Rows(j)("id") & " - <b>title :</b> " & DT_2.Rows(j)("title") & "<br/>" If mc.fct3() Then Dim DT_3 As New DataTable DT_3 = mc.DT For k As Integer = 0 To DT_3.Rows.Count - 1 rslt &= "---<b>id :</b> " & DT_3.Rows(k)("id") & " - <b>title :</b> " & DT_3.Rows(k)("title") & "<br/>" Next Else err = "mc.fct2() : " & mc.msgErr & "<br/>" End If Next Else err = "mc.fct2() : " & mc.msgErr & "<br/>" End If rslt &= "<br/>" Next Else err = "mc.fct1() : " & mc.msgErr & "<br/>" End If Catch ex As Exception err = ex.ToString End Try mc.Dispose() mc = Nothing If rslt <> "" Then litRslt.Text = rslt End If If err <> "" Then litErr.Text = err End If End Sub
TEST 2 - OK-id : 1 - title : fct1() : title #1
System.IndexOutOfRangeException: Aucune ligne à la position 1. à System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex) à System.Data.DataRowCollection.get_Item(Int32 index) à maPage.Page_Load(Object sender, EventArgs e) dans X:\...\maPage.aspx.vb:ligne 23
Retour de TEST 2 - OK
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 Private Sub test2() Dim rslt As String = "" Dim err As String = "" Dim mc_1 As New myClass Try If mc_1.fct1() Then Dim DT_1 As New DataTable DT_1 = mc_1.DT For i As Integer = 0 To DT_1.Rows.Count - 1 rslt &= "-<b>id :</b> " & DT_1.Rows(i)("id") & " - <b>title :</b> " & DT_1.Rows(i)("title") & "<br/>" Dim mc_2 As New myClass If mc_2.fct2() Then Dim DT_2 As New DataTable DT_2 = mc_2.DT For j As Integer = 0 To DT_2.Rows.Count - 1 rslt &= "--<b>id :</b> " & DT_2.Rows(j)("id") & " - <b>title :</b> " & DT_2.Rows(j)("title") & "<br/>" Dim mc_3 As New myClass If mc_3.fct3() Then Dim DT_3 As New DataTable DT_3 = mc_3.DT For k As Integer = 0 To DT_3.Rows.Count - 1 rslt &= "---<b>id :</b> " & DT_3.Rows(k)("id") & " - <b>title :</b> " & DT_3.Rows(k)("title") & "<br/>" Next Else err = "mc_3.fct2() : " & mc_3.msgErr & "<br/>" End If mc_3.Dispose() mc_3 = Nothing Next Else err = "mc_2.fct2() : " & mc_2.msgErr & "<br/>" End If mc_2.Dispose() mc_2 = Nothing rslt &= "<br/>" Next Else err = "mc_1.fct1() : " & mc_1.msgErr & "<br/>" End If Catch ex As Exception err = ex.ToString End Try mc_1.Dispose() mc_1 = Nothing If rslt <> "" Then litRslt.Text = rslt End If If err <> "" Then litErr.Text = err End If End Sub
-id : 1 - title : fct1() : title #1
--id : 1 - title : fct2() : title #1
---id : 1 - title : fct3() : title #1
---id : 2 - title : fct3() : title #2
---id : 3 - title : fct3() : title #3
--id : 2 - title : fct2() : title #2
---id : 1 - title : fct3() : title #1
---id : 2 - title : fct3() : title #2
---id : 3 - title : fct3() : title #3
--id : 3 - title : fct2() : title #3
---id : 1 - title : fct3() : title #1
---id : 2 - title : fct3() : title #2
---id : 3 - title : fct3() : title #3
[...]
Partager