473,725 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Should this be a warning, at least?

The C# compiler gives warnings about unused variables. I thin it needs a
warning about an unassigned reference. For instance, this compiles without
any warnings/errors, but of course blows up when run:

SqlConnection cnn = null;
try
{
new SqlConnection(m yConnectionStri ng);
cnn.Open()
Comments?

Rich Sienkiewicz
Nice Systems, Inc.
Jan 25 '06 #1
10 1686

Rich Sienkiewicz wrote:
The C# compiler gives warnings about unused variables. I thin it needs a
warning about an unassigned reference. For instance, this compiles without
any warnings/errors, but of course blows up when run:

SqlConnection cnn = null;
try
{
new SqlConnection(m yConnectionStri ng);
cnn.Open()
Comments?


I'd say no. Its too hard to figure out if cnn could have been set as a
side-effect
of some other action. Yes, this particular one would be easy to spot,
but cnn
IS assigned to something (null). If you just wrote:

SqlConnection cnn;

cnn.Open();

I'd expect an error, or a warning at the very least.

Matt

Jan 25 '06 #2
Rich,

You initialize cnn by setting *null*. If you don't do that the compiler will
complain that you are trying to use unitialized variable.
--

Stoitcho Goutsev (100)

"Rich Sienkiewicz" <Ri************ *@discussions.m icrosoft.com> wrote in
message news:26******** *************** ***********@mic rosoft.com...
The C# compiler gives warnings about unused variables. I thin it needs a
warning about an unassigned reference. For instance, this compiles without
any warnings/errors, but of course blows up when run:

SqlConnection cnn = null;
try
{
new SqlConnection(m yConnectionStri ng);
cnn.Open()
Comments?

Rich Sienkiewicz
Nice Systems, Inc.

Jan 25 '06 #3
Rich,

I agree, for this situation, it would be nice, but it would be very
limited, limited only by variables declared on the stack. If they were
parameters or fields, it would require one to do a much more intensive
static analysis.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rich Sienkiewicz" <Ri************ *@discussions.m icrosoft.com> wrote in
message news:26******** *************** ***********@mic rosoft.com...
The C# compiler gives warnings about unused variables. I thin it needs a
warning about an unassigned reference. For instance, this compiles without
any warnings/errors, but of course blows up when run:

SqlConnection cnn = null;
try
{
new SqlConnection(m yConnectionStri ng);
cnn.Open()
Comments?

Rich Sienkiewicz
Nice Systems, Inc.

Jan 25 '06 #4
I'd wonder if Spec# would catch this. Too bad it's still in R&D.

Anyone know if I can safely run Spec# on the same production dev box and not
mess up my reliable C# compiler? I'd like to play with this...

"Stoitcho Goutsev (100)" wrote:
Rich,

You initialize cnn by setting *null*. If you don't do that the compiler will
complain that you are trying to use unitialized variable.
--

Stoitcho Goutsev (100)

"Rich Sienkiewicz" <Ri************ *@discussions.m icrosoft.com> wrote in
message news:26******** *************** ***********@mic rosoft.com...
The C# compiler gives warnings about unused variables. I thin it needs a
warning about an unassigned reference. For instance, this compiles without
any warnings/errors, but of course blows up when run:

SqlConnection cnn = null;
try
{
new SqlConnection(m yConnectionStri ng);
cnn.Open()
Comments?

Rich Sienkiewicz
Nice Systems, Inc.


Jan 25 '06 #5
Just to clarify, are you saying that the compiler should issue a
warning about this line:

new SqlConnection(m yConnectionStri ng);

because the reference returned from "new" wasn't assigned to anything?

Or are you saying that the compiler should issue a warning about this
line:

cnn.Open();

because cnn will be null at run time?

The first seems reasonable to me, although it seems an unlikely mistake
to make. True, the compiler doesn't _really_ know that doing the "new"
doesn't have some useful side-effect that in fact you don't want to
assign the result to anything, but such a warning would be trivially
eliminated by just doing this:

SqlConnection dummy = new SqlConnection(m yConnectionStri ng);

so it's not a bad idea, although I'm not sure how useful it would be
because I've never seen that mistake, myself.

The second wouldn't fly because assigning "null" to cnn means that it
has been assigned. The following, on the other hand, already generates
a warning (I believe):

SqlConnection cnn;
try
{
new SqlConnection(m yConnectionStri ng);
cnn.Open();
}

because the compiler knows that "cnn" hasn't been given any value at
all, not even null.

Jan 25 '06 #6
To clarify, I would like the compiler to give a warning about the result of
the new statement not being assigned to anything. Like I said, the compiler
warns of non used variables, of non-assigned variables, so it could issue a
warning about a missing assignment. What use could this new statement, or any
other new, be if it's not assigned to something?

BTW, the error was made during a hasty cut and paste.

Thanks everyone

Rich

"Bruce Wood" wrote:
Just to clarify, are you saying that the compiler should issue a
warning about this line:

new SqlConnection(m yConnectionStri ng);

because the reference returned from "new" wasn't assigned to anything?

Or are you saying that the compiler should issue a warning about this
line:

cnn.Open();

because cnn will be null at run time?

The first seems reasonable to me, although it seems an unlikely mistake
to make. True, the compiler doesn't _really_ know that doing the "new"
doesn't have some useful side-effect that in fact you don't want to
assign the result to anything, but such a warning would be trivially
eliminated by just doing this:

SqlConnection dummy = new SqlConnection(m yConnectionStri ng);

so it's not a bad idea, although I'm not sure how useful it would be
because I've never seen that mistake, myself.

The second wouldn't fly because assigning "null" to cnn means that it
has been assigned. The following, on the other hand, already generates
a warning (I believe):

SqlConnection cnn;
try
{
new SqlConnection(m yConnectionStri ng);
cnn.Open();
}

because the compiler knows that "cnn" hasn't been given any value at
all, not even null.

Jan 26 '06 #7
Rich Sienkiewicz wrote:
To clarify, I would like the compiler to give a warning about the result of
the new statement not being assigned to anything. Like I said, the compiler
warns of non used variables, of non-assigned variables, so it could issue a
warning about a missing assignment. What use could this new statement, or any
other new, be if it's not assigned to something?


Constructing a new instance might do any number of things in the
background, such as registering the new instance in a pool. It's
unlikely to be a good thing, but I don't think the language ought to
complain. Would you want the language to complain about calling methods
but not using the return value? It's much the same issue.

Jon

Jan 26 '06 #8
With all due respect, Jon, it's not all that much the same issue.

Calling a method but not using its return value (or using an operator
but not storing the result) is a common technique for causing some
side-effect. (Viz: the oh-so-common "i++;")

However, "new"ing an object but not storing it anywhere is only very
rarely a useful thing to do, and, in those very few cases in which it
is useful, it's easy enough to assign the result to a "dummy" variable
in order to have the compiler shut up.

That said, I would also say that this is such an uncommon mistake to
make (newing something but not assigning it to a variable) that it's
unlikely to make it anywhere near the top of the list of compiler mods.

Jan 26 '06 #9
Bruce,

I agree with Jon,

If the compiler force you to create dummy variables then one is going to
complain about this. The compiler should do anything possible to help you
spot problems without being intrusive forcing you to write obviously
redundant code, otherwise it is going to be more like fighting with the
compiler than writing programs.
--

Stoitcho Goutsev (100)

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
With all due respect, Jon, it's not all that much the same issue.

Calling a method but not using its return value (or using an operator
but not storing the result) is a common technique for causing some
side-effect. (Viz: the oh-so-common "i++;")

However, "new"ing an object but not storing it anywhere is only very
rarely a useful thing to do, and, in those very few cases in which it
is useful, it's easy enough to assign the result to a "dummy" variable
in order to have the compiler shut up.

That said, I would also say that this is such an uncommon mistake to
make (newing something but not assigning it to a variable) that it's
unlikely to make it anywhere near the top of the list of compiler mods.

Jan 26 '06 #10

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

Similar topics

1
12858
by: Mini Mouse | last post by:
Hiya folks, I'm getting the following error(s) below and I'm at a bit of a loss as to how to correct it. When I give it a parameter it then complains it needs two parameters and the second one being an integer or something like that. Warning: domdocument() expects at least 1 parameter, 0 given in orders.php on line 157
0
400
by: tmartsum | last post by:
I have a discussion in comp.std.c++ After a (bit stupid) suggestion on representing a fixed 'big' length int I moderated it to "Bitfields-ints should be allowed to have any fixed length" I got no replys on my moderated suggestion. (I am asking you your opinion of this - I do not really have a C++-problem)
4
1232
by: Daniel Jin | last post by:
look at the sampl <pre using System class Bas public virtual void Print(
6
15100
by: Daniel Rudy | last post by:
Hello Group. Please consider the following code: /* this table is used in the wipedevice routine */ static const struct wipe_t { uchar wte; /* wipe table entry */ } wipetable = { {0x00, 0x00, 0x00},
92
6215
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
2
26933
by: EricDeWerth | last post by:
When I try to call db2move $todbname load from a shell script it will give me this error in the log. When I call it from the command line using the exact same dataset it works perfectly. Does anyone know what might be causing that to occur? Below is the .msg file. Thanks for any help. I really need to put this script to bed. This is the last hangup. Regards Eric
10
2335
by: MisterE | last post by:
typedef struct sg { int a; } G; int c(G* g) { return g->a; }
0
951
by: jason-sage | last post by:
Hi all, I just started using the warnings module in Python 2.5.2. When I trigger a warning using the default warning options, an entry is created in a module-level cache so that the warning is ignored in the future. However, I don't see an easy way to clear or invalidate these module-level caches of previously triggered warnings. That means that if I ever have a warning triggered with a "once" or a default warning level or filter, I...
24
2518
by: Steven D'Aprano | last post by:
Sometimes it seems that barely a day goes by without some newbie, or not- so-newbie, getting confused by the behaviour of functions with mutable default arguments. No sooner does one thread finally, and painfully, fade away than another one starts up. I suggest that Python should raise warnings.RuntimeWarning (or similar?) when a function is defined with a default argument consisting of a list, dict or set. (This is not meant as an...
0
8751
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
9257
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
9172
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
8095
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
6702
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
6011
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();...
0
4781
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3219
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
2633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.