none
About unused variables RRS feed

  • Question

  • I don't understand what happened here in the forum?? Under forum category no mention of Visual Basic 10???  What happened to that forum it was here not to long ago??

    Hi I have been writing an application over the past year and have finally finished it so I want to clean it up.  Overtime through development I have declared a lot of variables and through time some of them are no longer needed.  Is there a quick way that I can get a list of variables that I don't use anymore in the app?? 

    Thanks,

    Les

    Thursday, May 3, 2018 4:08 AM

Answers

  • Hi les2011,

    Here is the thread about removing unused variables, you can take a look:

    https://stackoverflow.com/questions/2492398/removing-unused-local-variables-from-net-code

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Les2011 Wednesday, May 9, 2018 6:41 PM
    Thursday, May 3, 2018 8:30 AM
    Moderator
  • V in my case that's not the story since when I declare my variables I assign them a default value so as long as the compiler sees that it thinks I am using the variable.


    Then run a Code Analysis with the configuration set to "Microsoft All Rules".  Then just look through the results for instances of CA1804 in the code column.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Les2011 Wednesday, May 9, 2018 6:41 PM
    Thursday, May 3, 2018 10:05 PM
    Moderator
  • Hi Cherry,

    Thx for your help.  When I declare a variable I also assign a default value to it so I think your suggestion using the utility wont work.  I think once it sees its been assigned as ie Dim xyz as integer=0 it will not delete it or note it.  Am I right Sherry or perhaps you can explain it a little clearer for me.

    Thx  Les

    https://docs.microsoft.com/en-us/visualstudio/code-quality/code-analysis-for-managed-code-overview

    (see my previous reply; with the right ruleset it will find the unused variables that were initialized but then never referenced)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Les2011 Wednesday, May 9, 2018 6:46 PM
    Friday, May 4, 2018 12:05 PM
    Moderator

All replies

  • Probably you have used the old style of programming to create in top of your program all the variables (a memory map). In past computers could not do it in an other way. Avoid that. If you declare variables which you only need in a method in that method, than is what you currently see not the only thing which is solved. 

    However, a quick and dirty way if you have done it like I wrote. 

    Comment out all global variables and see where you get errors and comment them again in. 

    If your program is then error free you can remove all the outcommented variables. 


    Success
    Cor

    Thursday, May 3, 2018 6:12 AM
  • The unused local variables are reported in Errors window (if Warnings button is selected) after each re-build.

    Thursday, May 3, 2018 6:56 AM
  • Hi les2011,

    Here is the thread about removing unused variables, you can take a look:

    https://stackoverflow.com/questions/2492398/removing-unused-local-variables-from-net-code

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Les2011 Wednesday, May 9, 2018 6:41 PM
    Thursday, May 3, 2018 8:30 AM
    Moderator
  • Hi Cor,

    You are right about the variables placed at the top of each section.  I usually don't use global in the sense that all variables are visible to all subs and functions but they are placed at the top of all of them etc.  What you suggest is basically commenting out all the dim stmnts and then go about the reported errors by uncommenting them one by one ??  I think that idea you suggest will work but was hoping there was an easier way.  Let me do some research and will provide you feedback. Thx Cor

    Les

    Thursday, May 3, 2018 9:24 PM
  • V in my case that's not the story since when I declare my variables I assign them a default value so as long as the compiler sees that it thinks I am using the variable.

    Thursday, May 3, 2018 9:26 PM
  • Hi Cherry,

    Thx for your help.  When I declare a variable I also assign a default value to it so I think your suggestion using the utility wont work.  I think once it sees its been assigned as ie Dim xyz as integer=0 it will not delete it or note it.  Am I right Sherry or perhaps you can explain it a little clearer for me.

    Thx  Les

    Thursday, May 3, 2018 9:29 PM
  • V in my case that's not the story since when I declare my variables I assign them a default value so as long as the compiler sees that it thinks I am using the variable.


    Then run a Code Analysis with the configuration set to "Microsoft All Rules".  Then just look through the results for instances of CA1804 in the code column.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Les2011 Wednesday, May 9, 2018 6:41 PM
    Thursday, May 3, 2018 10:05 PM
    Moderator
  • Visual Basic changes revisions every so often so if there was a Forum for Visual Basic 10 which I never knew about it would have to be altered or else there would have to be forums for every .Net version of Visual Basic I suppose.

    La vida loca

    Friday, May 4, 2018 3:26 AM
  • Hi Cor,

    You are right about the variables placed at the top of each section.  I usually don't use global in the sense that all variables are visible to all subs and functions but they are placed at the top of all of them etc.  What you suggest is basically commenting out all the dim stmnts and then go about the reported errors by uncommenting them one by one ??  I think that idea you suggest will work but was hoping there was an easier way.  Let me do some research and will provide you feedback. Thx Cor

    Les

    Les,

    What is easier. That is probably depending who it does. I never take the time to search 2 days to a solution for something I do only one time, while I can do something in 30 minutes. If you try what I told, I guess you have already almost fixed it. You get an error listing at the bottom so you have to click on it. Go to the variable, uncomment it an run. Then you can do the next.  


    Success
    Cor


    Friday, May 4, 2018 10:13 AM
  •  while I can do something in 30 minutes. 


    Why not let the analyzer do the work in 30 seconds?

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Friday, May 4, 2018 11:14 AM
    Moderator
  •  while I can do something in 30 minutes. 


    Why not let the analyzer do the work in 30 seconds?

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Read my reply Reed, not only the last sentence, I was talking about 2 investigation days for a one time job. 

    In an analogy: Currently I take a car even for short distances. But when I was 8, I was walking for going on bicycle, because I did not know how to drive a car. 



    Success
    Cor



    Friday, May 4, 2018 11:26 AM
  • Hi Cherry,

    Thx for your help.  When I declare a variable I also assign a default value to it so I think your suggestion using the utility wont work.  I think once it sees its been assigned as ie Dim xyz as integer=0 it will not delete it or note it.  Am I right Sherry or perhaps you can explain it a little clearer for me.

    Thx  Les

    https://docs.microsoft.com/en-us/visualstudio/code-quality/code-analysis-for-managed-code-overview

    (see my previous reply; with the right ruleset it will find the unused variables that were initialized but then never referenced)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Les2011 Wednesday, May 9, 2018 6:46 PM
    Friday, May 4, 2018 12:05 PM
    Moderator
  • Ok Reed I will give it a try

    Les

    Saturday, May 5, 2018 3:14 AM
  • Hi Cor,

    I had no intention to challenge your suggestion but wanted to know if I set the variable I declared the compiler might not see it but I don't know the answer to that until I try it which I am aiming to do tonight.

    Thanks

    Les

    Saturday, May 5, 2018 3:16 AM
  • Hi Les2011,

    Anything update? Please share your result here. If you still have some issue, please provide new error message or detailed info here. If you have resolved your issue, please remember to close your thread by marking the helpful post as answer.

    It is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 8, 2018 8:12 AM
    Moderator
  • Hi Cherry,

    I try to remember to do that all the time.  I have had some medical issues and have not checked out some of the suggestions until now.

    Thank you,

    Les

    Wednesday, May 9, 2018 6:42 PM
  • Hi MB,

    No what I meant by my original remark was that coming to this forum initially wanted you to select a class for the help a person wanted and in the past it provided you a list of Windows... Visual Basic.net....Networking etc  and it no longer lists anything about programming or Visual Basic so I eventually found this site as you can see.  I was basically just curious if anyone saw theses changes <S>

    Les

    Wednesday, May 9, 2018 6:45 PM