Visual Basic 打字和转换问题
我是 Visual Basic 开发新手,但在 C#/C/C++/JAVA/Haskell 等方面有丰富的经验。
我维护代码并查找类型声明看起来很懒的代码示例是否有某种原因?我见过无数的字符串,它们只用作字符串类型,声明为对象。在 VB.NET 面向对象的功能出现之前,是否有必要确保方法可以采用多种类型?为什么有人会这样做?
I am new to developing with Visual Basic, but have lots of experience in C#/C/C++/JAVA/Haskell and a few others.
Is there some reason I am maintaining code and finding code examples where the type declaration just seems lazy? I have seen countless Strings, that are only ever used as the String type, declared as an object. Before the object oriented features of VB.NET came in, was this necessary to assure that methods could take in multiple types? Why would anyone do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据猜测...懒惰和旧的 VB6 习惯正在被继承。
At a guess...laziness and old VB6 habits being carried forward.
VB6 中存在可以采用多种类型的变体类型,因此需要谨慎使用。当 .Net 出现时,唯一的转换(除了大量更改代码之外)将使用对象作为类型。
如果代码似乎需要像您所描述的接受多种类型的方法那样的灵活性,我肯定会将其称为 VB6 的宿醉效果或代码转换效果。
然而,如果您看到声明为对象的字符串类型的代码,这比 VB6 的懒惰和后遗症要糟糕得多!这听起来像是非常糟糕的设计和缺乏照顾。
In VB6 there was the variant type that could take in various types and it was meant to be used with caution and care. When .Net came out the only conversion for that (besides changing your code a lot) would to have used object as the type.
I would definitely call it a hangover effect or code conversion effect from VB6 if the code appears to need that flexibility as you described for a method to take in multiple types.
However, if you are seeing code for string types that were declared as objects this is far worse than just laziness and a hangover from VB6! It sounds like horribly poor design and a lack of care.
一些 VB6 专家,例如“Mandelbrot Set”,建议使用 所有变量的变体。你可以称之为早期鸭子打字吗?!这是相当有争议的——我们许多人认为这是灾难的根源。
但这样的代码肯定有很多。在现代转换工具出现之前,将其升级到VB.NET将会非常困难。 Visual Studio 升级工具每次看到变体时都会出现恐慌。如果您确实需要用有限的资源进行升级,我想将所有变体更改为对象可能是最不糟糕的选择。显然,生成的代码会很糟糕。
你可以逐渐重构。当您处理代码区域时,您可以将声明更改为更具体的类型。
Some VB6 experts, for instance "the Mandelbrot Set", advised using Variants for all variables. You could call it early duck typing?! This was rather controversial - many of us thought it was a recipe for disaster.
But there must be a lot of code like this out there. Before modern conversion tools appeared, upgrading it to VB.NET would be very hard. The Visual Studio upgrade tool simply panics every time it sees a Variant. If you really, really needed to upgrade with limited resources, I suppose changing all variants to Object might be the least-bad option. Obviously the resulting code would be horrible.
You could gradually refactor. When you work on an area of code, you could change the declarations to a more specific type.