Replace CType

Peter Volz 1,295 Reputation points
2023-08-05T00:38:54.1933333+00:00

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())
VB
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
{count} votes

2 answers

Sort by: Most helpful
  1. Vahid Ghafarpour 22,800 Reputation points
    2023-08-05T03:20:25.26+00:00

    Each approach has its advantages and considerations:

    • Use TryCast when you want to handle a potential failure gracefully and continue with the program execution even if the cast fails.
    • Use DirectCast when you are confident that the cast will always succeed and want to raise an exception if it doesn't.
    • Use 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.
    0 comments No comments

  2. RLWA32 47,956 Reputation points
    2023-08-05T09:35:47.41+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.