Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving from C# to VB.Net [closed]

Tags:

c#

vb.net

So as a direct result of this global financial hoohar I'm going to start a new job as a VB.net developer tomorrow. Up to this point I've been developing in C# (bit of java, vb6, sql, tibco, etc. here and there)

So the question is this, what are the gotchas to look out for and does anyone have any good advice on writing good vb.net code?

(Any other advice on coping with a salary / prospects drop welcome but not essential ;-))


Just a quick update, company seems really good, current code base appears to be of a very high quality. Am starting to adjust to the VB way of doing things (can’t stop myself adding semicolons everywhere though!). Thanks again for the helpful advice everyone.

like image 981
Wilfred Knievel Avatar asked Mar 08 '09 15:03

Wilfred Knievel


People also ask

Is it hard to move from C to C++?

Switching from C to C++ can be both easy, as there are many similarities between the two languages, and hard, as there are many differences that require forgetting what you know and habits that you may have developed from programming in C.

Is learning C worth it in 2022?

C might be old, but it is definitely relevant in 2022 and will likely remain so. The simplicity of C provides you with a perfect gateway into the programming world. It helps you understand the detailed implementation of any algorithm.

Will C be used in the future?

Later, the C programming language was used to develop Microsoft Windows and a variety of Android applications. In future C can be used to make better operating systems for more user-friendly apps.


4 Answers

Option Strict

The most important thing to do in VB, in my (absolutely not humble) opinion is to use Option Strict On at all times (except, on a per-file basis, when non-strict typing makes sense, e.g. because you use PIA to interoperate with MS Office) and to enable it in the VS options.

Option Strict On, together with Option Explicit On, gives roughly the same behaviour as C#. Switched off, it removes a lot of type checks at compile-time and allows spurious, unnecessary and hard-to-debug implicit conversions between completely unrelated types.

Option Strict Off makes sense when working with COM API. Option Explicit Off never makes sense. It's stupid (and mainly there for VB6 compatibility).

Comparison: = versus Is

Another thing to look out for: equality vs. reference testing. In C#, you use == for both. In VB, you've got distinct operators:

Dim StringA = "Hello"
Dim StringB = Console.ReadLine()
Dim EqualContent = StringA = StringB
Dim EqualRefs = StringA Is StringB

Now depending on the user input, EqualContent may be True; EqualRefs will always be False. Beware that Is here is semantically equivalent to the following C# code (which nobody ever writes, usually):

var equalRefs = object.ReferenceEquals(stringA, stringB);

I actually think this is an advantage in VB over C#, but one rarely needed. The opposite of Is is IsNot. Another thing to pay attention to here is that the string comparison via the = operator actually calls a VB runtime method: Microsoft.VisualBasic.CompilerServices.Operators.CompareString.

This takes into account several other settings, especially the Option Compare setting which may be Binary (default, behaviour like in C#) or Text (case-insensitive comparison).

CType versus DirectCast and TryCast

The VB runtime is called in some other cases as well, one of them notably CType which is a general-purpose conversion operator in VB. I tend to avoid using the operator and I strongly advise anyone doing the same, in favour of other, more explicit conversions. The reasons for this is that CType tries several semantically very different conversions, when applied. This makes it hard to track what exactly is going on in the code, potentially introducing typing errors.

For one thing, CType allows parsing of strings for numbers. This is a concept better expressed through the NumberType.Parse operation, as in C#.

Instead of CType, I advise usage of DirectCast which is the equivalent of the C# cast, or TryCast which is the same as C#'s as conversion.

Another gotcha. When checking whether an object x has a certain type T, the following syntax has to be used:

If TypeOf x Is T Then …

Notice that this doesn't invoke the normal reference comparison operator Is. Rather, it uses an own operator construct TypeOf … Is …. You cannot write TypeOf … IsNot …, though. This is probably a bug in the specs.

Misc. …

There are a lot more differences, some useful (e.g. the differences in the Select Case statement) and some less (e.g. the Like operator for basic wildcard matching … just use regular expressions instead).

Some other questions relating to this:

  • What are the most important functional differences between C# and VB.NET?
  • What are the differences between C#.net and Visual Basic.net?
like image 113
Konrad Rudolph Avatar answered Nov 19 '22 01:11

Konrad Rudolph


Just one little detail as caveat. If you declare an array in VB.NET, the number between the brackets means the upper limit, not the count of elements:

Dim monthNames(11) As String
Console.Write(monthNames.Count)

Output is "12".

not

Dim monthNames(12) as String
Console.Write(monthNames.Count)

Output is "13".


And I recommend you read this blog post written by Kathleen Dollard.

What a C# Coder Should Know Before They Write VB

Her first advice is:

1) Get over the respect thing or quit before you start. VB.NET is a great language.

like image 38
splattne Avatar answered Nov 19 '22 01:11

splattne


VB.Net is .Net language, its the same as C# but with vb flavor. I faced the same case as you from 4 years ago, what i did is just writing with vb.net, i was afraid but when i started writing i found that i am very good, its the same as C#, just in the first few lines you will write ";" and "{}" but after few minutes, you will feel its normal.

So my advice don't feel that you will write VB.Net, you will write .Net but with new style.

May be you will need to know the very easy parts: How to write a function How to Declare a variable How to create an event

And sure there are more differences between C# and VB.Net than just the syntax, but you will got them very fast your self.

Also a few converters can help you coding faster:

Convert VB.Net to C#, C# to VB.Net Tools

like image 3
Amr Elgarhy Avatar answered Nov 19 '22 02:11

Amr Elgarhy


A special notice should be given to the My namespace: It is somehow missing from the C# nomenclature, so coming from there you might miss this important tool.

The My namespace in Visual Basic exposes properties and methods that enable you to easily take advantage of the power of the .NET Framework. The My namespace simplifies common programming problems, often reducing a difficult task to a single line of code. Additionally, the My namespace is fully extensible so that you can customize the behavior of My and add new services to its hierarchy to adapt to specific application needs.

The three central My objects that provide access to information and commonly used functionality are My.Application Object, My.Computer Object, and My.User Object. You can use these objects to access information that is related to the current application, the computer that the application is installed on, or the current user of the application, respectively.

An example from Performing Tasks with My.Application, My.Computer, and My.User:

' Displays a message box that shows the full command line for the
' application.
Dim args As String = ""
For Each arg As String In My.Application.CommandLineArgs
    args &= arg & " "
Next
MsgBox(args)
like image 2
gimel Avatar answered Nov 19 '22 03:11

gimel