When to use "new" and when not to use "new"?

John Wen

Member
Joined
Jul 3, 2008
Messages
8
Programming Experience
1-3
Hello

I want to know when and in what context should I use "new"? It is because I saw some source codes that sometimes had "new" and sometimes did not have "new".



for example : dim myfrm as form1 (why I have no "new" in this case?)

for example : dim myconn as new ADODB.Connection (why I have "new" in this case?)

Thank you in advance.



Best regards

John Wen

3 - 7 - 08
 
usually in oop (specially in vb.net) we use the keyword New to create a new instance of a specific class.

Dim oObject as someClass ' is a declaration
Dim oObject as New someClass(someParameter) 'is creating object

'you can also do after declaration
oOject = New someClass(someParameter) 'create an object of someClass


for further knowledge about OOP, browse for some useful websites that explain about this matter concisely

thanks
 
New Keyword

Every class that u declare has to be initialized. This will allow you to access some of its properties. Classes are instantiated using Objects.

An Object is an instance of a class, basically it means that object is a copy of a class.

One class can have multiple objects....

Now how do u declare a object of a class?
Say for example you want to declare object MyObject of a class MyClass:
Its done like this....

dim MyObject as MyClass

This will declare MyObject as a type of class of MyClass.

But to instantiate the object in order to use it, you have to initialize it. Thats done using the 'New' keyword.

The 'New' Keyword will create a New instance of the class MyClass using the object MyObject.

Thats done like this: Set MyObject = New MyClass.

But Vb.Net allows u 2 declare and initialize on the same line i.e.
dim MyObject as New MyClass.....

So hope its clear now.

if you have any other queries then do ask..:)
 
I am not asking about the meaning of "new"!

Hello

In my case it is a must to add "new" for
"dim myconn as new ADODB.Connection"

However, in my other case, it is not a must to add "new" for
"dim myfrm as Form1"

Both ADODB.Connection and Form1 are of reference , but my true question is : in case 1 it needs "new", but in case 2 it does not have "new". I think it should be very weird because both cases are of class library. Why there is such a difference in my specific case?

My logic is if ADODB.Connection must have "new", then Form1 also must have "new". You cannot simply sometimes have "new" sometimes do not have "new" for the reference.

Do you know my true puzzle?

John
6 - July - 08
 
Regarding dim myfrm as Form1 you are here only declaring a variable and setting the type, this code does not create a new object. To create a new object you have to use the New keyword. Sometimes you are not creating the object yourself, but instead perhaps call a function that returns an object, in those cases you only declare the variable and type and assign it the return value of the function call. Examples:
VB.NET:
Dim frm As New Form1
Dim frm As Form1 = New Form1 '(same as first really)
Dim frm As Form1 = functionThatConfiguresAndReturnANewForm1()
 
Maybe this might help....

Well, firstly i dont undrstnd how u r getting ADODB.Connection in Vb.Net?


Bcoz as far as i know ADODB connection is used in VB6. Please correct me here if i m wrong...

Next sometimes a class need not be initialized if the method(s) within the class is declared as 'Shared' (Static).

As opposed to shared methods, Instance Methods Compulsorily require that class within which they are declared is instantiated b4 their use.....

So i would assume u are not asked to compulsorily initialize your form class bcoz the methods in form class are shared in nature, whereas methods in ADODB are not shared...
 
I don't see how that is relevant as you don't declare variables at all to use shared methods of a class :rolleyes:
 
Back
Top