从C#迁移到VB.Net

时间:2022-01-10 01:40:36

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)

因此,作为这个全球金融市场的直接结果,我明天将开始作为VB.net开发人员的新工作。到目前为止,我一直在开发C#(java,vb6,sql,tibco等等)

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?

所以问题是这个,有什么需要注意的,有没有人对编写好的vb.net代码有任何好的建议?

(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.

只是一个快速更新,公司看起来非常好,目前的代码库看起来质量非常高。我开始适应VB的做事方式(虽然不能阻止自己添加分号!)。再次感谢大家的有益建议。

8 个解决方案

#1


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.

在VB中最重要的事情,在我(绝对不谦虚)的意见中,始终使用Option Strict On(除非在每个文件的基础上,当非严格打字有意义时,例如因为你使用PIA来与MS Office互操作)并在VS选项中启用它。

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 On与Option Explicit On一起提供与C#大致相同的行为。切换后,它会在编译时删除大量类型检查,并允许在完全不相关的类型之间进行虚假,不必要和难以调试的隐式转换。

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).

使用COM API时,Option Strict Off是有意义的。 Option Explicit Off永远不会有意义。这是愚蠢的(主要是为了兼容VB6)。

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:

需要注意的另一件事是:平等与参考测试。在C#中,您使用==。在VB中,你有不同的运算符:

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):

现在,根据用户输入,EqualContent可能为True; EqualRefs将始终为False。请注意,这里语义上等同于以下C#代码(通常没有人写过):

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.

我实际上认为这是VB相对于C#的优势,但很少需要。与Is相反的是IsNot。这里要注意的另一件事是通过=运算符的字符串比较实际上调用VB运行时方法: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).

这考虑了其他几个设置,尤其是Option Compare设置,它可能是Binary(默认,C#中的行为)或Text(不区分大小写的比较)。

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.

VB运行时也在其他一些情况下被调用,其中一个特别是CType,它是VB中的通用转换运算符。我倾向于避免使用运算符,我强烈建议任何人都这样做,支持其他更明确的转换。原因是CType在应用时会尝试几种语义上非常不同的转换。这使得很难跟踪代码中究竟发生了什么,可能会引入输入错误。

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

首先,CType允许解析数字的字符串。这是一个通过NumberType.Parse操作更好地表达的概念,就像在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.

而不是CType,我建议使用DirectCast,它相当于C#cast,或TryCast,它与C#的转换相同。

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

另一个问题。检查对象x是否具有某种类型T时,必须使用以下语法:

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.

请注意,这不会调用普通的引用比较运算符Is。相反,它使用自己的运算符构造TypeOf ... Is ....但是你不能写TypeOf ...... IsNot ......这可能是规范中的一个错误。

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).

存在很多差异,一些有用(例如,Select Case语句中的差异)和一些较少(例如,基本通配符匹配的Like运算符......只是使用正则表达式)。

Some other questions relating to this:

与此相关的其他一些问题:

#2


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:

只是一个小细节作为警告。如果在VB.NET中声明一个数组,括号之间的数字表示上限,而不是元素数:

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

Output is "12".

输出为“12”。

not

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

Output is "13".

输出为“13”。


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

我建议你阅读Kathleen Dollard撰写的这篇博客文章。

What a C# Coder Should Know Before They Write VB

在编写VB之前,C#编码器应该知道什么

Her first advice is:

她的第一个建议是:

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

1)在开始之前克服尊重或戒烟。 VB.NET是一门很棒的语言。

#3


Check out this link; I believe it's pretty much the definitive guide: http://msmvps.com/blogs/kathleen/archive/2008/07/25/what-a-c-coder-should-know-before-they-write-vb-updated.aspx

看看这个链接;我相信它几乎是权威指南:http://msmvps.com/blogs/kathleen/archive/2008/07/25/what-a-c-coder-should-know-before-they-write-vb-updated.aspx

#4


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.

