473,408 Members | 2,839 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,408 software developers and data experts.

New Keyword?

Consider the following 2 statements (VB.NET):

Dim Button1 As System.Windows.Forms.Button
Dim Button2 As New System.Windows.Forms.Button()

The .NET Framework 2.0 SDK states that:

The first statement declares an object variable that can contain a
reference to a button object. However, the variable Button1 contains
the value Nothing until you assign an object of type Button to it. The
second statement also defines a variable that can contain a button
object, but the New keyword creates a button object and assigns it to
the variable Button2.

I couldn't exactly follow the difference stated above. The value of the
variable Button1 is Nothing but then what is the value of Button2?
Moreover, why is the value of the variable Button1 Nothing? Also is
"declaring an object variable" same as "defining an object variable"?

Can someone please explain me the difference in a simpler way, if
possible?

Thanks,

Arpan

Jul 21 '06 #1
2 1139
Button1 points to Nothing. It does not point to an object, it is not
referencing anything. All Button1 can do at this point, is it is capable of
pointing to a button, but it isnt actually.

Button2 points to an instance of a button.

Declaring a variable and defining it are the same thing, different wording.
Instanciating a variable, that is different. Instanciating is what you are
doing with Button2 - you are giving it an object it can point to.

"Arpan" <ar******@hotmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
Consider the following 2 statements (VB.NET):

Dim Button1 As System.Windows.Forms.Button
Dim Button2 As New System.Windows.Forms.Button()

The .NET Framework 2.0 SDK states that:

The first statement declares an object variable that can contain a
reference to a button object. However, the variable Button1 contains
the value Nothing until you assign an object of type Button to it. The
second statement also defines a variable that can contain a button
object, but the New keyword creates a button object and assigns it to
the variable Button2.

I couldn't exactly follow the difference stated above. The value of the
variable Button1 is Nothing but then what is the value of Button2?
Moreover, why is the value of the variable Button1 Nothing? Also is
"declaring an object variable" same as "defining an object variable"?

Can someone please explain me the difference in a simpler way, if
possible?

Thanks,

Arpan

Jul 21 '06 #2
Arpan wrote:
Dim Button1 As System.Windows.Forms.Button
Dim Button2 As New System.Windows.Forms.Button()
I couldn't exactly follow the difference stated above. The value of the
variable Button1 is Nothing but then what is the value of Button2?
The above is a "compact" syntax the VB lets you get away with. It makes
[a little] more sense if you look at the /full/ syntax:

Dim Button1 As System.Windows.Forms.Button

Dim Button2 As System.Windows.Forms.Button _
= New System.Windows.Forms.Button()

The first gets you a variable that I would call "Button-shaped"
(formally it's an Object Reference to a Control of Type Button), but it
doesn't actually point at a anything (it's a null reference).
The second gets you another "Button-shaped" variable, but /also/ creates
a Button control (the "= New Button" bit) and stores a reference to that
Button in the variable.
Moreover, why is the value of the variable Button1 Nothing?
Because you haven't /assigned/ anything to it, so VB implicitly
initialises it to Nothing.
Also is "declaring an object variable" same as "defining an object variable"?
In this context, yes.

HTH,
Phill W.
Jul 25 '06 #3

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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.