472,993 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 NullTokenVisitor
{...}

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

}
I was surprised that this code compiles and runs. It appears that I am
creating a new instance of the NullTokenVisitor 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 1286
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 NullTokenVisitor'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*********@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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 NullTokenVisitor'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
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
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...
12
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...
1
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...
51
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...
7
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....
350
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...
12
by: Robert Fuchs | last post by:
Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
0
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.