Reputation: 3
I found an example in C# and from my understanding there is no alternative to 'var' in VB.NET. I am trying to create a datatable that will populate depending on a LINQ command further down in my code that calls this function. I have searched for a solution, but unable to find anything that works. Any assistance on what I should use would be appreciated. Note that I do have both Option Strict and Option Infer on as well.
Private Shared Function ToDataTable(rows As List(Of DataRow)) As DataTable
Dim table As New DataTable()
table.Columns.Add("Title")
table.Columns.Add("Console")
table.Columns.Add("Year")
table.Columns.Add("ESRB")
table.Columns.Add("Score")
table.Columns.Add("Publisher")
table.Columns.Add("Developer")
table.Columns.Add("Genre")
table.Columns.Add("Date")
For Each row As var In rows
table.Rows.Add(row.ItemArray)
Next
Return table
End Function
Upvotes: 0
Views: 1030
Reputation: 6367
The VB equivalent is simply Dim
, without any strong typing.
Dim sName = "John Henry"
In this example, the compiler infers type String
(when Option Infer
is set to On
).
In your example, you may omit the As var
portion. The compiler will infer type DataRow
.
Upvotes: 1
Reputation: 22866
.NET already has .CopyToDataTable
extension for that:
Dim table As DataTable = rows.CopyToDataTable
Upvotes: 2
Reputation: 4883
Tag your questions well, in this case there is no C# issue. Your problem is your are not writing an actual type on the foreach statement. This will fix it:
For Each row As DataRow In rows
table.Rows.Add(row.ItemArray)
Next
Upvotes: 0
Reputation: 6542
C# uses 'var' for implicit typing - VB uses Option Infer On combined with omitting the type. The VB equivalent is:
Option Infer On
...
For Each row In rows
table.Rows.Add(row.ItemArray)
Next row
Upvotes: 3