I'm used to C# and therefore I'm not too up to date with VB.Net syntax.
However, you should not try a cast operation in your case (a cast is used to
convert between compatible type (types for which an implicit or explicit
convertion is defined).
Your second line works simply because your "o" object is a string and you
convert it to a string. In any case, because ToString() method is defined
for all object, you will be always able to cast to a string. The 3rd line
has a problem. Your "o" object has no knowledge about how to convert to a
DateTime type.
What you should do is a parse operation:
Module Module1
Sub Main()
Dim str As String = "1900-01-01"
Dim dt As DateTime
If (DateTime.TryParse(str, dt)) Then
Console.WriteLine(dt)
End If
Console.ReadLine()
End Sub
- José
"Leo Leys" <Le*****@discussions.microsoft.coma écrit dans le message de
news:
C6**********************************@microsoft.com...
Hi,
The code below generates a compile error on line 3 (dim d...)
Dim o As Object = "1900-01-01"
Dim s As String = TryCast(o, String)
Dim d As DateTime = TryCast(o, DateTime)
The error is : TryCast operand must be reference type, but 'Date' is a
value
type.
How can I resolve this?
The second line of code does not generate a compile error at all.
Thanks.
Leo Leys