views:

52

answers:

2

Hi all,

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#?

+7  A: 

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
JaredPar
wow... thank you very much!!! Once I turn it on, over 1000 compilation errors. hahahaa~ Really thanks!!!
Alex Yeung
One more question, do you know how to make a strong-typed `IIf`? In C#, the `?:` operator is strong-typed.
Alex Yeung
If you're using Visual Studio 2008 use `If` instead of `IIf` IIRC
JaredPar
You could also set Option Strict On at project level so all your code files have strong type. Go to the Compile page of your project's properties and set Option Strict over there.
Alex Essilfie
@JaredPar: Wrong. VB.NET's `IIf` is better than VB6's. In VB6, both the true and false part of the `IIf` function are evaluated irrespective of the condition being true or false. In VB.NET the condition is evaluated first and depending on the result, the true Statement or false statement is executed. This is equivalent to C#'s `condition ? truePart : falsePart`. Unless you want us to also avoid using C#'s ternary operator, I see no reason we shouldn't use VB.NET's `IIf`.
Alex Essilfie
@Alex Jared is right, actually. IIf acts like the vb6 version, and If is like ? in C#. Jared does work on the vb.net compiler, you know!
MarkJ
@MarkJ: My bad. I thought he was referring to the `If(condition, truePart, falsePart)` function of VB.NET.
Alex Essilfie
@Alex But I think Jared *was* referring to the `If(condition, truePart, falsePart)` operator in Vb.Net? `If` short-circuits, unlike `IIf`, and `If` also works better with type inference. See the docs http://msdn.microsoft.com/en-us/library/bb513985(v=VS.90).aspx
MarkJ
@Alex, @MarkJ is correct that I was referring to the short-circuiting, type inference friendly `If` operator. But I don't work on the VB.Net compiler anymore although i do still participate in C# and VB.net design. I work on a language / OS research project now.
JaredPar
A: 

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)

asawyer