开发者

Making Class methods instead of Instance Methods in VB.NET

开发者 https://www.devze.com 2022-12-24 08:11 出处:网络
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 temporar

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 r开发者_Go百科emember) 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?


If you are looking to define class methods in VB.Net you just need to add the Shared modifier to the function

Class C1
  Public Shared Function DoSomething() As String 
    ' Insert code here
  End Function
End Class

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


You want to create a Shared method in VB.net.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号