CodingCool Uncategorized VB.NET vs C#

VB.NET vs C#

In this article will be explaining why I like VB.NET over C#

Let’s start with some history. VB was intially released in 2002 and is VB6’s successor. Developers usually use Visual Studio to develop in VB. Generally VB developers, me included, use WinForms to create applications.

Here are topics we will be discussing:

  1. Symbols
  2. Semi-colons
  3. Variables
  4. Case-sensitivity
  5. Easy to use and understand

Symbols

This is a major problem with C#. If they can’t fix this C# is going to remain in the dumpster. Let’s compare this:

PurposeC#VB
Check both conditions true&&And
Check if one or the other condition is true|Or
Check if condition is false!Not
Check if condition is true===
Assign object a value==
Concatenate to strings+&
Modulo operator%Mod
Expontial operatorN\A^
Symbols

As you can VB obviously wins here.

Semi-colons

C#, you can’t ever leave the dumpster if you don’t scrap that semi-colon. Who wants to type a semi-colon at the end of every blasted line. Programming is hard enough the way it is without semi-colons.

Variables

Hello user, this a programmer and he’s called Joe. You generally don’t introduce someone like that so why do it in code. Look at these examples.

' VB.NET
Dim programmer As String = "Joe"
// C#
string programmer = "Joe";

Choose your pick.

Case-sensitivity

One major flaw and perhaps the biggest flaw is that it is CasEsEnSItIvE. This means that means you can have multiple variables with the same name:

// C#
var Test;
var tEST;
var tEst; // ect..

See what the problem is.

Easy to use and understand

Look at these two samples. Pretend your completely new programming and tell me which is easier to understand.

' VB.NET
If bTest1 = True Or (bTest2 = True And bTest3 = True) Then
   ' Do something
End If
// C#
if (bTest1 == true | (bTest2 == true & bTest3 == true))
{
   // Do something
}

It’s obvious which is simpler. Heres another sample:

' VB.NET
Dim objItems = lItems.Where(Function(x) x.IsActive = True)
// C#
var objItems = lItems.Where(x => x.IsActive == true);

Again which makes more sense.

' VB.NET
Module Module1

    Sub Main()
        Console.WriteLine("Hello World!")
    End Sub

End Module
// C#
internal static partial class Module1
{
    public static void Main()
    {
        Console.WriteLine("Hello World!");
    }
}

Now lets show a more advanced programm:

' VB.NET
Public Module modEncryption

    ' TAKEN FROM MS KB Q317535

    Public Function EncryptTripleDES(ByVal sIn As String, ByVal sKey As String) As String
        Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider
        Dim hashMD5 As New System.Security.Cryptography.MD5CryptoServiceProvider

        ' scramble the key
        sKey = ScrambleKey(sKey)
        ' Compute the MD5 hash.
        DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey))
        ' Set the cipher mode.
        DES.Mode = System.Security.Cryptography.CipherMode.ECB
        ' Create the encryptor.
        Dim DESEncrypt As System.Security.Cryptography.ICryptoTransform = DES.CreateEncryptor()
        ' Get a byte array of the string.
        Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(sIn)
        ' Transform and return the string.
        Return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))
    End Function

    Public Function DecryptTripleDES(ByVal sOut As String, ByVal sKey As String) As String
        Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider

        Dim hashMD5 As New System.Security.Cryptography.MD5CryptoServiceProvider

        ' scramble the key
        sKey = ScrambleKey(sKey)
        ' Compute the MD5 hash.
        DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey))
        ' Set the cipher mode.
        DES.Mode = System.Security.Cryptography.CipherMode.ECB
        ' Create the decryptor.
        Dim DESDecrypt As System.Security.Cryptography.ICryptoTransform = DES.CreateDecryptor()

        Dim Buffer As Byte() = Convert.FromBase64String(sOut)
        ' Transform and return the string.
        Return System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))
    End Function

    Private Function ScrambleKey(ByVal v_strKey As String) As String

        Dim sbKey As New System.Text.StringBuilder
        Dim intPtr As Integer
        For intPtr = 1 To v_strKey.Length
            Dim intIn As Integer = v_strKey.Length - intPtr + 1
            sbKey.Append(Mid(v_strKey, intIn, 1))
        Next

        Dim strKey As String = sbKey.ToString

        Return sbKey.ToString

    End Function

End Module
using System;
using System.Text;
using Microsoft.VisualBasic; // Install-Package Microsoft.VisualBasic

public static partial class modEncryption
{

    // TAKEN FROM MS KB Q317535

    public static string EncryptTripleDES(string sIn, string sKey)
    {
        var DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
        var hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

        // scramble the key
        sKey = ScrambleKey(sKey);
        // Compute the MD5 hash.
        DES.Key = hashMD5.ComputeHash(Encoding.ASCII.GetBytes(sKey));
        // Set the cipher mode.
        DES.Mode = System.Security.Cryptography.CipherMode.ECB;
        // Create the encryptor.
        var DESEncrypt = DES.CreateEncryptor();
        // Get a byte array of the string.
        var Buffer = Encoding.ASCII.GetBytes(sIn);
        // Transform and return the string.
        return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
    }

    public static string DecryptTripleDES(string sOut, string sKey)
    {
        var DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
        var hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

        // scramble the key
        sKey = ScrambleKey(sKey);
        // Compute the MD5 hash.
        DES.Key = hashMD5.ComputeHash(Encoding.ASCII.GetBytes(sKey));
        // Set the cipher mode.
        DES.Mode = System.Security.Cryptography.CipherMode.ECB;
        // Create the decryptor.
        var DESDecrypt = DES.CreateDecryptor();
        var Buffer = Convert.FromBase64String(sOut);
        // Transform and return the string.
        return Encoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
    }

    private static string ScrambleKey(string v_strKey)
    {
        var sbKey = new StringBuilder();
        int intPtr;
        var loopTo = v_strKey.Length;
        for (intPtr = 1; intPtr <= loopTo; intPtr++)
        {
            int intIn = v_strKey.Length - intPtr + 1;
            sbKey.Append(Strings.Mid(v_strKey, intIn, 1));
        }

        string strKey = sbKey.ToString();
        return sbKey.ToString();
    }
}

c# vb

Leave a Reply

Your email address will not be published.