Posted on 2006-04-09
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
    LVL 14

    Expert Comment

    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

    '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

    by:VBdotnet2005 that we don't need to instanciate it
    LVL 14

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now