I think this scenario is best suited because DirectCast has a false idea of compile-time type security checking for the type of non-object objects and is intended only to be back.
float f = 10; long l = f; Option Strict On Dim f As Single = 10 Dim l As Long = f
A C # encoder, upon detecting that a float cannot be assigned for a long time and will not compile, will do the following:
long l = (long)f;
It is right.
Now, back to our VB.NET encoder, after finding that the float is not assigned for a long time and does not compile, it will try to do this:
Dim l As Long = DirectCast(f, Long)
In a few seconds ...
VB.Net Programmer: "Please let me make my suggestions, please compile, please ... !!!"
After a few moments via GoogleDu and MSDN:
VB.NET programmer: "Ah ... so I need to use this CLng or CType construct to cast variables"
Dim l As Long = CLng(f)
This is what I meant by DirectCast, it has a false impression of the security of compilation type checking. DirectCast just has to be the opposite if the programmer does not know when and where to use them. DirectCast is a protective blanket that does not wear all the time.
How useful is DirectCast in this scenario if it will not be used in the end?
[EDIT]
@Jules
I do not claim that all VB.NET programmers do not know what the real use of DirectCast is, some of them really know that DirectCast is only for use with object types (and primitive types, objects).
One scenario is when VB.NET encoding, which encodes existing C # code, to VB.NET comes to an erroneous conclusion, with the expected (whether correct or not) language symmetry to each other.
When he sees this construct in code ...
TextBox txt = (TextBox)sender;
... He will translate this:
Dim txt As TextBox = DirectCast(sender, TextBox)
It is right.
Now, since we programmers love symmetry, some of us (maybe me too, if I don’t know CLng) will try to convert this code ...
float f = file.ReadFloat(0); long l = (long)f;
... to that:
Dim f As Single = file.ReadFloat(0) Dim l As Long = DirectCast(f, Long)
If the C # person is the one who will convert the C # code to VB.NET, he will be disappointed with the apparent lack of symmetry here.
But for the person VB.NET, who is tasked with converting C # code to VB.NET, he will get the impression that the C # compiler does not catch incompatible type assignments, but VB.NET catches it. Now, for this obvious discovery, the VB.NET feature for his colleagues and some forums boasts.
But so that the VB.NET programmer is not mistaken in mistakenly determining the intent of the first code. The C # code snippet above began its life as follows: it was originally written as follows:
float f = file.ReadFloat(0); long l = f;
And this will not compile, the C # compiler catches incompatible type assignments, in the same way that the equivalent VB.NET with Option Strict On also not compile this (although it will not compile when Option Strict set to On , too soft) . Therefore, we need to use the (long) type float for long use. Becomes as follows: long l = (long)f;
Now, in order to pour one type of variable into another compatible type, in the same spirit that we will convert to this code ...
TextBox txt = (TextBox)sender;
... to this code:
Dim txt As TextBox = DirectCast(sender, TextBox)
We must convert this code ...
long l = (long)f;
... to this code:
Dim l As Long = DirectCast(f, Long) ' will not compile
But alas, this will not be compiled when casting between compatible primitive types, in which case DirectCast is shortened. It does not offer any symmetry to the C # code described above; it cannot be used for primitive primitive types, despite its name Direct Cast .
As I see it, DirectCast should be called CastObject , since it can only use object types (as well as primitive types placed in an object). DirectCast really does not have a business with the assignment of compatible primitive types (integer, double and their lower and higher counterparts). When assigned between compatible primitive types, DirectCast is no longer useful, especially if you somehow move it and replace it with a suitable one.
Or in another way I see this, the DirectCast construct should be changed so that it can display compatible types, for example, like older and newer languages, since then, for example, C, C ++, C #, Java, Delphi, D, etc. .d. Having done this, he will offer significant VB.NET symmetry for other languages when it comes to type casting. By doing this, we can also throw away (hypothetically only, we cannot make other programs fail by relying on old functions), the whole set of functions whose names are not directly mapped to its types (for example, CInt, CDbl, CSng, etc. .), We will use DirectCast instead.