473,799 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When to use 'New' key word?

Hello,
I have a basic question regarding 'New' keyword.

For instance I can declare a variable as string (class)

as Dim myName as String = String.Empty
or Dim I as Integer
But when I want declare a button we instantiate it as
Dim b as Button = New Button

I am just trying to understand when (for what kind of classes) to use
New key word.

Please some one help to clarify this.
Thanks
-L

Mar 29 '06 #1
4 1356
There are two fundamental types of variables in .NET (and most any
language). IN .NET they are called Value types and Reference Types.
Technically value types are allocated on the stack (relatively small amount
of memory), reference type are allocated on the heap (relatively large
amount of memory).

Knowing that, you don't use "new" to instantiate any value types, and you
use it to instantiate reference types. Value types are all the primitive
types (int, char, bool, long) and structure (either that you create or
pre-existing ones (ints, chars, bools, longs,... are all structures)). All
other "classes" are instantiated with new. The exception is a string which
despite being a reference type doesn't require a new.

You can read up more on stack and heaps and get even greater insight.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Learner" <pr****@gmail.c om> wrote in message
news:11******** *************@v 46g2000cwv.goog legroups.com...
Hello,
I have a basic question regarding 'New' keyword.

For instance I can declare a variable as string (class)

as Dim myName as String = String.Empty
or Dim I as Integer
But when I want declare a button we instantiate it as
Dim b as Button = New Button

I am just trying to understand when (for what kind of classes) to use
New key word.

Please some one help to clarify this.
Thanks
-L

Mar 29 '06 #2
For classes you'll need to call the new keyword
For value-types (integer, double, string, etc) you dont have to do that.
Also for shared or static functions in a class you dont need to use new.

Hope this helps.

"Learner" <pr****@gmail.c om> wrote in message
news:11******** *************@v 46g2000cwv.goog legroups.com...
Hello,
I have a basic question regarding 'New' keyword.

For instance I can declare a variable as string (class)

as Dim myName as String = String.Empty
or Dim I as Integer
But when I want declare a button we instantiate it as
Dim b as Button = New Button

I am just trying to understand when (for what kind of classes) to use
New key word.

Please some one help to clarify this.
Thanks
-L

Mar 29 '06 #3
Hello,
Thanks for the explanation and its clear :)

thanks again
-L

Mar 29 '06 #4
DWS
Use new when the IDE asks for it.

"Pipo" wrote:
For classes you'll need to call the new keyword
For value-types (integer, double, string, etc) you dont have to do that.
Also for shared or static functions in a class you dont need to use new.

Hope this helps.

"Learner" <pr****@gmail.c om> wrote in message
news:11******** *************@v 46g2000cwv.goog legroups.com...
Hello,
I have a basic question regarding 'New' keyword.

For instance I can declare a variable as string (class)

as Dim myName as String = String.Empty
or Dim I as Integer
But when I want declare a button we instantiate it as
Dim b as Button = New Button

I am just trying to understand when (for what kind of classes) to use
New key word.

Please some one help to clarify this.
Thanks
-L


Mar 29 '06 #5

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

Similar topics

2
2026
by: ksskumar2000 | last post by:
Hi, I have added following two reference under COM tab, Microsoft Office 11.0 Object Library Microsoft Word 11.0 Object Library The software I have used: Visual studio 1.14 Microsoft Office 2003
5
4045
by: Jerry Hull | last post by:
I'm working with a database developed by an untrained person over several years - and on a network that has recently been upgraded with a new server installed and MS office upgraded from 2K (I think - it might have been XP) to 2003. The database is impressive, both in what it does and the obtuse and inconsistent ways it works. There are several hundred queries, for example, with no indication of where they are used or if they are in fact...
0
1250
by: Eric | last post by:
I have written a C# program that creates a new bitmap: Bitmap ChartBitmap = new Bitmap( 780, 360 ); then I create a button which copies that bitmap into the clipboard: Clipboard.SetDataObject( ChartBitmap ); after which I paste that bitmap into MS Word. Some of the colors change or don't show up when the bitmap is pasted into Word. This
3
30820
by: Adam Faulkner via DotNetMonster.com | last post by:
I want to create a method within a class that opens a Microsoft Word 2000 Document and has the facility to Create a new word document and then extract a Page that exists within the original Word Document and save it to a new Word Document. I would need to generate a loop for each page found within a word document to create a new word document and insert the existing page into the new word document and then save as a new word document. ...
1
7652
by: Adam Faulkner via DotNetMonster.com | last post by:
I had a problem before extracting pages from an existing word document and then inserting the content into a new word document. The following code below works with Microsoft Word 2000 Function ParseWordDoc(ByVal Filename As String) As String Dim sNewFileName As String Dim WordApp As Word.Application = New Word.Application Dim BaseDoc As Word.Document Dim DestDoc As Word.Document
2
1489
by: Edgardo | last post by:
I hope I'm in the correct forum for this, as the problem is hard to categorize. I have a VS 2005 asp.net application that invokes Word 2003 to opean and read a word document. the aplication have <identity impersonate="true"/. The application works fine on my machine (Win XP SP2 and Office 2003 SP2) but when I deploy the app to a server (Server 2003, Office 2003 SP2) everything works fine except when I try to launch Word. The application...
5
1450
by: steve | last post by:
Hi All I have a VB.net 2005 App which uses MS Word 2003 to do printing and Mail Merging, by automation All works fine on my Laptop (XP Pro SP2, Word 2003) but on my desktop (XP Pro SP2, Word 2003) I get the following message when I try to launch Word using the code.. Imports Word = Microsoft.Office.Interop.Word
5
6882
by: Rob | last post by:
Hi, I have a VB.Net application that parses an HTML file. This file was an MS Word document that was saved as web page. My application removes all unnecessary code generated by MS Word and does some custom formatting needed by my client. I use a StreamReader to read in the file...regular expressions to parse and clean up the file...and a StreamWriter to write the new file. On some HTML files that I parse, I get this character "á"...
4
1444
by: lyle | last post by:
Sometimes before clicking "Post" I copy my message, open Word and paste. Word is excellent, and can suggest synonyms and translations. But it takes a while for Word to open. Recently, I've been running Outlook 2007 minimized; it opens quickly and gives me the same facility as Word when I paste into a new Mail Message, but considerably faster. Sometimes I forget to do either. My spelling and grammar skills and knowledge are good but my...
2
20177
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to switch back to "Left" aligned for remaining paragraphs, the text within the Word document remained Centered. I'm using: VS2008 w/ .NET Framework 3.5, Microsoft.Office.Interop.Word Version 12.0.0.0, and I'm creating the document as a 97-2003 document...
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10026
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.