MattP
Well-known member
C#'s 'as' keyword will let you do this:
Here's what I would assume the vb.net equivalent would be:
There's an intellisense error in the TryCast stating the operand must be a reference type but Integer? is a value type.
Intellisense on Nullable(Of Integer) says 'Represents an object whose underlying type is a value type that can also be assigned null like a reference type.'
It seems C#'s 'as' handles this like a reference type where TryCast doesn't have this built in.
In VB10 I was able to take advantage of the new CTypeDynamic function to do the casting. Conversion.CTypeDynamic Method (Microsoft.VisualBasic)
Or:
There's a cost here as CTypeDynamic examines the type at runtime.
My question is what is the elegant way to handle this without CTypeDynamic?
VB.NET:
int? input = value as int?
Here's what I would assume the vb.net equivalent would be:
VB.NET:
Dim input As Integer? = TryCast(value, Integer?)
There's an intellisense error in the TryCast stating the operand must be a reference type but Integer? is a value type.
Intellisense on Nullable(Of Integer) says 'Represents an object whose underlying type is a value type that can also be assigned null like a reference type.'
It seems C#'s 'as' handles this like a reference type where TryCast doesn't have this built in.
In VB10 I was able to take advantage of the new CTypeDynamic function to do the casting. Conversion.CTypeDynamic Method (Microsoft.VisualBasic)
VB.NET:
Dim input As Integer? = CTypeDynamic(Of Integer?)(value)
Or:
VB.NET:
Dim input As Integer? = CTypeDynamic(Value, GetType(Integer?))
There's a cost here as CTypeDynamic examines the type at runtime.
My question is what is the elegant way to handle this without CTypeDynamic?