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(myConnectionString);
cnn.Open()
Comments?
Rich Sienkiewicz
Nice Systems, Inc. 10 1657
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(myConnectionString); 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
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.microsoft.com> wrote in
message news:26**********************************@microsof t.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(myConnectionString); cnn.Open()
Comments?
Rich Sienkiewicz Nice Systems, Inc.
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.com
"Rich Sienkiewicz" <Ri*************@discussions.microsoft.com> wrote in
message news:26**********************************@microsof t.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(myConnectionString); cnn.Open()
Comments?
Rich Sienkiewicz Nice Systems, Inc.
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.microsoft.com> wrote in message news:26**********************************@microsof t.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(myConnectionString); cnn.Open()
Comments?
Rich Sienkiewicz Nice Systems, Inc.
Just to clarify, are you saying that the compiler should issue a
warning about this line:
new SqlConnection(myConnectionString);
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(myConnectionString);
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(myConnectionString);
cnn.Open();
}
because the compiler knows that "cnn" hasn't been given any value at
all, not even null.
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(myConnectionString);
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(myConnectionString);
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(myConnectionString); cnn.Open(); }
because the compiler knows that "cnn" hasn't been given any value at all, not even null.
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
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.
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*******@canada.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.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.
Hi,
"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.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++;")
In the framework itself you have a lot of methods like this, take for
example
SqlCommand.Parameters.Add ( ... ) more often you do not use the returned
Parameter ( at least directly )
Another good example is DataTable.Column.Add , unless you need to especify
some extra features ( PK, autoincrement) you just ignore the returned object
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.
I use construction like this very often:
new A_Windows_Form().Show();
I dont need to store a reference to it, so I don't
I agree with Jon, this should not be even a warning
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: Daniel Jin |
last post by:
look at the sampl
<pre
using System
class Bas
public virtual void Print(
|
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 = {...
|
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);
|
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...
|
by: MisterE |
last post by:
typedef struct sg
{
int a;
} G;
int c(G* g)
{
return g->a;
}
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| | |