4

I am a C# programmer (hobby), but I want to convert to a VB.NET programmer. I have seen many posts written in both C# and VB.NET, but I need some links which explain from the basics (like void main void) to the most advanced.

Note: Microsoft blogs (until now whatever I read) do not refer to the basic core level knowledge/things.

drew010
  • 68,777
  • 11
  • 134
  • 162
user287745
  • 3,071
  • 10
  • 56
  • 99
  • 11
    Why in the world would you like to convert **from** C# **to** VB.NET. That's the wrong way mister ;) – Øyvind Bråthen Jan 25 '11 at 07:01
  • @Øyvind Knobloch-Bråthen: hope you didn't just start a flame war. Allthough I would generally agree ;) – froeschli Jan 25 '11 at 07:04
  • 2
    There are many reasons to convert from C# to VB. From insanity to just mild craziness. Also don't forget that some businesses might require you to know both C# and VB.NET, so that you know good from bad. – Gleno Jan 25 '11 at 07:08
  • 2
    @Øyvind Knobloch-Bråthen: I hope you know what you're trying to start; a flame war! If you think C# is better than VB.NET, I will also argue that C++ is better than C#. Just in case you didn't know, the various .NET languages (C++, C#, F#, VB.NET, etc) are just wrappers for the .NET framework. Mind you **any language is good as its user**. If you think C# is better, I'm sorry but you're mistaken. – Alex Essilfie Jan 25 '11 at 08:29
  • I use conversion tools (like [this one](http://www.developerfusion.com/tools/convert/csharp-to-vb/)) extensively. As many of the answers already point out, the difference is almost entirely syntactical. If you know the .NET Framework, it's only a matter of learning the syntax nuances that differ between the languages. – Cody Gray - on strike Jan 25 '11 at 08:42
  • 1
    @Alex Essilfie - I hoped the smiley at the end would convey that I didn't mean it to seriously. For me, C# is the best language around, but this is a personal preference and will not be true for everyone (although it should :)). No flame war indended, but since this is a *religious* comment it usually ends up in a war anyway. As everything related to religion in any form usually does. – Øyvind Bråthen Jan 25 '11 at 09:02
  • This question is very similar to [this one](http://stackoverflow.com/questions/1337253/converting-c-knowledge-to-vb-net-any-potential-problems). – MarkJ Jan 25 '11 at 12:31
  • @Øyvind If you don't intend to start a flame war, why have you just brought religion into it!? Can I suggest you and Alex move this discussion elsewhere. – MarkJ Jan 25 '11 at 12:32

7 Answers7

6

Aside from books/blogs, a good way to learn the other side of the C#/VB wall is to write some code in C#, compile it, and open the DLL in Reflector and view as VB code. This will allow you to answer your own questions about VB.NET.

For example, suppose you want to see how to do generics in VB.NET. You write a simple library in C#:

void SomeMethod()
{
    List<int> list = new List<int>();
}

Compile this, then open in Reflector, and it will show you:

Sub SomeMethod
    Dim list as List(Of Integer) = New List(Of Integer)
End Sub

or something like that...

If you know how to do it in C#, you can probably teach yourself how to do it in VB.NET this way easier than looking for samples online.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • This is OK up to a point, but there are some things that are best done differently in Vb.Net. E.g. the [declarative approach to events](http://stackoverflow.com/questions/1488573/how-to-raise-event-using-addhandler/1488990#1488990). IMHO it's better to read up the differences, otherwise you'll be trying to code VB.Net as if it were C# & get frustrated. – MarkJ Jan 25 '11 at 12:30
5

C# and VB.NET are just syntactic sugar on top of the .NET Framework. .NET Framework APIs are the same for both, and there are some features which are available to one and not to the other.

One thing you will find yourself is putting ";" after every statement when you switch from C# to VB.NET (which is illegal in VB.NET).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pradeep
  • 3,258
  • 1
  • 23
  • 36
  • 3
    Agreed on the semi-colon thing - the first VB.NET project I wrote after years of C#, I was constantly doing that. – Joe Enos Jan 25 '11 at 07:11
  • 1
    And I was constantly forgetting the semi colon when I worked on my first C# project. I code in C# now but I prefer VB.NET; it's my native language. – Alex Essilfie Jan 25 '11 at 08:24
  • Nowadays you will find Visual Studio automatically removing the semicolons for you – MarkJ Jan 25 '11 at 12:24
4

The thing about Visual Basic .NET is that it has a huge history in pre-.NET versions. This has caused some odd constructions and keywords that are not entirely logical from a C# point of view but do make sense when you have a background in VB.

E.g., And, AndAlso, Or, OrElse. Look them up and find out that it is all about keeping the VB6 programmers happy.

Get a good book if you really want/need to do this. This is bigger than a single question on this forum.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • What's illogical about `AndAlso` vs `And`? C# has both a logical AND (`&`) and a conditional AND (`&&`). Same for the `OrElse`/`Or` duo. – Cody Gray - on strike Jan 25 '11 at 08:38
  • VB6 And translates to the VB.NET And but does not translate to the conditional C# &&. The C# && does short-circuit evaluation; if the left operand evaluates to false, the right operand is not evaluated. The VB AndAlso matches this behaviour. From a .NET point of view it would have been more logical to match the VB And to the C# && (as it did in the first CTP in 2000). – Emond Jan 25 '11 at 08:55
  • @Erno: If that was done, then what would VB.NET use for a logical AND operator? I'm familiar with VB 6's behavior; I'm asking in the context of VB.NET. It simply doesn't seem that `AndAlso` matching `&&` and `And` matching `&` is any less logical than the alternatives. Your explanation confirm that the only reason it's strange to you is that VB.NET's `And` operator retains the ability of its VB 6 cousin to serve dual-purposes. But I could certainly think of cases where I could evaluate conditionals with a logical AND in C#. That doesn't make C# a backwards language. – Cody Gray - on strike Jan 25 '11 at 09:41
  • Don't get me wrong. I am not calling VB nor C# backward. All I was trying to show was that a C# programmer transitioning to VB.NET is likely to run into some less than obvious 'features'. Nearly all VB.NET code in examples on the web uses And. I wouldn't be surprised if a C# programmer mistakes the And for a &&. That's all. – Emond Jan 25 '11 at 09:49
  • `AndAlso` and `OrElse` are about backward compatibility for porting **code**. Not just to keep **programmers** happy. – MarkJ Jan 25 '11 at 12:34
  • Backward compatibility makes programmers happy. There is no reason why VB.NET **HAS** to have AndAlso and OrElse. This track of comments is getting off-topic. From here on I will on respond to these comments if they are about the topic. There are enough language-flame-wars on the web. – Emond Jan 25 '11 at 12:46
  • BTW, although short-circuiting is usually only applied to true/false "and/or" operands, I would think it could also apply to numeric ones. If the first operand to a numeric "and" is zero, the result will be zero. If the first operand to a numeric "or" is either -1 or the max unsigned value of the larger operand size, that's also going to be the result. – supercat Jan 25 '11 at 23:54
2

There were some useful articles in Visual Studio magazine back in Jan 2008.

You might also be interested in the questions "what's allowed in VB that is prohibited in C#" and "Converting C# knowledge to VB.Net"

Community
  • 1
  • 1
MarkJ
  • 30,070
  • 5
  • 68
  • 111
1

MS blog wouldn't talk about basic stuff, unless they're something newly released or in progress. I suggests the MSDN instead http://msdn.microsoft.com/en-us/library/2x7h1hfk.aspx

Martheen
  • 5,198
  • 4
  • 32
  • 55
1

I found the The Code Project article Complete Comparison for VB.NET and C# which does an in-depth comparison of VB.NET and C# on for language version 2005. I hope this helps.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AbrahamJP
  • 3,425
  • 7
  • 30
  • 39
1

Might I also recommend the VB Language Spec

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48