| Top 10 reasons VB.NET is better than C# |
Applies To |
|
| OS: |
Windows .NET Framework |
|
Very few programmers have the luxury of toiling on their code for a really long time - we are expected to churn apps out in a quick manner. Which means that to be productive, we must use tools that enable us to be that. So let's get practical, religion aside, with an eye on programmer productivity, here are the top 10 reasons why VB.NET is better than C#, in no particular order
//get the form state from Ini File
//following line fails
FormWindowState eState = oIni.GetValue("FormState");
//must use cast (FormWindowState)
FormWindowState eState = (FormWindowState) oIni.GetValue("FormState");
//even this does not work
FormWindowState eState = 1;
//you have to use a cast even on simple numbers
FormWindowState eState = (FormWindowState) 1;
//finally set the value
this.WindowState = eState;
So here we have a case where Form.WindowState should accept value such as 1, but refuses to. IMO,
the compiler should be able to convert on the fly. I don't mind strong typing. I do mind stupid
typing. In VB.NET all the lines below work.
me.WindowState = 1
me.WindowState = val(oIni.GetValue("FormState"))
| C# | VB.NET |
![]() | ![]() |
| VB.NET | C# |
'instantiate a textbox Dim a As New TextBox() 'Update some class With MyClassName .ThisProperty = "sss" .ThatProperty = "sss" .ID = 4 .Name = "Frank" End With |
//instantiate a textbox - Word 'TextBox' had to be written twice TextBox a = new TextBox(); //Update some class - count how many times MyClassName had to be typed??? MyClassName.ThisProperty = "sss" MyClassName.ThatProperty = "sss" MyClassName.ID = 4 MyClassName.Name = "Frank" |
Class Example Public Event SomethingOccured() Public Sub Method1() RaiseEvent SomethingOccured End Sub End Sub End Class Class Papa Private WithEvents oExample as new Example() Private Sub SomethingChanged() Handles oExample.SomethingOccured ... End Sub 'or use AddHandler function AddHandler oExample.SomethingOccured, AddressOf x Private Sub SomethingChanged() ... End Sub End ClassAs you can see extremely easy and no need to much with delegates which are a lot more code-intensive. Now, mind you, VB.NET still fully supports the delegates, but for 99% of the time standard events do the trick.
Do
Dim attempt As Integer
Try
' something that might cause an error.
Catch ex As IO.FileLoadException When attempt < 3
If MsgBox("do again?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
Exit Do
End If
Catch ex As Exception
' if any other error type occurs or the attempts are too many, do the following.
MsgBox(ex.Message)
Exit Do
End Try
' increment the attempt counter.
attempt += 1
Loop
Thanks to German Voronin for this.
c#
case 1
case 2
case 3
(...) // Do Action
break;
case 4
break;
vb.net
case 1,2,3
(...) ' Do Action
case 4
Thanks to Brian Yule and Vashon Kirkman for this piece.