New(new)

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?
VBdotnet2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PockyMasterCommented:
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.

String.Format(...)

ArrayList got a few Shared Methods as well.

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

e.g.

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

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

'wrong
s1.Copy("s2")
MessageBox.Show(s1)

'right
s1 = String.Copy("s2")

MessageBox.Show(s1)

Hope its more clear now...
0
VBdotnet2005Author Commented:
'But there are also methods, referred to as 'Shared' in VB.NET.

Does shared  method always use in Class?

Private Shared mintCount As Integer  

or


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

    End Function
0
VBdotnet2005Author Commented:
...so that we don't need to instanciate it
0
PockyMasterCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.