Is casting really necessary in this code?

Still learning about Xojo programming. Reading the Introduction to Programming With Xojo PDF, I see this code example, where Course is an explicitly defined class elsewhere in the code, and this code is the DoubleClick event on a ListBox control object:

Dim c As Course If Me.ListIndex <> -1 Then If Me.CellTag(Me.ListIndex,0) IsA Course Then c = Course(Me.CellTag(Me.ListIndex,0)) MsgBox c.Instructor End If End If

Line 4 is explained this way:

When I replace line 4 with

     c = Me.CellTag(Me.ListIndex,0)

the code compiles and runs the same way, without Casting. Can I assume that this is just showing me how to do Casting, but really isn’t necessary here because C was declared As Course already? Or, is there an important reason to do it here?

The casting is not truly necessary in this case because CellTag is a Variant so it actually contains an instance of Course and will get assigned correctly.

But including the cast makes it very clear what you are expecting the code to do, so it is often recommended.