473,624 Members | 2,238 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can someone explain this use of "new"?

I have another newbie question I could use some help with. I ran across this
code in one of the MS training guides:

public class NullTokenVisito r
{...}

public class Application
{
public static void Main()
{
new NullTokenVisito r();
}

}
I was surprised that this code compiles and runs. It appears that I am
creating a new instance of the NullTokenVisito r class without assigning the
object reference to a variable. Am I understanding this code correctly? If
so, what would be the purpose of using this type of expression outside of a
purely academic exercises?
Jan 23 '06 #1
7 1336
the only thing matters to the compiler is syntax (c#).
theres *nothing* wrong to that code.
Jan 23 '06 #2
It would mean that any code in the NullTokenVisito r's constructor will
be called. Perhaps the code in that constructor has some affect on
objects outside of its scope. Maybe it "adds 1" to the "Visitor" table
in a database or something. God knows really... whats in the
constructor code?

Its kind of along the same lines as something like this:
Dim d as double = 45.7214
Math.Floor(d)

I can call Math.Floor(d) and not assign its result to anything. It
would be pointless, but the compiler accepts it, and still performs the
flooring function....

This help?

I could be wrong tho... (I usually rate my chances at around 25%
correctness)

Jan 23 '06 #3
Does that mean you're overpaid by a factor of 4? ;o)
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Steven Nagy" <le*********@ho tmail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
[ ]
I could be wrong tho... (I usually rate my chances at around 25%
correctness)

Jan 23 '06 #4
At my pay rate, I'm practically a volunteer...

Jan 23 '06 #5
Steven,

Thanks for your input. In the example you provide of Math.Floor() I sort of
assumed that Floor is a static method so it makes sense that you would call
it without instantiating an object. What good is it to fire the constructor
in a class if you don't assigne the object reference to a variable? The
object is lost and now just wasted memory. It bakes my noodle is that the C#
compiler would even allow me to instantiate an object without assigning it to
a variable. The sample code I provided uses it as an artifical test just to
make sure that the class would instantiate without throwing an error. I
wouldn't expect to ever leave that sort of code in a release application and
I can't figure out what sort of practical use such a statement could have in
a live application.

"Steven Nagy" wrote:
It would mean that any code in the NullTokenVisito r's constructor will
be called. Perhaps the code in that constructor has some affect on
objects outside of its scope. Maybe it "adds 1" to the "Visitor" table
in a database or something. God knows really... whats in the
constructor code?

Its kind of along the same lines as something like this:
Dim d as double = 45.7214
Math.Floor(d)

I can call Math.Floor(d) and not assign its result to anything. It
would be pointless, but the compiler accepts it, and still performs the
flooring function....

This help?

I could be wrong tho... (I usually rate my chances at around 25%
correctness)

Jan 23 '06 #6
Why?

Firstly, the object isn't lost: the GC knows exactly where to find it, and
will come a-knockin' very soon.

Secondly, the compiler has no way of knowing what your ctor does; it could
increment static variables, it could chhose to throw an exception (perhaps
arguably unwisely, but that's another issue), it could choose to tweak any
of the variables in the arguments to the ctor.

Thridly - in the "lost" scenario, your instance ctor could also perhaps (not
100% sure - haven't tried it) add itself to a static collection, so even
less "lost" - in fact, if it was a form, the ctor could also perhaps show
itself? (again not tried, and again, not perhaps adviseable).

OK, fine, all of the above scenarios would be better done in better ways,
but I see no sensible reason that the compiler should know this. I'd rather
it simply collected an object occasionally than have me wondering why
something that looks logical doesn't work...

Marc
Jan 23 '06 #7
Exactly.

The point is you can do it, just like you code like this:

MyObject.x = 1;
MyObject.x = 2;
MyObject.x = 1;

Seems completely pointless! But the compiler lets you do it...
And it could be that the X property is doing more than what the code
suggests... it could be launching missiles at the soviet union in the
related property getter.

Jan 24 '06 #8

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

Similar topics

15
2288
by: b83503104 | last post by:
Hi, class MyClass{ int array_size; HisClass **hisObject; }; I want hisObject to point to an array of HisClass objects. The size of the array is given by array_size.
1
2579
by: sven_c_t | last post by:
Hi! Probably a newbie question. I´ve been working a bit with the widget toolkit FLTK and I have been wondering why there is such a heavy use of the new operator, when it does not seem to be nessesary (at least to me). What follows is an example taken from the FLTK tutorial: ------------- #include <FL/Fl.H> #include <FL/Fl_Window.H>
12
1907
by: Ola Johansson | last post by:
Can anyone explain to me a meningfull use of the "new" keyword (Shadows in VB). I think i understand how it works fully but i cant figure out why you would use it. Why would you want to declare a variable in a derived class with the same name as in the base class but with a diffrent meaning? Anyone can think of a scenario for this?
1
1999
by: mmcc128 | last post by:
Currently using the "document.images" to "preload" images - not for future pages, but for the page being loaded. I got it from http://www.dynamicdrive.com/dynamicindex4/imagetooltip.htm Its a nice script - pops up an img on mouseover. I understand that its downloading SOMETHING - but its not rendering.... THE QUESTION: How does "preloading" these images (into cache, as I have read) affect page size, page load, etc. I cannot find...
51
3915
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and output to be within one directory, and I want to be able to combine files from different directories into another directory of my choice). My program seems to work fine, but I'm wondering about this loop: for (int i = 0; i < numFiles; i++)
7
1847
by: Jim Land | last post by:
In the Yahoo Interface Blog, Douglas Crockford wrote about Javascript: "So the rule is simple: The only time we should use the new operator is to invoke a pseudoclassical Constructor function. When calling a Constructor function, the use of new is mandatory." http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/
350
11677
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
12
4801
by: Robert Fuchs | last post by:
Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
0
999
by: Manousos | last post by:
i am very new in programming in c,but i have to learn many things very quickly because of some lessons i have took on my university!! my English are not very good but l will try to explain what i want to built!!! i have to write a code which solves differential equations (-u''+u=f) with two methods after many hours compiling i achieve this but this was not the end the next step is my problem!!! on those methods i use one separate it in N...
0
8233
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
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8619
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7158
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
6108
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
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
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.