-
Apr 27th, 2021, 05:18 AM
#1
Thread Starter
PowerPoster
Annoying entries in 'Errors' list.
Hi,
Yes, I know it's only a warning, but it's still annoying to have anything in the 'Errors' list.
I don't like to see anything at all in my errors list, I'm aware of it and try to keep it clear so that I can spot a new entry in there.
I expect we all get 'Unused variable' warnings, so when I get one I generally add a line to initialise it.
So, somewhere in my code I might have...
Code:
Dim total As Integer
...and because I know 'total' is zero, later in the subroutine I might use...
Code:
If ..... Then
total += 1
...and I get
'Variable 'total' is used before it has been assigned a value'
So, I add...
Code:
Dim total As Integer = 0
But THEN I get...
Unnecessary assignment of a value to 'total'
...So annoying !
I don't suppose there's anything I can do about it.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Apr 27th, 2021, 06:21 AM
#2
Re: Annoying entries in 'Errors' list.
This may be the "why" behind it:
https://www.sololearn.com/Discuss/21...efinedvariable
Modern IDEs often recommend changes that reflect usual coding conventions, best practices and preferred coding styles, even if they have no impact on the result. Because they assume that someone else will read your code and these other coders expect the same consistency that is used everywhere.
_ usually denotes a throwaway value that you don't need and don't use anywhere else. For example if you don't print x, then why do you assign it?
I am not sure if your warning is related to something like this; anyway you can probably turn off these kind of warnings somehow if it bothers you
Please remember next time...elections matter!
-
Apr 27th, 2021, 07:38 AM
#3
Re: Annoying entries in 'Errors' list.
Not all datatypes have a default value.
Integer default = 0
String default = ""
Point default = Nothing not the Point.Empty you'd expect
It's good practice to initialise variables...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 27th, 2021, 11:03 AM
#4
Re: Annoying entries in 'Errors' list.
Value types MUST have a default value. They can't very well NOT have one. A variable of a reference type just holds the reference, which is essentially the address of the actual object off in memory. So, if the object isn't created, then the reference can be to nowhere. That's not the case with value types. The variable holds the thing itself, so it is some set of bytes that the thing occupies, and those bytes can't be empty (empty is just 0).
For this reason, I don't believe that the example should happen, and having tested it, I am not seeing that behavior. If total is a reference type, then it makes sense. If I write that exact example in VS2019, with some meaningless something in the If condition, then I don't get the warning you are reporting.
I also disagree with what .Paul. is saying about Point:
Code:
Dim pt As Drawing.Point
if pt= Point.Empty Then 'This evaluates to True.
So, there is something odd about this thread.
Is Total not really an integer?
Are you using some kind of third-party code analysis tool?
Is Point not a Drawing.Point?
My usual boring signature: Nothing
-
Apr 27th, 2021, 11:53 AM
#5
Re: Annoying entries in 'Errors' list.
Yeah, I agree with SH, there most be something else going on here.
Code:
Dim total As Integer
If total = 0 Then
MessageBox.Show("Zero")
End If
No error listed
-
Apr 27th, 2021, 12:08 PM
#6
Re: Annoying entries in 'Errors' list.
Also not all warningσ must be eliminated.
For example I'm having a WCF connection to severαλ servers, I do not initialize the connections to the servers at first because they take a while to do so and the program is slowing down. I only initialize when a specific WCF connection needs to call a server, so I get a warning that the variables are not initialized but that is my intention.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 27th, 2021, 07:09 PM
#7
Re: Annoying entries in 'Errors' list.
Originally Posted by Shaggy Hiker
I also disagree with what .Paul. is saying about Point:
Code:
Dim pt As Drawing.Point
if pt= Point.Empty Then 'This evaluates to True.
So, there is something odd about this thread.
Is Total not really an integer?
Are you using some kind of third-party code analysis tool?
Is Point not a Drawing.Point?
Shows what i know But, i tend to initialise variables...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 27th, 2021, 08:11 PM
#8
Thread Starter
PowerPoster
Re: Annoying entries in 'Errors' list.
Originally Posted by .paul.
Not all datatypes have a default value.
Integer default = 0
String default = ""
Point default = Nothing not the Point.Empty you'd expect
It's good practice to initialise variables...
That's what I'm complaining about... We know that...
Code:
Dim total As Integer
...at this point 'total' has no value, so setting it to zero is an 'Unnecessary assignment of a value'.
But if I don't assign it and then increment it, in a For-To loop say, I get the other 'error' from intellisense ''Variable is used before it has been assigned a value'. It's automatically assigned a value when it's defined... it's zero !
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Apr 27th, 2021, 08:15 PM
#9
Thread Starter
PowerPoster
Re: Annoying entries in 'Errors' list.
Originally Posted by TysonLPrice
anyway you can probably turn off these kind of warnings somehow if it bothers you
That's what I'm asking... HOW ?
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Apr 27th, 2021, 08:21 PM
#10
Re: Annoying entries in 'Errors' list.
IMO some thing should be initialized.
Imaging switching to a language the integer is null at first of boolean show to false.
But that's just an observation.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 27th, 2021, 08:31 PM
#11
Re: Annoying entries in 'Errors' list.
But if I don't assign it and then increment it, in a For-To loop say, I get the other 'error' from intellisense ''Variable is used before it has been assigned a value'.
Code:
Dim total As Integer
For i = 5 To 10
total += 1
Next
I get no error, I'm using 2019 but I don't think that matters.
-
Apr 27th, 2021, 08:36 PM
#12
Re: Annoying entries in 'Errors' list.
I can't try now as office con is off but, does it show with either option strict on and off?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 27th, 2021, 08:39 PM
#13
Re: Annoying entries in 'Errors' list.
This will cause that error
Code:
Dim total
For i = 5 To 10
total += 1
Next
This solves the error
Code:
Dim total = 0
For i = 5 To 10
total += 1
Next
-
Apr 27th, 2021, 08:42 PM
#14
Re: Annoying entries in 'Errors' list.
Yes but I guess this would impress me if it didn't show a warning.
I guess Dim total as object shouldn't cause a warning either. Not sure if it does cuz again , can't check, but...
Edit, Dim total = 0 should cause a warning also. Are you sure it doesn't with option strict on?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 27th, 2021, 08:47 PM
#15
Re: Annoying entries in 'Errors' list.
Originally Posted by .paul.
Not all datatypes have a default value.
Integer default = 0
String default = ""
Point default = Nothing not the Point.Empty you'd expect
It's good practice to initialise variables...
The default value for any variable of any type is Nothing and Nothing does have a corresponding value for EVERY value type. String is not a value type so the default for a String variable is no object, just like every other reference type. The reason that this may not be obvious is that the String class supports value equality as well as reference equality, with value equality being the default. That means that using the standard equality operator to compare Nothing and String.Empty will return True. That doesn't mean that a String variable is String.Empty by default though. It is still Nothing, i.e. no object, but that is considered to be a value equal to an empty String, for obvious reasons.
vb.net Code:
Dim str1 As String
Dim str2 As String = Nothing
Dim str3 As String = String.Empty
Console.WriteLine(str1 = str2) 'Value equality: True
Console.WriteLine(str1 = str3) 'Value equality: True
Console.WriteLine(str2 = str3) 'Value equality: True
Console.WriteLine(str1 Is str2) 'Reference equality: True
Console.WriteLine(str1 Is str3) 'Reference equality: False
Console.WriteLine(str2 Is str3) 'Reference equality: False
Console.ReadLine()
-
Apr 27th, 2021, 09:37 PM
#16
Re: Annoying entries in 'Errors' list.
Maybe post the actual code, because as SH said this shouldn't be happening.
-
Apr 28th, 2021, 07:57 AM
#17
Thread Starter
PowerPoster
Re: Annoying entries in 'Errors' list.
Originally Posted by wes4dbt
Code:
Dim total As Integer
For i = 5 To 10
total += 1
Next
I get no error, I'm using 2019 but I don't think that matters.
Ah !
Yes...
Try it like this though:
Code:
Dim total As Integer, txt As string
If txt = "Fred" Then
For i = 5 To 10
total += 1
Next
End If
Sorry, I didn't remember to include that part in the example.
'txt' isn't likely to cause an error report, but 'total' should.
Poppa
Last edited by Poppa Mintin; Apr 28th, 2021 at 08:04 AM.
Along with the sunshine there has to be a little rain sometime.
-
Apr 28th, 2021, 08:00 AM
#18
Re: Annoying entries in 'Errors' list.
Originally Posted by Poppa Mintin
Ah !
Yes...
Try it like this though:
Code:
Dim total As Integer, txt As string
If txt = "Fred" Then
For i = 5 To 10
total += 1
Next
End If
Sorry, I didn't remember to include that part in the example.
Poppa
That seems like a valid warning to me. I would want to know if I was looking for a variable value that was never set. Is that what it is about?
Please remember next time...elections matter!
-
Apr 28th, 2021, 08:07 AM
#19
Thread Starter
PowerPoster
Re: Annoying entries in 'Errors' list.
Originally Posted by TysonLPrice
That seems like a valid warning to me. I would want to know if I was looking for a variable value that was never set. Is that what it is about?
Yes, a valid warning, but a nuisance nonetheless.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Apr 28th, 2021, 08:08 AM
#20
Re: Annoying entries in 'Errors' list.
Originally Posted by Poppa Mintin
Yes, a valid warning, but a nuisance nonetheless.
Poppa
It wouldn't be for you but someone coming behind you. As posted that code is useless.
Last edited by TysonLPrice; Apr 28th, 2021 at 01:11 PM.
Please remember next time...elections matter!
-
Apr 28th, 2021, 09:32 AM
#21
Re: Annoying entries in 'Errors' list.
Originally Posted by Poppa Mintin
Ah !
Yes...
Try it like this though:
Code:
Dim total As Integer, txt As string
If txt = "Fred" Then
For i = 5 To 10
total += 1
Next
End If
Sorry, I didn't remember to include that part in the example.
'txt' isn't likely to cause an error report, but 'total' should.
Poppa
I took that exact code and pasted it into a project. I got that warning for txt, because you never set txt to anything, so it is Nothing at the time of the If statement. I remedied that by setting txt to "". However, I get no warning for total, which is what I expected.
In other words, this:
Code:
Dim total As Integer, txt As String = ""
If txt = "Fred" Then
For i = 5 To 10
total += 1
Next
End If
Shows no errors or warnings in VS2019 with pretty vanilla settings. So, if you ARE getting such a warning or error on that code, you must have something set that I do not, or vice versa.
My usual boring signature: Nothing
-
Apr 28th, 2021, 12:14 PM
#22
Thread Starter
PowerPoster
Re: Annoying entries in 'Errors' list.
Maybe...
I have Option Strict 'ON' by default.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Apr 28th, 2021, 12:17 PM
#23
Re: Annoying entries in 'Errors' list.
@sapator - you should still explicitly initiaiize to nothing just to be safe imho. (post #6) In that case warnings can be fullly turned on. As they should.
-
Apr 28th, 2021, 01:44 PM
#24
Re: Annoying entries in 'Errors' list.
Originally Posted by Poppa Mintin
Maybe...
I have Option Strict 'ON' by default.
Poppa
So do I, so it can't be that.
As you noted, this shouldn't be happening. Integer DOES have a value, regardless of whether or not you set a value for it. If you set no value, then it is 0, so you shouldn't be getting that warning, and I do not.
Do you have any code analysis tools that could be doing this? I find that the code analysis built into VS can be overly sensitive. It may also be that there exists a setting that covers this in the VAST array of settings available in modern VS (which might be a bit too much, in my opinion). If there is such an option, I have no idea what it is.
My usual boring signature: Nothing
-
Apr 28th, 2021, 02:00 PM
#25
Re: Annoying entries in 'Errors' list.
Originally Posted by Poppa Mintin
Ah !
Yes...
Try it like this though:
Code:
Dim total As Integer, txt As string
If txt = "Fred" Then
For i = 5 To 10
total += 1
Next
End If
Sorry, I didn't remember to include that part in the example.
'txt' isn't likely to cause an error report, but 'total' should.
Poppa
Again, as SH stated, no error on "total", warning on "txt". Option Strict on.
-
Apr 28th, 2021, 02:33 PM
#26
Re: Annoying entries in 'Errors' list.
I don't have the warning on "total" with VB2010 and 2017 too. I usually has this kind of warning with string variable.
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Apr 29th, 2021, 06:31 AM
#27
Thread Starter
PowerPoster
Re: Annoying entries in 'Errors' list.
Originally Posted by Shaggy Hiker
Do you have any code analysis tools that could be doing this? I find that the code analysis built into VS can be overly sensitive. It may also be that there exists a setting that covers this in the VAST array of settings available in modern VS (which might be a bit too much, in my opinion). If there is such an option, I have no idea what it is.
I've never used Analyze but as you mention it I gave it a 'look-see'.
Analyze > Run Code Analysis on (Project name) : No apparent response.
So I had a little play, looked at 'Code Cleanup' > 'Configure Code Cleanup'
I found that there are two 'Profiles' and they are both the same, and both configurable.
Didn't change profile 1, added 'Remove unused variables' to profile 2.
I have an 'Unnecessary assignment' error and a 'used before it has been assigned a value' error.
I ran: Code Cleanup Profile 1. No change to code or warnings.
I ran: Code Cleanup Profile 2. Still no change to code or warnings.
I added an unused variable to my code and now also have an 'Unused variable' error.
I ran: Code Cleanup Profile 2. Still no change to code or warnings. I'd hoped it'd actually remove the unused variable.
So, I don't believe I've actually learned anything from the exercise.
However, I usually have the Error List 'Box' shunted down quite narrow to reduce it's interference with reading code, and upon stretching it out I did (Re-discover) that the 'Errors' I've been getting are actually Errors, Warnings or Messages.
The Error List has a tab for each, clicking these tabs turns the relevant Error, Warning or Message off.
Fine, I can stop seeing errors but sadly clicking (say) Warnings totally stops the warnings, it doesn't show any new ones, so a bit pointless.
Shame really, I thought I'd found the answer.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Apr 29th, 2021, 10:59 AM
#28
Re: Annoying entries in 'Errors' list.
Sure you have. Negative results are still results.
I'm not sure what is happening there. I can only assume that there is some setting buried on the myriad pages of options, but after taking a look at the most likely places, I don't see anything that looks like it might address this.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|