In VS 2008,
I have an object, dbReader, that I get a warning saying that it is used
before it has been assigned a value.
That is correct.
Dim dbReader As SqlDataReader
and later:
Finally
If (Not dbReader Is Nothing) Then dbReader.Close()
End Try
Why is this an error?
Here I am testing to see if it is nothing, which it is (isn't it?) before
using it. The error is in the "if" test not the dbReader.Close().
Thanks,
Tom 17 2064
On Mon, 6 Oct 2008 08:11:32 -0700, "tshad" <tf*@dslextreme.comwrote:
>In VS 2008,
I have an object, dbReader, that I get a warning saying that it is used before it has been assigned a value.
That is correct.
Dim dbReader As SqlDataReader
and later:
Finally
If (Not dbReader Is Nothing) Then dbReader.Close()
End Try
Why is this an error?
Here I am testing to see if it is nothing, which it is (isn't it?) before using it. The error is in the "if" test not the dbReader.Close().
Thanks,
Tom
I think the error is correct. The compiler is complaining that you
are testing dbReader before you have set it to anything.
You are assuming that dbReader is intialized to Nothing and therefore
the test is OK. While that is probably true, you should explicitly
set dbReader to Nothing on the Dim line. The compiler checks for
using before setting do not take into account the value that an
uninitialized variable has.
Dim dbReader As SqlDataReader = Nothing
Try that.
"tshad" <tf*@dslextreme.comwrote in message
news:OG**************@TK2MSFTNGP02.phx.gbl...
In VS 2008,
I have an object, dbReader, that I get a warning saying that it is used
before it has been assigned a value.
That is correct.
Dim dbReader As SqlDataReader
and later:
Finally
If (Not dbReader Is Nothing) Then dbReader.Close()
End Try
Why is this an error?
Here I am testing to see if it is nothing, which it is (isn't it?) before
using it. The error is in the "if" test not the dbReader.Close().
Thanks,
Tom
"Jack Jackson" <jj******@cinnovations.netschrieb
On Mon, 6 Oct 2008 08:11:32 -0700, "tshad" <tf*@dslextreme.com>
wrote:
In VS 2008,
I have an object, dbReader, that I get a warning saying that it is
used before it has been assigned a value.
That is correct.
Dim dbReader As SqlDataReader
and later:
Finally
If (Not dbReader Is Nothing) Then dbReader.Close()
End Try
Why is this an error?
Here I am testing to see if it is nothing, which it is (isn't it?)
before using it. The error is in the "if" test not the
dbReader.Close().
Thanks,
Tom
I think the error is correct. The compiler is complaining that you
are testing dbReader before you have set it to anything.
You are assuming that dbReader is intialized to Nothing and
therefore the test is OK. While that is probably true, you should
explicitly set dbReader to Nothing on the Dim line. The compiler
checks for using before setting do not take into account the value
that an
uninitialized variable has.
I think it is a bad practice to initialize a reference variable with Nothing
just to suppress the compiler warning. Maybe in the Try block he does use
dbReader.method and did forget to initialize the variable, and I mean
setting it to a real object and not to Nothing. It is almost like disabling
Option Strict to avoid warnings in lines that will never lead to an
exception in the particular case but can make us overlook errors in other
cases.
I also think that Is/IsNot comparisons of reference variables should not
lead to a compiler warning.
Armin
"Jack Jackson" <jj******@cinnovations.netwrote in message
news:le********************************@4ax.com...
On Mon, 6 Oct 2008 08:11:32 -0700, "tshad" <tf*@dslextreme.comwrote:
>>In VS 2008,
I have an object, dbReader, that I get a warning saying that it is used before it has been assigned a value.
That is correct.
Dim dbReader As SqlDataReader
and later:
Finally If (Not dbReader Is Nothing) Then dbReader.Close() End Try
Why is this an error?
Here I am testing to see if it is nothing, which it is (isn't it?) before using it. The error is in the "if" test not the dbReader.Close().
Thanks,
Tom
I think the error is correct. The compiler is complaining that you
are testing dbReader before you have set it to anything.
You are assuming that dbReader is intialized to Nothing and therefore
the test is OK. While that is probably true, you should explicitly
set dbReader to Nothing on the Dim line. The compiler checks for
using before setting do not take into account the value that an
uninitialized variable has.
That makes sense.
I think that scalars are set to their various defaults (ints are initialized
to 0, I belive). But reference variables are not (and that includes
strings - you get the same error if you enclose it in a block.
Thanks,
Tom
>
I also think that Is/IsNot comparisons of reference variables should not
lead to a compiler warning.
Armin
I have had this same issue many times. In fact, in one application,
this one warning occurs so many times, that I reach the max # of
warnings (like 102) and can't see the rest. It really bugs me too as
this seems to be a totally valid check. After all, dbReader is either
Nothing or it is not. As long as it has been Dim'd, this check should
be allowed WITHOUT a compiler warning, in my humble opinion.
"Armin Zingler" <az*******@freenet.dewrote in message
news:uC**************@TK2MSFTNGP05.phx.gbl...
"Jack Jackson" <jj******@cinnovations.netschrieb
>On Mon, 6 Oct 2008 08:11:32 -0700, "tshad" <tf*@dslextreme.com> wrote:
In VS 2008,
I have an object, dbReader, that I get a warning saying that it is
used before it has been assigned a value.
That is correct.
Dim dbReader As SqlDataReader
and later:
Finally
If (Not dbReader Is Nothing) Then dbReader.Close()
End Try
Why is this an error?
Here I am testing to see if it is nothing, which it is (isn't it?)
before using it. The error is in the "if" test not the
dbReader.Close().
Thanks,
Tom I think the error is correct. The compiler is complaining that you are testing dbReader before you have set it to anything.
You are assuming that dbReader is intialized to Nothing and therefore the test is OK. While that is probably true, you should explicitly set dbReader to Nothing on the Dim line. The compiler checks for using before setting do not take into account the value that an uninitialized variable has.
I think it is a bad practice to initialize a reference variable with
Nothing
just to suppress the compiler warning. Maybe in the Try block he does use
dbReader.method and did forget to initialize the variable, and I mean
setting it to a real object and not to Nothing. It is almost like
disabling
Option Strict to avoid warnings in lines that will never lead to an
exception in the particular case but can make us overlook errors in other
cases.
The error is actually a warning not an error. And it does say that it could
lead to a runtime error. In C#, it is an error and won't let you continue
until you resolve it.
Sub Main()
Dim stemp As String
Dim stemp2 As String
Try
Catch ex As Exception
stemp = stemp2
End Try
End Sub
This is a warning because it could potentially cause a runtime error.
I don't think it is bad practice. A scalar is set to something, it is just
set by the compiler. It would be nice if the compiler would also set a
reference variable to nothing when defined as well. But there may be a
reason or a case where that may be a problem - not sure.
If you don't set it and goes to the Catch, you will probably get a runtime
error.
Why is setting the variable in the "try" any different than just setting it
to nothing to start with? Why would ever want the variable to be set to
garbage?
Thanks,
Tom
I also think that Is/IsNot comparisons of reference variables should not
lead to a compiler warning.
I agree, but it does.
>
Armin
"Jack Jackson" <jj******@cinnovations.netschrieb:
>
>>In VS 2008,
I have an object, dbReader, that I get a warning saying that it is used before it has been assigned a value.
That is correct.
Dim dbReader As SqlDataReader
and later:
Finally If (Not dbReader Is Nothing) Then dbReader.Close() End Try
Why is this an error?
Here I am testing to see if it is nothing, which it is (isn't it?) before using it. The error is in the "if" test not the dbReader.Close().
Thanks,
Tom
I think the error is correct. The compiler is complaining that you
are testing dbReader before you have set it to anything.
You are assuming that dbReader is intialized to Nothing and therefore
the test is OK. While that is probably true, you should explicitly
set dbReader to Nothing on the Dim line.
I strongly disagree. In VB the statement 'Dim x As Foo' is semantically
equal to 'Dim x As Foo = Nothing' because VB implicitly initializes
variables of reference types with 'Nothing'. This is specified behavior and
I don't think there is any reason to make the programmer enter a rather
useless ' = Nothing', although the 'Dim x As Foo' statement already contains
this contract. Consequently I have disabled the warning and I consider it
flawed.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
On Oct 6, 11:45 am, "tshad" <t...@dslextreme.comwrote:
>
I don't think it is bad practice. A scalar is set to something, it is just
set by the compiler. It would be nice if the compiler would also set a
reference variable to nothing when defined as well. But there may be a
reason or a case where that may be a problem - not sure.
Why is setting the variable in the "try" any different than just setting it
to nothing to start with? Why would ever want the variable to be set to
garbage?
The compiler doesn't set any values. They do not get set until
runtime.
Consider the following VB sub:
Private Sub SomeSub()
Dim s As String
Dim i As Integer
s = "Hello"
i = 123
End Sub
This code compiles to the following IL:
..method private instance void SomeSub() cil managed
{
.maxstack 1
.locals init (
[0] int32 i,
[1] string s)
L_0000: ldstr "Hello"
L_0005: stloc.1
L_0006: ldc.i4.s 0x7b
L_0008: stloc.0
L_0009: ret
}
Note the .locals init instruction. The init keyword sets a flag that
causes the JIT compiler to inject code initializing all local
variables before executing the sub. Thus, for all value types, the
corresponding memory is zeroed out and all reference types types are
set to null (Nothing in VB).
Although I agree that you should logically not assume the value of a
variable, you can assume in .Net that variables don't point to
anything bad when they are declared. They are initialized properly.
This information comes from the book "Expert .Net 2.0 IL Assembler",
By Serge Lidin, Apress
Cheers,
Chris
I strongly disagree. In VB the statement 'Dim x As Foo' is semantically
equal to 'Dim x As Foo = Nothing' because VB implicitly initializes
variables of reference types with 'Nothing'. This is specified behavior and
I don't think there is any reason to make the programmer enter a rather
useless ' = Nothing', although the 'Dim x As Foo' statement already contains
this contract. Consequently I have disabled the warning and I consider it
flawed.
I agree with Herfried, and I have also disabled this warning.
"AMercer" <AM*****@discussions.microsoft.comwrote in message
news:81**********************************@microsof t.com...
>I strongly disagree. In VB the statement 'Dim x As Foo' is semantically equal to 'Dim x As Foo = Nothing' because VB implicitly initializes variables of reference types with 'Nothing'. This is specified behavior and I don't think there is any reason to make the programmer enter a rather useless ' = Nothing', although the 'Dim x As Foo' statement already contains this contract. Consequently I have disabled the warning and I consider it flawed.
I agree with Herfried, and I have also disabled this warning
Where do you disable the warning?
Thanks,
Tom.
Where do you disable the warning?
In VS 2005, load your project, then go to Project, Properties, Compile, and
then disable condition "Use of variable prior to assignment". For other
versions of VS, it should be similar.
tshad wrote:
It would be nice if the compiler would also set a
reference variable to nothing when defined as well. But there may be a
reason or a case where that may be a problem - not sure.
Actually the variable is set to nothing when the method starts, and that
can potentially cause problems in some situations.
I guess that the warning has been introduced to prevent exactly that. It
wasn't there in earlier versions of VB.
--
Göran Andersson
_____ http://www.guffa.com
Herfried K. Wagner [MVP] wrote:
In VB the statement 'Dim x As Foo' is semantically
equal to 'Dim x As Foo = Nothing' because VB implicitly initializes
variables of reference types with 'Nothing'.
Not exactly. The variable is initialsed when the method starts, not
where the variable is declared. Consider:
For i As Integer = 1 to 10
Dim x As Integer
If i = 6 Then
x = 10
End If
Console.WriteLine(x)
Next
This would output:
0
0
0
0
0
10
10
10
10
10
If you change the line to:
Dim x As Integer = 0
It would output:
0
0
0
0
0
10
0
0
0
0
The warning is clearly intended to prevent situations like these.
--
Göran Andersson
_____ http://www.guffa.com
Sloan,
And what does that help, beside not showing the warning.
Why should VB be used at the same low level as C#.
See the comments from Armin and Herfried about this.
Cor
"sloan" <sl***@ipass.netschreef in bericht
news:en**************@TK2MSFTNGP05.phx.gbl...
Dim dbReader As SqlDataReader = Nothing
Try that.
"tshad" <tf*@dslextreme.comwrote in message
news:OG**************@TK2MSFTNGP02.phx.gbl...
>In VS 2008,
I have an object, dbReader, that I get a warning saying that it is used before it has been assigned a value.
That is correct.
Dim dbReader As SqlDataReader
and later:
Finally If (Not dbReader Is Nothing) Then dbReader.Close() End Try
Why is this an error?
Here I am testing to see if it is nothing, which it is (isn't it?) before using it. The error is in the "if" test not the dbReader.Close().
Thanks,
Tom
Tshad,
I have tried your code, not any problem.
\\\\
Dim dbReader As System.Data.SqlClient.SqlDataReader
If (Not dbReader Is Nothing) Then dbReader.Close()
///
Are you sure that the declaration of the datareader is global or in the same
method?
(And not in the try block by instance)
Cor
"tshad" <tf*@dslextreme.comschreef in bericht
news:OG**************@TK2MSFTNGP02.phx.gbl...
In VS 2008,
I have an object, dbReader, that I get a warning saying that it is used
before it has been assigned a value.
That is correct.
Dim dbReader As SqlDataReader
and later:
Finally
If (Not dbReader Is Nothing) Then dbReader.Close()
End Try
Why is this an error?
Here I am testing to see if it is nothing, which it is (isn't it?) before
using it. The error is in the "if" test not the dbReader.Close().
Thanks,
Tom
"Göran Andersson" <gu***@guffa.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Herfried K. Wagner [MVP] wrote:
>In VB the statement 'Dim x As Foo' is semantically equal to 'Dim x As Foo = Nothing' because VB implicitly initializes variables of reference types with 'Nothing'.
Not exactly. The variable is initialsed when the method starts, not where
the variable is declared. Consider:
For i As Integer = 1 to 10
Dim x As Integer
If i = 6 Then
x = 10
End If
Console.WriteLine(x)
Next
This would output:
0
0
0
0
0
10
10
10
10
10
So you are saying in the above case it is initialized to 0 each time and
once set it stays set, but not in the one below?
Not sure I understand that. I would have thought that in both cases x would
be initialized to 0.
Thanks,
Tom
If you change the line to:
Dim x As Integer = 0
It would output:
0
0
0
0
0
10
0
0
0
0
The warning is clearly intended to prevent situations like these.
--
Göran Andersson
_____ http://www.guffa.com
On Fri, 10 Oct 2008 15:22:51 -0700, "tshad" <tf*@dslextreme.com>
wrote:
> "Göran Andersson" <gu***@guffa.comwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
>Herfried K. Wagner [MVP] wrote:
>>In VB the statement 'Dim x As Foo' is semantically equal to 'Dim x As Foo = Nothing' because VB implicitly initializes variables of reference types with 'Nothing'.
Not exactly. The variable is initialsed when the method starts, not where the variable is declared. Consider:
For i As Integer = 1 to 10 Dim x As Integer If i = 6 Then x = 10 End If Console.WriteLine(x) Next
This would output:
0 0 0 0 0 10 10 10 10 10
So you are saying in the above case it is initialized to 0 each time and once set it stays set, but not in the one below?
Not sure I understand that. I would have thought that in both cases x would be initialized to 0.
Thanks,
"Dim x As Integer" initializes x to zero when the method is entered.
The Dim statement does not change the value of x each time through the
loop.
"Dim x As Integer = 0" sets x to zero each time through the loop. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Daniel |
last post by:
Hello =)
I have an object which contains a method that should execute every x ms. I
can use setInterval inside the object construct like this -
self.setInterval('ObjectName.methodName()',...
|
by: sneill |
last post by:
How is it possible to take the value of a variable (in this case,
MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name?
In the following example I want 'oIcon' object to have...
|
by: Luke Matuszewski |
last post by:
Here are some questions that i am interested about and wanted to here
an explanation/discussion:
1. (general) Is the objectness in JavaScript was supported from the
very first version of it (in...
|
by: yb |
last post by:
Hi,
Is there a standard for the global 'window' object in browsers? For
example, it supports methods such as setInterval and clearInterval, and
several others.
I know that w3c standardized...
|
by: Andrew Poulos |
last post by:
With the following code I can't understand why this.num keeps
incrementing each time I create a new instance of Foo. For each instance
I'm expecting this.num to alert as 1 but keeps incrementing.
...
|
by: satyajit |
last post by:
I am trying to learn the concept of constructors in ECMAScript. I
executed following code (See execution in Rhino JavaScript shell):
function Foo(a)
{
this.a = a;
}
function Bar(b)
{
this.b...
|
by: User1014 |
last post by:
A global variable is really just a property of the "Global Object", so
what does that make a function defined in the global context? A method
of the Global Object?
...
|
by: alex |
last post by:
I am so confused with these three concept,who can explained it?thanks
so much?
e.g.
var f= new Function("x", "y", "return x * y");
function f(x,y){
return x*y
}
var f=function(x,y){
|
by: Ralph |
last post by:
Hi
I don't understand why it's not working:
function schedule(imTop){
this.tdImagesTop = imTop;
}
schedule.prototype.selectEl = function() {
alert(this.tdImagesTop);
|
by: vishnu |
last post by:
Hi,
I am working on asp.net project which I converted the code fron VB
to C# and instead of RaiseEvent in VB code I used the following code.
using System;
using System.Data;
using...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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,...
| |