Results 1 to 28 of 28

Thread: Annoying entries in 'Errors' list.

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,068

    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.

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,541

    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!

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,974

    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...

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,153

    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

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,683

    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

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,954

    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.
    .

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,974

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by Shaggy Hiker View Post
    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...

  8. #8

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,068

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by .paul. View Post
    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.

  9. #9

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,068

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by TysonLPrice View Post
    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.

  10. #10
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,954

    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.
    .

  11. #11
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,683

    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.

  12. #12
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,954

    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?
    .

  13. #13
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,683

    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

  14. #14
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,954

    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?
    .

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,907

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by .paul. View Post
    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:
    1. Dim str1 As String
    2. Dim str2 As String = Nothing
    3. Dim str3 As String = String.Empty
    4.  
    5. Console.WriteLine(str1 = str2) 'Value equality: True
    6. Console.WriteLine(str1 = str3) 'Value equality: True
    7. Console.WriteLine(str2 = str3) 'Value equality: True
    8.  
    9. Console.WriteLine(str1 Is str2) 'Reference equality: True
    10. Console.WriteLine(str1 Is str3) 'Reference equality: False
    11. Console.WriteLine(str2 Is str3) 'Reference equality: False
    12.  
    13. Console.ReadLine()

  16. #16
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,683

    Re: Annoying entries in 'Errors' list.

    Maybe post the actual code, because as SH said this shouldn't be happening.

  17. #17

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,068

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by wes4dbt View Post
    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.

  18. #18
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,541

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by Poppa Mintin View Post
    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!

  19. #19

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,068

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by TysonLPrice View Post
    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.

  20. #20
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,541

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by Poppa Mintin View Post
    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!

  21. #21
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,153

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by Poppa Mintin View Post
    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

  22. #22

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,068

    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.

  23. #23
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,343

    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.

  24. #24
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,153

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by Poppa Mintin View Post
    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

  25. #25
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,683

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by Poppa Mintin View Post
    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.

  26. #26
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    625

    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)

  27. #27

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,068

    Re: Annoying entries in 'Errors' list.

    Quote Originally Posted by Shaggy Hiker View Post
    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.

  28. #28
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,153

    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
  •  



Click Here to Expand Forum to Full Width