不建议在 VB.Net 中使用 Mid()、Instr()、LBound()、UBound() 等吗? [英] Is use of Mid(), Instr(), LBound(), UBound() etc. in VB.Net not recommended?

查看:29
本文介绍了不建议在 VB.Net 中使用 Mid()、Instr()、LBound()、UBound() 等吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自 C# 背景,但现在主要使用 VB.Net.在我看来,上述功能(和其他功能 - 例如 UCase、LCase)等是 VB6 及之前的遗留物.在 VB.Net 中使用这些功能是不受欢迎的,还是纯粹归结为个人喜好?

I come from a C# background but am now working mostly with VB.Net. It seems to me that the above functions (and others - eg. UCase, LCase) etc. are carryovers from VB6 and before. Is the use of these functions frowned upon in VB.Net, or does it purely come down to personal preference?

我个人的偏好是远离他们,但我想知道这是否只是我的 C# 偏见.

My personal preference is to stay well away from them, but I'm wondering if that is just my C# prejudice.

我遇到了几个问题 - 特别是从 VB6 转换为 VB.Net 的代码,其中集合的 0 索引意味着代码中引入了错误,因此我很警惕.

I've come across a couple of issues - particularly with code converted from VB6 to VB.Net, where the 0 indexing of collections has meant that bugs have been introduced into code, and am therefore wary of them.

推荐答案

出现这些函数的首要原因当然是它们是 VB 语言的一部分,继承自 VB 6.

The reason that those functions are there in the first place is of course that they are part of the VB language, inherited from VB 6.

然而,它们不仅仅是框架中方法的包装器,其中一些具有一些额外的逻辑,使它们在某些方面有所不同.例如,Mid 函数允许您指定字符串之外的范围,它会静默地缩小范围并返回剩余的字符串部分.如果您指定字符串之外的范围,String.Substring 方法会抛出异常.

However, they are not just wrappers for methods in the framework, some of them have some additional logic that makes them different in some ways. The Mid function for example allows that you specify a range that is outside the string, and it will silently reduce the range and return the part of the string that remains. The String.Substring method instead throws an exception if you specify a range outside the string.

因此,函数不仅仅是包装器,它们代表了一种更符合 Visual Basic 的不同编程方法,您可以在函数中抛出任何内容,并且几乎总能得到一些内容.在某些方面这更容易,因为您不必考虑所有特殊情况,但另一方面,当您向函数提供不合理的内容时,您可能希望获得异常而不是获得结果.在调试时,如果您尽早获得异常,而不是试图追溯错误值的来源,通常会更容易.

So, the functions are not just wrappers, they represent a different approach to programming that is more in line with Visual Basic, where you can throw just about anything at a function and almost always get something out. In some ways that is easier, as you don't have to think about all the special cases, but on the other hand you might want to get an exception instead of getting a result when you feed something unreasonable to a function. When debugging, it's often easier if you get the exception as early as possible instead of trying to trace back where a faulty value comes from.

这篇关于不建议在 VB.Net 中使用 Mid()、Instr()、LBound()、UBound() 等吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