Public Class VerTest
Public a As Integer
Public b As Integer
End Class
Private Function Test() As List(Of VerTest)
Dim ListTest As List(Of VerTest) = New List(Of VerTest)()
Dim ListTest1 As List(Of VerTest) = New List(Of VerTest)()
Dim strSQL As String
Dim dtAdapter As SqlDataAdapter
Dim ds = New System.Data.DataSet()
strSQL = "SELECT "
strSQL += "* "
strSQL += "FROM Table1 "
dtAdapter = New SqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(ds)
dtAdapter = New SqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
ListTest = New List(Of VerTest)(ds.Tables(0).Rows.Count)
For i As Integer = 0 To ds.Tables(0).Rows.Count
Dim VerTest As New VerTest()
If ListTest.Count <= ds.Tables(0).Rows.Count - 1 Then
ListTest.a = ds.Tables(0).Rows(i)(0)
End If
ListTest.Add(VerTest)
Next
End If
For j As Integer = 0 To ListTest.Count - 1
Dim VerTest As New VerTest()
If (ListTest(j).lease_kikan_kbn <> "") Then
Dim lkk_split() As String = Split(ListTest(j).lease_kikan_kbn, "-")
Dim a As Integer
For a = 0 To lkk_split.Length - 1
VerTest.b = 1
Next
End If
ListTest1.Add(VerTest)
Next
Return ListTest
End Function
Public b(10) As Integer
Public Class VerTest
Public a As Integer
End Class
Private Function Test() As List(Of VerTest)
Dim ListTest As List(Of VerTest) = New List(Of VerTest)()
Dim ListTest1 As List(Of VerTest) = New List(Of VerTest)()
Dim strSQL As String
Dim dtAdapter As SqlDataAdapter
Dim ds = New System.Data.DataSet()
strSQL = "SELECT "
strSQL += "* "
strSQL += "FROM Table1 "
dtAdapter = New SqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(ds)
dtAdapter = New SqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
ListTest = New List(Of VerTest)(ds.Tables(0).Rows.Count)
For i As Integer = 0 To ds.Tables(0).Rows.Count
Dim VerTest As New VerTest()
If ListTest.Count <= ds.Tables(0).Rows.Count - 1 Then
ListTest.a = ds.Tables(0).Rows(i)(0)
End If
ListTest.Add(VerTest)
Next
End If
For j As Integer = 0 To ListTest.Count - 1
Dim VerTest As New VerTest()
If (ListTest(j).lease_kikan_kbn <> "") Then
Dim lkk_split() As String = Split(ListTest(j).lease_kikan_kbn, "-")
Dim a As Integer
For a = 0 To lkk_split.Length - 1
b(a) = 1
Next
End If
Next
Return ListTest
End Function
อ่านแล้วงง 555
1. ทำไมประกาศ Public b(10) As Integer ทั้งๆที่ b(a) = 1 น่าจะใช้วิธี redim (แต่ก็ไม่ใช่เรื่องผิดครับ)
2.ทำไมเรียกใช้
dtAdapter = New SqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(ds)
dtAdapter = New SqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(ds)
เรียกครั้งเดียวไม่ได้เหรอครับ?
ค่า a มีค่าเป็น
1-2
1-2-3
1-2-3-4
... Code (VB.NET)
For j As Integer = 0 To ListTest.Count - 1
Dim VerTest As New VerTest()
If (ListTest(j).lease_kikan_kbn <> "") Then
Dim lkk_split() As String = Split(ListTest(j).lease_kikan_kbn, "-")
Dim a As Integer
For a = 0 To lkk_split.Length - 1
b(a) = 1
Next
End If
Next
จากโค้ดกับข้อมูลดังกล่าว b จะไม่มีการเพิ่มข้อมูล b จะมีการเพิ่มข้อมูลลงไปแค่ 4 ข้อมูล
เพราะมันจะเพิ่มข้อมูลตาม a a = 0 To lkk_split.Length - 1 ซึ่ง ค่าที่มากสุดจะได้จาก 1-2-3-4
ถ้าต้องการเพิ่ม b เรื่อยๆ ต้องประกาศ
Public b(1) As Integer
For a = 0 To lkk_split.Length - 1
b(UBound(b)) = a
ReDim b(UBound(b) + 1) 'เพิ่มเรื่อยๆครับแบบนี้ง่ายกว่าครับไม่ยึดตาม a
Next
Public Class VerTest
Public a As Integer
Public b As Integer
End Class
Private Function Test() As List(Of VerTest)
Dim ListTest As List(Of VerTest) = New List(Of VerTest)()
Dim ds = New System.Data.DataSet()
Dim dtAdapter As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Table1 ", objConn)
dtAdapter.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
For i As Integer = 0 To ds.Tables(0).Rows.Count
If ListTest.Count <= ds.Tables(0).Rows.Count - 1 Then
If (ds.Tables(0).Rows(i)(0) <> "") Then
Dim lkk_split() As String = Split(ds.Tables(0).Rows(i)(0), "-")
For a As Integer = 0 To lkk_split.Length - 1
Dim verTest As New VerTest()
verTest.a = 1 'แล้วแต่จะใส่ครับ
verTest.b = 2 'แล้วแต่จะใส่ครับ
ListTest.Add(verTest)
Next
End If
End If
Next
End If
Return ListTest
End Function
Function Test() As List(Of verTest)
Dim dtFake As DataTable = ds.Tables(0)
Return dtFake.AsEnumerable().Select(Function(หอย)
Dim งาม As New verTest() With {.a = หอย.Item(0),
.b = 0,
.lease_kikan_kbn = หอย.Item(2)
}
If Not String.IsNullOrEmpty(งาม.lease_kikan_kbn) Then
Dim lkk_split() As String = งาม.lease_kikan_kbn.Split({"-"}, StringSplitOptions.RemoveEmptyEntries)
งาม.b = 1
End If
Return งาม
End Function).ToList()
End Function