I used C# before. However, after I joined in a new company, I need to write vb.net. However I find very difficult to write it. Because I found that vb.net is not strong-typed. It is really not strong-typed? Or any settings for that? Here are examples.
If (Me.Check1() And Me.Check2()) Then
From my C# knowledge, once Me.Check1()
returns false, Me.Check2()
will not be executed. However I was wrong. And
is for bitwise operations. I should use AndAlso
for boolean operations. So it should be
If (Me.Check1() AndAlso Me.Check2()) Then
The problem is that If (Me.Check1() And Me.Check2()) Then
is still valid and no compilation error. I really want to know if I am able to check such "inappropriate" operations.
And
and AndAlso
is just one of the cases.
Sometimes, I need to do ignore cases string comparisons. However, everyone makes mistakes. Sometimes, I did
If (String.Compare(Me.string1, Me.string2, True)) Then
I think everyone knows the problem. It should be
If (String.Compare(Me.string1, Me.string2, True) = 0) Then
However, I still cannot check such case during compilation.
I love C# because it helps us to find many problems during compilation. However, VB.NET makes me very confused and many errors must be determined during run-time and testing.
One more example is that
Public Sub MySub(ByVal obj as Object)
Console.WriteLine(obj.MyProperty)
End Sub
In vb.net, this kind of s开发者_JS百科tatement is still valid. Why? Why? Why? Does anyone know how to use vb.net like C#?
VB.Net has both strong and weak typed modes. It is controlled via the Strict option. This can be set at a project or source file level.
' Enable VB.Net strong typing
Option Strict On
' Enable VB.Net weak / dynamic typing
Option Strict Off
I recently hit the Option Strict button on an older vb.net project and spent several hours fixing type errors, but it was worth it. Be sure to be very carefull around DateTime to string, and string to DateTime conversions. I had several instances of "12:00:00 AM" tacked onto the end of textboxs that would cause issues with the old codebase.
Once the errors are clear you can turn Option Infer on so Dim will work like c#'s var, and you don't have to explicitly type all your Linq queries.
(I'd have made this a comment but it seems I need more points, sorry)
精彩评论