在 VB.NET 中创建类方法而不是实例方法
我不确定标题是否清楚我的问题,但我试图在 Visual Basic 中创建类方法而不是实例方法,这样我就不必浪费内存和代码来创建临时对象来执行不执行的方法需要实例变量。
我不确定你是否可以在 VB 中做到这一点,但我知道你可以在 Objective-C 中通过在方法声明前面使用“+”或“-”符号来做到这一点。在 C++ 中(至少我认为,我不记得了),您将 static 关键字或 const 关键字放在函数前面。
如果可以的话,我该如何在VB中做到这一点?或者我应该创建一组不属于类成员的单独函数?
I am not sure how clear my question is by the title, but I am trying to make Class methods instead of Instance methods in Visual Basic that way I don't have to waste memory and code creating temporary objects to execute methods that don't need instance variables.
I am not sure if you can do that in VB but I know you can in Objective-C by using either a "+" or "-" sign in front of the method declaration. And in C++ (at least I think, I can't remember) you put the static keyword or const keyword in front of the function.
How would I do this in VB if it is possible? Or should I just make a separate set of functions that are not members of a class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在 VB.Net 中定义类方法,则只需向函数添加 Shared 修饰符
即可确定是否应该使用类方法而不是实例方法来避免分配。我认为你在这里使用了错误的推理模式。我会从简单地设计类开始,使其具有最自然、最直接的 API。然后,在该过程之后,如果探查器显示小对象的分配存在问题,则更新 API 以解决此问题。
在不使用探查器的情况下出于性能原因做出 API 设计决策几乎肯定会导致浪费精力。
If you are looking to define class methods in VB.Net you just need to add the
Shared
modifier to the functionAs to whether or not you should use a class method over an instance method to avoid allocations. I think you're using the wrong reasoning pattern here. I would start simply with design the class to have the most natural and straight forward API. Then after that process if a profiler shows that allocation of small objects is a problem update the API to account for this.
Making an API design decision for performance reasons without using a profiler will almost surely lead to wasted effort.
您想要在 VB.net 中创建共享方法。
You want to create a Shared method in VB.net.