2

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.