Tag Archives: VB

C# Left & Right function.

I used to be a Visual Basic programmer up to a few months ago. One of the things that has proven most usefull in the past few years is string manipulation. The well-known "Right(string, integer)" & "Left(string, integer)" functions. I'm sure that any VB.Net programmer would say that these things are pretty basic and I'm sure no decent VB.Net programmer doesn't know these, or hasen't used these yet.

Now in C# I encountered a problem, it doesn't actually know these functions. But since C# is more powerfull than VB.net saying that there's a work around is like saying there's an elephant in the room (English expression:: meaning it's obvious).

Let's take a look at how this works out in Visual Basic first.


And when we show this in action.


This is a fairly easy task in Visual Basic. All what you need is built-in in the language. In C# this wasn't the case.

Let's open up a C# commandline application and try to enter similar code.


So C# has no rights? (hilarious pun, I know).

Lucky for us, the workaround is rather easy. let's add some code to our project.



There we go, now C# knows Left and Right. When you use it it will refer to this code and return the result. It's not hard to get the result at all as you can see.

Now let's write our code again in C#


As you see, the Left and Right functions don't thow an error anymore. This is because our program now knows what to do with these, as we've decleared this earlier.

Let's run this code.


Et Voila, as the french would say.
Now we've got a perfectly functioning Right & Left function in C#.

You can download the project files here: http://code.msdn.microsoft.com/C-Left-Right-function-290f2906

C# & VB.Net

So over the past few weeks, I’ve been active on the MSDN forums for quite a bit. And there was one discussion that kept popping up every x days, the big question and source of discussion to lots of developers. Is C# better than VB.net, or isn’t it?

Truth is, there are quite a bit of difference but when you break the languages down to their core, they are the same. Picture this as being 2 cars. C# is a 2-door green car, and VB is a 4-door blue car. These 2 don’t look alike whatsoever, but inside they have the same features. Both have a steering wheel, radio, SatNav and whatnot. Think about this for a second, and read on to break down 2 of the most important languages there are.

If you got the picture, you will realise that what I’m trying to explain, is that the two languages don’t look alike on the surface, but inside they are the same.
Let’s cover the most obvious difference, as silly as it might sound. The whole layout of VB is blue, and the whole layout of C# is green. Not coincidentally the colours of the cars I used to illistrate this matter.

Once you have launched your editor, you can start programming. Here is where we encounter our second difference, and probably the most important one to notice.

The whole syntax of the  2 languages are like apples and pears.

As you can tell by these little snippets, the code is equally readable. But C# closes and opens a scope using the curly braces ( {} ).  Whereas in VB it opens and closes using code ( Sub & End Sub / Module & End Module). C# also indicated changed & saved code snippets. Using the green line at the right of the code explorer.

Furthermore there are differences in the language, for example: C# is case-sensitive whearas VB.Net is not.. Another difference is the Keywords used to refer to the underlying datatypes. And C# does support some datatypes which Visual Basic does not.

Visual Basic .NET Visual C# .NET .NET Framework
Boolean bool System.Boolean
Byte byte System.Byte
Short short System.Int16
Integer int System.Int32
Long long System.Int64
Single float System.Single
Double double System.Double
Decimal decimal System.Decimal
Date System.DateTime System.DateTime
String string System.String
Char char System.Char
Object object System.Object
n/a sbyte System.Sbyte
n/a ushort System.UInt16
n/a uint System.UInt32
n/a ulong System.UInt64


But let’s look at this code we have written earlier in this article, by using ILDASM, so we can break down the code to the Intermediate Language.

In essence they do look the same. So ILDASM shows us what’s inside our car (.net language).

And yes, it’s given that C# has some small things VB.net seems to lack at the moment I’m writing this, as shown in the table above. But in a few years, both of these languages look alot more alike, at microsoft they strive to make the languages an equal to eachother.

So why the discussions? What makes the difference?
One simple word: Taste. What makes people like the green car over the blue car? Taste, preference, and in some cases their habit.
I started out programming in VB.net, and I have loved the language (in a pure platonic way ofcourse) as long as I’ve used it. Recently I switched to C#, the only reason being that I like the syntax more.

So to wrap it up: C# shows small differences with VB.Net, but both are equally powerfull languages because they both use the CLR. So it’s really just a matter of preference.

(If you want a full list of the syntax differences you can look at the white paper published by Microsoft)