|
-
Sep 13th, 2024, 06:32 AM
#1
Thread Starter
Lively Member
Identifying Unused Variables in VB.NET Using Visual Studio
I am working on a VB.NET project in Visual Studio 2017 and need to clean up my code by identifying unused variables. I would like Visual Studio to automatically highlight or notify me of any variables that are declared but not used in the code. For example, if I declare a variable like
Dim tempValue As String = ""
but do not use 'tempValue', I want to be alerted to this. I have tried configuring similar settings for C# by adjusting the Code Style --> Var Preferences--> warning , to show warnings for unused variables, but I found that this specific setting is not available for VB.NET.
Are there configurations or alternative methods available to achieve this in VB.NET that can enable warnings or errors for unused variables for visual studio 2017 or higher versions?"
-
Sep 13th, 2024, 08:38 AM
#2
Re: Identifying Unused Variables in VB.NET Using Visual Studio
Visual Studio 2022 does report on this in the error list (by default it is classed as a "message"). You might be able to configure this through a .editorconfig file - never tried for VB however.
-
Sep 13th, 2024, 10:44 AM
#3
Re: Identifying Unused Variables in VB.NET Using Visual Studio
VS will report this if the variable is truly not used, but your example circumvents that. When you assign something to a variable, as you are with:
Code:
Dim tempValue As String = ""
then VS will assume you know what you are doing and not alert you that the variable is not otherwise used. If you had simply written:
Code:
Dim tempValue As String
then VS would have alerted you that the variable was never used. That assignment foiled your intentions and did nothing at all.
So, VS will alert you if the variable is TOTALLY not used, but will not alert you if the variable is totally worthless. After all, you could write a method like this:
Code:
Public Sub Foo()
Dim tempVal As String
For x=0 to 100
tempVal = ""
Next
End Sub
The variable tempVal is used, but is worthless, as it isn't used in any meaningful way. The whole method accomplishes nothing other than wasting a bit of time, which could be just as easily wasted without tempVal. Still, VS won't tell you about the worthlessness of the variable.
MS made a choice as to how much to alert you to and how much to leave up to you. By adding that = "" onto the declaration, you made it so that the variable is over the line that MS drew, and the variable is not recognized as not being used.
My usual boring signature: Nothing
 
-
Sep 19th, 2024, 04:23 AM
#4
Thread Starter
Lively Member
Re: Identifying Unused Variables in VB.NET Using Visual Studio
In Visual Studio 2017, under Build and Run > Code Style > Var Preferences > Warning, I was able to set a warning for unused variables in C#. Are there similar settings for VB in Visual Studio 2017 or later versions?
-
Sep 19th, 2024, 04:38 AM
#5
Re: Identifying Unused Variables in VB.NET Using Visual Studio
As I said above, in VS 2022 an unused variable does get reported in the Error window. You need to make sure it is showing Messages as well as Errors and Warnings to see it though.
Tags for this Thread
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
|