The backstory is that I want to be able to initialize a blank row on for a DataTable without having to always check for DBNull.Value. I have an extension method that creates a new blank row and then fills each of the values in the row with the default type for that column.

<!-- language: lang-vb -->
<Extension()> _
Public Function NewBlankRow(ByVal table As DataTable) As DataRow
    Dim row As DataRow = table.NewRow()
    For Each col As DataColumn In table.Columns
        row(col.ColumnName) = CType(Nothing, col.DataType)
    Next
    Return row
End Function

I get the following message for the second usage of col (the one within CType)

Cannot resolve symbol 'col'

For C-Sharper's, Calling CType(Nothing, T) should be the same as calling Default<T>

Is there a better way to do this? Why can't I access col within CType?

CType is not a runtime function. It's compile time. The data type in the second argument must be a specified at compile time.

http://msdn.microsoft.com/en-us/library/4x2877xb%28v=vs.80%29.aspx

CType is compiled inline, which means that the conversion code is part of the code that evaluates the expression. In some cases there is no call to a procedure to accomplish the conversion, which makes execution faster.

It's not the same as Default<T> in this instance because NewBlankRow is not a generic method. Regardless, Default<T> is also compile time and is not available when T is specified as a run-time expression.

To create a default instance of col.DataType you could use reflection, such as Activator.CreateInsatnce(col.DataType).