Converting a DataTable Column to an Array?
-
Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function:
Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function
-
Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function:
Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function
-
Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function:
Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function
I'm not really confirm with the VB-Syntax but arrValues(i2) = dt.Rows(i2).Item(intColomn) looks like you try to access an double array that is not initialized (or doesn't have enough space). Look at you definition: Dim arrValues() As Double = New Double(){} <- maybe here you should say that this array should have dt.Rows.Count elements
Greetings Covean
-
Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function:
Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function
-
Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function:
Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function
First of all why dont you use
List
.... as your collection. It is always better to useArrayList
or GenericList
as collection rather than going for Array. Next, I always prefer to useForEach
rather thanFor
when I am looping through anEnumerable
. If I was in your situation I would write this as :Private Function DataTableToArray(ByVal x As DataTable, ByVal intCol As Integer) As List(Of Double) Dim lst As New List(Of Double) For Each dr As DataRow In x.Rows lst.Add(dr.Item(intCol)) Next Return lst End Function
Or if you still want to use Array you might use
List(Of Double).ToArray
as well. Cheers. :rose:Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript -
Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function:
Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function
Thanks a lot guys. As soon as I'm home I'll give all of these options a try. :)
-
Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function:
Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function
-
Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function:
Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function
Thank you guys, my problem is solved! :) Greatly appreciate it.