none
Why is this variable being reported as unused, its clearly been used? RRS feed

  • Question

  • look at this screenshot (visual studio 2013)

    I am getting a warning "warning BC42024: Unused local variable: 'dtUser'."

    What am I missing?

    Thanks.


    Ab

    Friday, September 21, 2018 2:53 PM

All replies

  • It needs initialized. (clearly it is not being used)

    IE:

    Dim dtuser As New DataTable

    or

    Dim dtuser as datatable = nothing

    or later in code...

    dtuser = New Datatable

    or

    dtuser = some initiated datatable

    or maybe in your case

    Dim dtuser as datatable = comFun.blablabla



    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi





    • Edited by Gtripodi Friday, September 21, 2018 3:14 PM
    Friday, September 21, 2018 3:09 PM
  • Its initialized right on the next line, there is nothing wrong with that. If I had not used it at all OR if I used one of its properties without every initializing it, then that error message would have been right, there is something wrong here.

    Ab

    Friday, September 21, 2018 7:55 PM
  • Intellesense is obviously not diggin the way youre initializing it.

    Hard telling...

    What is going on with comFun.blahblahblah?

    Is it a function that may not be returning a datatable on all paths?


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, September 21, 2018 8:03 PM
  • I just checked its a simple 2 lines functions its returning a datatable.

    Ab

    Friday, September 21, 2018 8:36 PM
  • Have you tried it like this?

    Dim dtuser as datatable = comFun.blablabla

    Also, why not provide the function code?


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, September 21, 2018 9:00 PM

  • I am getting a warning "warning BC42024: Unused local variable: 'dtUser'."


    What happens in your code if the conditional is not true?

    Does dtUser get used if that path is followed through your program's 
    logic? If the compiler sees a path that *might* be followed where
    dtUser is never used (after having been given a value) then it will
    issue that message.

    - Wayne

    Friday, September 21, 2018 9:17 PM
  • I can't reproduce the warning with VS 2015.

    Likely just a bug in VS 2013.



    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Friday, September 21, 2018 11:55 PM

  • I am getting a warning "warning BC42024: Unused local variable: 'dtUser'."


    What happens in your code if the conditional is not true?

    The conditional itself is evaluating the variable that has the erroneous warning, so it is certainly being used.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Friday, September 21, 2018 11:57 PM
  • I am curious what your 2 line function that accepts command text as an argument looks like


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Saturday, September 22, 2018 12:21 AM
  • I can't reproduce the warning with VS 2015.

    Likely just a bug in VS 2013.



    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    i cant produce it in 2012 either

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Saturday, September 22, 2018 12:22 AM
  • This is what I'd expect.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by tommytwotrain Saturday, September 22, 2018 1:58 PM
    Saturday, September 22, 2018 12:50 AM
    Moderator
  • This is what I'd expect.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    I do not get that result


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Saturday, September 22, 2018 2:08 AM
  • Question still stand, what do you do with sql command in a 2 line function...

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Saturday, September 22, 2018 2:18 AM
  • It is what it is. Visual Stuido using C# does the same thing. You know it's being used so just ignore the warning.
    Saturday, September 22, 2018 10:34 PM
  • sorry for late reply, I had found out what was happening, actually I asked a pretty bogus question (sorry for that), since the function was doing a Return earlier in the code (and I am pretty sure I am dyslexic to some degree), I had completely overlooked the Return statement. The code below it was all unreachable code, in unreachable code your variables are essentially unused even when you can see them being used, logic being that code will never be executed, but since the variables exist, its there and not being used.

    Thanks for all who replied. Sorry again!


    Ab

    Thursday, September 27, 2018 11:12 AM