Scrub Out Unused Constants

Low-level parsing in VB doesn’t come up every day. This routine demonstrates how to search a VB module for dead module-level constants with only a few lines of code. Reading the entire contents of a file into a string variable is key to this approach:

 Private Sub FindDeadConstants(ByVal _	PathAndFile As String)	Dim FileHandle As Integer	Dim FileContents As String	Dim PositionOfDeclaration As Long	Dim StartOfConstantName As Long	Dim EndOfConstantName As Long	Dim ConstantName As String	'open the file and read the contents 	'into a string variable:	FileHandle = FreeFile	Open PathAndFile For Binary Access _		Read As FileHandle	FileContents = _		Input$(LOF(FileHandle), _		FileHandle)	Close FileHandle	'loop through all the module-level 	'constants:	Do		PositionOfDeclaration = _			InStr(PositionOfDeclaration _			+ 1, FileContents, _			"Private Const ")		If PositionOfDeclaration > 0 Then			'we've found a constant:			StartOfConstantName = _				PositionOfDeclaration _				+ Len("Private Const ")			EndOfConstantName = _				InStr( _				StartOfConstantName, _				FileContents, " ")			ConstantName = _				Mid$(FileContents, _				StartOfConstantName, _					EndOfConstantName _					- StartOfConstantName)			'if the constant is not 			'referenced beyond its 			'declaration, then it's dead:			If InStr(EndOfConstantName, _				FileContents, _				ConstantName) = 0 Then				lstDeadConstants.AddItem _					ConstantName			End If		End If	Loop Until PositionOfDeclaration = 0End Sub
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS