10 Reasons Why Visual Basic is Better Than C#
After having converted a whole lot of training materials based on VB.NET into C#, Andy ‘Wise Owl’ Brown decided to write a tongue-in-cheek rant whilst he could still remember the pain-points. 'Convert to VB.NET! You have nothing to lose but your semi-colons! '
Visual Basic is a better programming language than Visual C#. Who says so? This article! Here are 10 reasons why you should always choose VB over C#.
1 – “Rose is a rose is a rose is a rose”
This is a quotation from Gertrude Stein’s 1922 play Geography and Plays. However, the poetry wouldn’t work in C#, because – unforgivably – it’s a cASe-SeNSitIvE language. This is madness!Before I start ranting, let me just acknowledge that case-sensitivity confers one (and only one) advantage – it makes it easier to name private and public properties:
Writing properties like this means that you can refer to the public Name property, and it’s obvious what the private equivalent will be called (name).
| // private version of variableprivate string name = null;public string Name { get { return txtName.Text; } set { name = txtName.Text; } } |
- You keep having to switch between upper and lower case when typing, causing RSI in your poor little fingers as you reach for the inconsiderately located Shift key.
- You are much more likely to make mistakes - are you sure you meant to type DateOfBirth, or should it have been dateofbirth?
- When you accidentally leave Caps lock on, it really matters.
It doesn’t matter if you disagree with everything else in this article: case-sensitivity alone is sufficient reason to ditch C#!
2 – The Switch clause
Both VB and C# contain a way of testing mutually exclusive possibilities, the Select Case and SwitchA Visual Basic Select Case clause, returning a description of how old someone is. The age range for a young person is a tad generous, reflecting the age of the author of this article.
A Visual Basic Select Case clause, returning a description of how old someone is. The age range for a young person is a tad generous, reflecting the age of the author of this article.
|
Select Case AgeEntered
Case Is < 18 txtVerdict.Text = " Case Is < 50 txtVerdict.Text = " Case Is < 65 txtVerdict.Text = " Case Else txtVerdict.Text = " |
switch (AgeThreshold) {
case 18 : txtVerdict.Text = " break; case 50 : txtVerdict.Text = " break; case 65 : txtVerdict.Text = " break; default: txtVerdict.Text = " break;} |
It’s easy to forget to type in each of these Break statements!
|
3 – Event-Handling code
This is specific to Visual Studio (I’m using 2010, the latest version). Suppose I want to attach code to anything but the default Click event of a typical button:
Let’s suppose that I want to attach code to the MouseHover event of this button.
|
a) First choose the object from the drop list.
|
b)Then choose the event you want to code.
|
You can double-click to attach code to this event for the selected button – but that’s the only simple way to create it for C#.
|
Private Sub btnApply_
ByVal e As System.Even
MessageBox.Show("
End Sub
|
Globally changebtnApply to the new button’s name in code, and everything will work as before.
|
4 –Stupid symbols
C# was written by academics. It shows. Consider this table of C# symbols and their VB equivalents:What you're trying to do | C# Symbol | VB Equivalent |
Test if two conditions are both true | && | and |
Test if one or other condition is true | || | or |
Test if a condition is not true | ! | not |
Concatenate two strings of text | + | & |
Test if a condition is true within an if statement | == | = |
5 – Autocorrection in Visual Basic actually works
IntelliSense works much better for Visual Basic than for Visual C#. Take just one example – creating a write-only property. Let’s start with Visual Basic:
When you press return at the line end…
| |
WriteOnly Property
Set(value As String)
End Set
End Property
|
… You get this fully-completed clause.
|
When you press return here, nothing happens (other than a blank line appearing).
|
6 – Lack of supported functions
Here are a couple of functions I use from time to time in VB:Function | What it does |
IsNumeric | Tests if a value can be converted to a number |
PMT | Calculates the annual mortgage payment for a loan |
7 – That wretched semi-colon
Why do I have to end every line in C# with a semi-colon? The argument used to be that it avoided the need to use continuation characters in Visual Basic:
MessageBox.Show( _
text:="This article is a bit opinionated", _ caption:="Message") |
You used to have to use an underscore as a continuation character to show incomplete lines of code in VB.
|
Someone commented on my original (much shorter) blog about this:
"In a short amount of time you'll type those semi-colons without thinking about it (I even type them when programming in visual basic)."
That’s like saying that you’ll soon get used to not having any matches to light your fire, and you’ll even start rubbing sticks together to start a fire when you finally manage to buy a box!
"In a short amount of time you'll type those semi-colons without thinking about it (I even type them when programming in visual basic)."
That’s like saying that you’ll soon get used to not having any matches to light your fire, and you’ll even start rubbing sticks together to start a fire when you finally manage to buy a box!
8 – Arguments and variables
The order of words in a C# variable declaration is wrong. When you introduce someone, you wouldn’t say, “This is my friend who’s a solicitor; he’s called Bob”. So why do you say:
string PersonName = "Bob";
To me:
Dim PersonName As String = "Bob"
…is much more logical. I also find the C# method of having to prefix arguments with the word out confusing, particularly as you have to do it both in the called and calling routine.9 – Strictness
C# is a much fussier language than Visual Basic (even if you turn Option Strict on in Visual Basic, this is still true). “And a good thing, too!”, I hear you cry. Well, maybe. Consider this Visual Basic code:
Enum AgeBand
Child = 18 Young = 30 MiddleAged = 60 SeniorCitizen = 90 End Enum Select Case Age Case Is < AgeBand.Child MessageBox.Show(" Case Else MessageBox.Show(" |
With Option Strict turned on this shouldn’t really work, as it’s comparing an integer with an enumeration – but VB has the common sense to realise what you want to do.
|
A less forgiving language…
What this means is that you end up having to fill your code with messy type conversions:
The simplest way of converting an enumeration to an integer; but why should you have to?
| // find out the age enteredint Age = Convert. MessageBox.Show("Child");} MessageBox.Show("Adult");} |
10 – Redimensioning arrays
If you want to dynamically change the length of an array in Visual Basic, you can use Redim Preserve. To do the same thing in Visual C#, you have to copy the array, add a value and then copy it back:
The vile, clunky C# method of extending an array.
|
string[] PartyGuests = new string[2];PartyGuests[0] = "Sarah Palin";PartyGuests[1] = "
// create a new extended arraystring[] tempGuests = new string[PartyGuests.Length + 1];// copy all of the elements from the old array into new oneArray.Copy(PartyGuests,temp System.Diagnostics.Debug. |
- Behind the scenes, the Redim Preserve command does exactly the same thing as the C# code above; and
- I should be using lists and not arrays anyway.
Conclusion
So those are my 10 reasons to code in Visual Basic. What are you waiting for, all you C# code-monkeys? Convert all of your code to VB – you have nothing to lose but your semi-colons!Original post is:https://www.simple-talk.
I like to disseminate knowledge that will I've accrued with the year to help enhance team functionality.
ReplyDeleteMy weblog; SEO收費
Good article! We are linking to this great content on our website.
ReplyDeleteKeep up the great writing.
my website ... free online dating personals