Posted on 2006-04-09
Medium Priority
Last Modified: 2010-04-23
When we write a program, whatever it is, and then create a class, let say, "MySample". When we need to use MySample, we need to instantiated using New. If we use a class, for example, Arraylist, or any other classes, do we need to use New, always?
Question by:VBdotnet2005
  • 2
  • 2
LVL 14

Expert Comment

ID: 16414604
You need to use new in case you use an instance

Methods you will call on the instance will generally do something with your instance.

But there are also methods, referred to as 'Shared' in VB.NET.

These are methods you can call without creating an instance first, e.g.


ArrayList got a few Shared Methods as well.

To make your code readable, don't confuse yourself and call the shared methods on instances.


Dim s1 as String = "string1"
Dim s2 as String = "string2"

'working but confusing
MessageBox.Show (s1.Copy("s2"))


s1 = String.Copy("s2")


Hope its more clear now...

Author Comment

ID: 16414676
'But there are also methods, referred to as 'Shared' in VB.NET.

Does shared  method always use in Class?

Private Shared mintCount As Integer  


 Public Shared Function Speed(ByVal x As Integer) As Integer
        Return x

    End Function

Author Comment

ID: 16414683
...so that we don't need to instanciate it
LVL 14

Accepted Solution

PockyMaster earned 500 total points
ID: 16414801
Yeah, you can do like this, but that is used for methods that do not interfere with member variables in your class. You cannot access instance methods from a shared method.

Most of the time your class will contain some data as well, and in that case, you will probably choose to use the non-shared methods.

Shared you will use if you got some kind of functionality that belongs to a class but does not interfere with the class itself.

Think like:
Shared can use in e.g. a math class (Generally speaking)
Non-shared e.g. in a collection class

This is not ALWAYS the case, but to give you a pointer what to think about

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question