VB.Net是.Net语言,它与C#相同,但具有vb风格。我4年前遇到的情况和你一样,我做的只是用vb.net写作,我很害怕但是当我开始写作时我发现我很好,它和C#一样,只是在前几个你会写“;”的行和“{}”,但几分钟后,你会感觉它正常。

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

所以我的建议不要觉得你会写VB.Net,你会写.Net但新风格。

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.

确保C#和VB.Net之间的差异不仅仅是语法,但你会很快得到它们。

Also a few converters can help you coding faster:

此外,一些转换器可以帮助您更快地编码:

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

将VB.Net转换为C#,将C#转换为VB.Net Tools

#5


When I made a similar switch I found this little side by side comparison particularly useful: http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

当我进行类似的切换时,我发现这个小的并排比较特别有用:http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

#6


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.

应该给My命名空间一个特别的注意事项:它在某种程度上缺少C#命名法,所以从那里你可能会错过这个重要的工具。

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.

Visual Basic中的My命名空间公开了使您能够轻松利用.NET Framework强大功能的属性和方法。 My命名空间简化了常见的编程问题,通常将困难的任务简化为单行代码。此外,My命名空间是完全可扩展的,因此您可以自定义My的行为并将新服务添加到其层次结构以适应特定的应用程序需求。

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.

提供对信息和常用功能的访问的三个中心My对象是My.Application Object,My.Computer Object和My.User Object。您可以使用这些对象分别访问与当前应用程序,安装应用程序的计算机或应用程序的当前用户相关的信息。

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

使用My.Application,My.Computer和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)

#7


I found this reference handy when I was switching from VB to C#, I expect it would be just as helpful the other way around. So that might help with the syntactic piece of the transition you're headed through. Not the hardest part of the transition for sure, but hopefully it can help.

当我从VB切换到C#时,我发现这个参考很方便,我希望它反过来会有所帮助。这可能有助于你所经历的过渡的句法部分。肯定不是过渡中最困难的部分,但希望它可以提供帮助。

#8


+1 for option strict.

选项严格+1。

I went from VB.Net to C# and was initially a little scared I wouldn't be able to pick it up. If you know the .Net framework you will be fine, just keep Google handy for any syntax you are unsure of.

我从VB.Net转到C#,最初有点害怕,我无法拿起它。如果您了解.Net框架就可以了,只需将Google放在任何您不确定的语法上即可。

Good luck.

#1


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.

在VB中最重要的事情,在我(绝对不谦虚)的意见中,始终使用Option Strict On(除非在每个文件的基础上,当非严格打字有意义时,例如因为你使用PIA来与MS Office互操作)并在VS选项中启用它。

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 On与Option Explicit On一起提供与C#大致相同的行为。切换后,它会在编译时删除大量类型检查,并允许在完全不相关的类型之间进行虚假,不必要和难以调试的隐式转换。

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).

使用COM API时,Option Strict Off是有意义的。 Option Explicit Off永远不会有意义。这是愚蠢的(主要是为了兼容VB6)。

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:

需要注意的另一件事是:平等与参考测试。在C#中,您使用==。在VB中,你有不同的运算符:

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):

现在,根据用户输入,EqualContent可能为True; EqualRefs将始终为False。请注意,这里语义上等同于以下C#代码(通常没有人写过):

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.

我实际上认为这是VB相对于C#的优势,但很少需要。与Is相反的是IsNot。这里要注意的另一件事是通过=运算符的字符串比较实际上调用VB运行时方法: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).

这考虑了其他几个设置,尤其是Option Compare设置,它可能是Binary(默认,C#中的行为)或Text(不区分大小写的比较)。

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.

VB运行时也在其他一些情况下被调用,其中一个特别是CType,它是VB中的通用转换运算符。我倾向于避免使用运算符,我强烈建议任何人都这样做,支持其他更明确的转换。原因是CType在应用时会尝试几种语义上非常不同的转换。这使得很难跟踪代码中究竟发生了什么,可能会引入输入错误。

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

首先,CType允许解析数字的字符串。这是一个通过NumberType.Parse操作更好地表达的概念,就像在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.

而不是CType,我建议使用DirectCast,它相当于C#cast,或TryCast,它与C#的转换相同。

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

另一个问题。检查对象x是否具有某种类型T时,必须使用以下语法:

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.

请注意,这不会调用普通的引用比较运算符Is。相反,它使用自己的运算符构造TypeOf ... Is ....但是你不能写TypeOf ...... IsNot ......这可能是规范中的一个错误。

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).

