473,395 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

New Keyword

Is there any difference between these 3 ways of using the
NEW keyword? Which is prefered?

Dim xlApp As New Excel.Application

Dim xlApp As Excel.Application = New Excel.Application

Dim xlApp As Excel.Application
xlApp = New Excel.Application
Jul 21 '05 #1
5 3800
Jeff,
Is there any difference between these 3 ways of using the
NEW keyword?
No

Which is prefered?


I prefer the first one, simply because it's less typing.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 21 '05 #2
Jeff,

There's no difference between the three examples you gave, but the third
example can lead to more efficient code in the right situations.... Let me
explain:

Say you have an object which takes a long time to create an instance of
(MyReallySlowObject). If you are not 100% sure you are going to use this
object, then you can save this time by only creating it when it is needed.

For example:

Test 1: Wastes time by creating object needlessly if filename is empty
~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Function Test1(Filename as String) as Boolean

Dim objTest as new MyReallySlowObject

' Validate Filename
if Filename.Length = 0 then

' Empty Filename not allowed
return false

end if

' If we get here, do stuff
return objTest.DoStuff(Filename)

End Function

Test 2: Saves time when filename is empty
~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Function Test1(Filename as String) as Boolean

Dim objTest as MyReallySlowObject

' Validate Filename
if Filename.Length = 0 then

' Empty Filename not allowed
return false

end if

' If we get here, do stuff
objTest = New MyReallySlowObject
return objTest.DoStuff(Filename)

End Function
HTH,

Trev.
"Jeff Grundy" <je********@nospam.com> wrote in message
news:19****************************@phx.gbl...
Is there any difference between these 3 ways of using the
NEW keyword? Which is prefered?

Dim xlApp As New Excel.Application

Dim xlApp As Excel.Application = New Excel.Application

Dim xlApp As Excel.Application
xlApp = New Excel.Application

Jul 21 '05 #3
Mattias Sjögren <ma********************@mvps.org> wrote in news:
#u**************@TK2MSFTNGP10.phx.gbl:
Is New a keyword or is it just the name of the constructor of a VB Class?

Just wondering.

Chris
Jul 21 '05 #4
> Is New a keyword or is it just the name of the constructor of a VB Class?

Both.

The "New" keyword is used to instantiate an instance of a class and call its
constructor. E.g.:

Dim objTest as New MyClass()
The "New" method is the name of the constructor of the class. E.g.

Public Class MyClass

Public Sub New()

' Do initialization stuff

End Sub

End Class

The new method can be overloaded with different parameters. It can also be
marked with Public, Private or friend scope to control where an instance can
be created from.

HTH,

Trev.

"Chris Dunaway" <du******@lunchmeatsbcglobal.net> wrote in message
news:Xn**********************************@207.46.2 48.16...
Mattias Sjögren <ma********************@mvps.org> wrote in news:
#u**************@TK2MSFTNGP10.phx.gbl:
Is New a keyword or is it just the name of the constructor of a VB Class?

Just wondering.

Chris

Jul 21 '05 #5
You may find that in some circumstances declaring it separately from
instantiating it (New) can be useful. Especially when the constructor takes
arguments that won't be known until later in the code.

Take, for example a streamreader class. You know that you need one, but you
want to verify that the user has supplied a path before you create it.

If you declare the object within the IF statement, it will have "Block
Level" scope and only be available within the IF statement. By declaring it
outside the block, you can use it anywhere else in the procedure.
Dim x as System.IO.StreamReader

If txtFilePath.text <> "" then
x = new System.IO.StreamReader(txtFilePath.text)
End If
"Jeff Grundy" <je********@nospam.com> wrote in message
news:19****************************@phx.gbl...
Is there any difference between these 3 ways of using the
NEW keyword? Which is prefered?

Dim xlApp As New Excel.Application

Dim xlApp As Excel.Application = New Excel.Application

Dim xlApp As Excel.Application
xlApp = New Excel.Application

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
4
by: tzellman | last post by:
Ok, so here is my situation: Let's assume I have a function that makes good use of the kwargs parameter. It requires that there is a certain "format" for the kwargs keywords. (I am using Django,...
33
by: Snis Pilbor | last post by:
With the "as if" rule in play, doesn't that effectively render the "register" keyword completely useless? Example: I make a silly compiler which creates code that goes out of its way to take a...
6
by: tom | last post by:
Hi I try to check whether a given input is keyword or not. However this script won't identify keyword input as a keyword. How should I modify it to make it work? #!usr/bin/env python import...
4
by: Pranjal9880 | last post by:
Hi all I am trying to parse the xml file using perl in which I am succeeded , I am able to fetch the data from the xml file by using one keyword. Now I want to do it using more than one keyword. It...
2
by: rlemusic | last post by:
Hi everybody, I’m creating a database in Access (I believe it’s 2000) to catalogue items in the archives of a small museum. I’m a total n00b as far as using Access goes, but by looking at some...
3
by: Redbeard | last post by:
Hi All this is my first time post, be gentle. I am looking at creating a keyword search that searches multiple fields in a Form and then filters records that match the keyword. The Form...
10
by: Armando Serrano Lombillo | last post by:
Why does Python give an error when I try to do this: Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> len(object=) TypeError: len() takes no keyword arguments but...
1
adelemb
by: adelemb | last post by:
Hi, I'm trying to make a SQL statement work and am getting quite mixed up with it, I hope someone can help! I have a form with a textbox named "keyword". I want the user to enter a keyword and...
1
by: alamodgal | last post by:
hiiiiiii I have a problem in highlighting searching keyword.Actually im using this function for searching Public Function HighLight(ByVal Keyword As String, ByVal ContentFor As String) Dim...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.