Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET weak-typed? strong-typed?

Tags:

vb.net

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 statement is still valid. Why? Why? Why? Does anyone know how to use vb.net like C#?

like image 375
Alex Yeung Avatar asked Aug 18 '10 23:08

Alex Yeung


1 Answers

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
like image 146
JaredPar Avatar answered Oct 11 '22 01:10

JaredPar