存在很多差异,一些有用(例如,Select Case语句中的差异)和一些较少(例如,基本通配符匹配的Like运算符......只是使用正则表达式)。

Some other questions relating to this:

与此相关的其他一些问题:

#2


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:

只是一个小细节作为警告。如果在VB.NET中声明一个数组,括号之间的数字表示上限,而不是元素数:

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

Output is "12".

输出为“12”。

not

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

Output is "13".

输出为“13”。


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

我建议你阅读Kathleen Dollard撰写的这篇博客文章。

What a C# Coder Should Know Before They Write VB

在编写VB之前,C#编码器应该知道什么

Her first advice is:

她的第一个建议是:

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

1)在开始之前克服尊重或戒烟。 VB.NET是一门很棒的语言。

#3


Check out this link; I believe it's pretty much the definitive guide: http://msmvps.com/blogs/kathleen/archive/2008/07/25/what-a-c-coder-should-know-before-they-write-vb-updated.aspx

看看这个链接;我相信它几乎是权威指南:http://msmvps.com/blogs/kathleen/archive/2008/07/25/what-a-c-coder-should-know-before-they-write-vb-updated.aspx

#4


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.

VB.Net是.Net语言,它与C#相同,但具有vb风格。我4年前遇到的情况和你一样,我做的只是用vb.net写作,我很害怕但是当我开始写作时我发现我很好,它和C#一样,只是在前几个你会写“;”的行和“{}”,但几分钟后,你会感觉它正常。

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

所以我的建议不要觉得你会写VB.Net,你会写.Net但新风格。

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.

确保C#和VB.Net之间的差异不仅仅是语法,但你会很快得到它们。

Also a few converters can help you coding faster:

此外,一些转换器可以帮助您更快地编码:

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

将VB.Net转换为C#,将C#转换为VB.Net Tools

#5


When I made a similar switch I found this little side by side comparison particularly useful: http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

当我进行类似的切换时,我发现这个小的并排比较特别有用:http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

#6


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.

应该给My命名空间一个特别的注意事项:它在某种程度上缺少C#命名法,所以从那里你可能会错过这个重要的工具。

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.

Visual Basic中的My命名空间公开了使您能够轻松利用.NET Framework强大功能的属性和方法。 My命名空间简化了常见的编程问题,通常将困难的任务简化为单行代码。此外,My命名空间是完全可扩展的,因此您可以自定义My的行为并将新服务添加到其层次结构以适应特定的应用程序需求。

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.

提供对信息和常用功能的访问的三个中心My对象是My.Application Object,My.Computer Object和My.User Object。您可以使用这些对象分别访问与当前应用程序,安装应用程序的计算机或应用程序的当前用户相关的信息。

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

使用My.Application,My.Computer和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)

#7


I found this reference handy when I was switching from VB to C#, I expect it would be just as helpful the other way around. So that might help with the syntactic piece of the transition you're headed through. Not the hardest part of the transition for sure, but hopefully it can help.

当我从VB切换到C#时,我发现这个参考很方便,我希望它反过来会有所帮助。这可能有助于你所经历的过渡的句法部分。肯定不是过渡中最困难的部分,但希望它可以提供帮助。

#8


+1 for option strict.

选项严格+1。

I went from VB.Net to C# and was initially a little scared I wouldn't be able to pick it up. If you know the .Net framework you will be fine, just keep Google handy for any syntax you are unsure of.

我从VB.Net转到C#,最初有点害怕,我无法拿起它。如果您了解.Net框架就可以了,只需将Google放在任何您不确定的语法上即可。

Good luck.