How to write an Add-in to find the useless variables


How to write an Add-in or anything to find
1. the declared variables never be used in the program,
2. the variables assigned value but never be used,
3. anything similar you think we should do.
Who is Participating?
Sage020999Connect With a Mentor Commented:
After you fiqure out how to write an add-in, you will need to use the active code panes to search for any Dimmed varibles.  You will need some sort of function that determines if the varible is a field name, varible name, comment, or a function call.  The comments are the easiest to solve.  Any lines beginning with the single quote ', you would ignore. Any Varibles following the DIM statement you would want to stick in an array.  It is important to remember scoping here.  You will have the potential of having a Public varible named the same as private varibles.  Once you have your array, you will want to search for the whole word that matches the varible name.  This will exclude field names which are either formatted ('Field'),!Field, or [Field].  Everything else is a varilbe being used.  You then want to have a counter to count the times the varible is found within the scope.  Once you find it once, you can move to the next varible.  This is not an easy task and will take some time.  These are one of those tasks that cost more to do than purchase, but would be rewarding after the completion of the project.  Good Luck. I hope this helps a little.  -Sage.  This is an interesting topic, I hope you receive some feed back from people who have done simular tasks.  I have created multiple clip boards, modified text, but as far as searching for unused varibles, I purchased VBCompress Pro.
Thats a biggie - I suspect you might need to offer more than 70 pts.

Keep your eyes peeled on - I'm just completing a tool to do that among other things - I'll post the source when finished !
Here is the site I posted for you yesterday on your other question.  I think that this will be the simplest way:    hope this helps.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Creating an add-in is the straightforward bit. Determining what has or hasnt been used is a different kettle of fish entirely !
Could be done as an add-in or as a stand alone project.  As a stand alone, you would use the project file to find out which files to parse and then do what Sage says above.  

Also, you might want to consider creating a call tree of routines to find dead code.  Variables used in dead code are not really used.

Basically, you would need to trace any routine (Sub, Function or Properties) called from startup modules and/or user-interface and follow any routines called from those etc.

Then any routines that are left over could be flagged as dead and displayed in a list to the user.

This is a big project, good luck!

You'll also need to keep track of functions with the same name as variables, variables declared with type identifiers (!, %, $, #), multiple variables dimensioned on single lines etc,etc etc

I have an application developed in-house and if I can persuade the author to let me publish I'll do so, but its too large to publish here. I'll try and post it on a web site somewhere  (as I understand its against house-rules to e-mail answers to individuals).
username1Author Commented:

Thank you for all experts who gave me usefull suggestions.

This is not as easy as I thought....if VB7 or VB2000 wont do this and I have spare time, I will try :-)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.