VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,821 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello all
In my code below I wanna replace CType by Try/Direct Cast / Convert.ToX / etc
Which one is the best fit to use? :)
Private Sub DataListViewEx_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataListViewEx.DragDrop
Dim MyData() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
Each approach has its advantages and considerations:
TryCast
when you want to handle a potential failure gracefully and continue with the program execution even if the cast fails.DirectCast
when you are confident that the cast will always succeed and want to raise an exception if it doesn't.Convert.ToX
when you are converting the object to a different data type, and you are sure that the conversion will work without raising an exception.There is a helpful table that compares the various methods at https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/ctype-function#conversion-keywords