Results 1 to 32 of 32

Thread: Declaring Variables

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    558

    Declaring Variables

    OK guys... confession time. I didn't put an "Option Explicit" in my program. 20 years ago. <hangs head in shame> Now I've got this monster program with over 250,000 lines of code and a couple hundred forms. It's way too much work to go back and declare all my variables manually. But for sure every time I find a crazy bug it's because of some mis-typed variable that would be caught automatically if I had just done an Option Explicit way back in the day and declared all my darn variables.

    So I want to declare.

    But I wonder if there's some program or utility that could give me a hand with the project? Any ideas?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Declaring Variables

    Egads, even if you can use a tool or some parser, you'd have to trust it 100% else it could do more harm than good if it also creates errors. Maybe MZ tools can do this for you? Not that familiar with that tool. Always backup before doing anything this massive.

    Can't you just do one class, one form, one module at a time, as time permits, to get the task done? Of course, if you never used any DIM statements at all, that ain't gonna work easily
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    WOW, that's a tough one. I keep brainstorming for ways to even get a list of your variables, and nothing obvious comes to mind.

    Also, from just a bit of playing around without using "Option Explicit", it appears that you're getting Variants for everything. In and of themselves, they can cause problems when used profusely like this. You've completely obviated most of the type-checking that the compiler does for you (both p-code IDE runtime, and making of EXE).

    Darkbob, how bad is it? I think all your UDTs should be fine. It doesn't seem you can get those created without already meeting the Option Explicit requirements. So, it's just variables you're creating "on-the-fly". Do you just never use the Dim statement? Or, is it that you usually do, just not always?

    I even looked into whether or not there was some way to do a command-line-compile to have it list all the errors. But it always stops on the first one. Again, as I recently explored in another thread, you need a listing of what's in the IDE's Project-Name Table. That's an internal table of all the "identifiers" in your project (with variables probably being the largest portion of that list).

    I've got no good answers for finding those variables (and, preferably with a procedure list of how/where they're used) and providing a list. If you could get that, it'd go a long way toward helping you to get it straightened out.

    I'll continue thinking, but good luck.

    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    You know? I think we can get a list. I'm just going to brainstorm out-loud for a bit.

    It seems that it should be safe to assume that all "meaningful" variables are created in a Let statement. Now, that's certainly not strictly true though. For instance, in the following ...

    Code:
    
        i = j + 5
    

    If that's the first time we've seen either "i" or "j", then they're both created. However, it would seem that "j" is fairly meaningless in that context.

    Therefore, what if we have a utility that finds all the Let statements (even implicit Let statements), determines whether or not the left-hand variable has been previously dimmed, and, if not, make a list of those, along with the procedure in which it occurred. Or, it could possibly insert a Dim at the top of the procedure, initially just leaving the Type blank (defaulting to Variant).

    That way, you could possibly get Option Explicit in your modules, hopefully without breaking anything. There'd be the additional clean-up of those "j" variables (see above example). But those probably need to be cleaned up anyway.

    If that would work, then you have the next large task of going through and explicitly typing your variable declarations (As Long, As String, etc, etc). But that's really a second problem.

    Does any of that make sense?

    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    Say Darkbob,

    Would you be interested in sharing a fair sized module (BAS, FRM, CLS, doesn't matter) that doesn't have Option Explicit in it, and has a fair number of variables created on-the-fly?

    I'm just playing around with something, and don't have a chunk of code for testing. It doesn't matter if the module is dependent on other stuff. I won't be trying to run it. If you could do this, just zip it and attach it to a post (over in "Go Advanced").

    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Declaring Variables

    I think this would be even more difficult than it first sounds due to scope of variables. We see for example X =14 in one rountine and X=15 in another. Is X a local var in each routine, is it form level, module level, or is it global. Makes a big difference and without having dimmed it initially there is no way to tell which way it is intended to be used.


    Also note that this would not solve your problem of
    I find a crazy bug it's because of some mis-typed variable
    As the mis-typed variable would simply be dimmed and still incorrect.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    Yes, scoping would be somewhat tricky. Variables that are scoped at the module level though, they could be identified. They already meet the "Option Explicit" criteria. Otherwise, how do you get a module level variable?

    It's global variables that are a bit trickier.

    Regarding the mis-spelling. I don't think there's going to be any "automatic" program that fixes Darkbob's problem. However, if he can get a list of the variables per-procedure, possibly alphabetized, he can start laying eyeballs on those variables and spot any potential problems.

    It's still a huge task, but I don't see it as insurmountable with a good set of tools. Although, I haven't seen any source code either.

    Best Regards,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    558

    Re: Declaring Variables

    Well thanks for thinking about it guys. Too bad there isn't a simple utility out there. It's not like I've never used DIM. I just don't use it consistently. * sigh *.

    I guess I'll save my project as text then write a program to go through each routine. I'll start with the module level declarations and find all the DIM's then run through each form level procedure for DIM's. Then I'll run through the procedures and see what's not defined.

    I should just look for things on the left side of the = sign. I don't think I ever would use a variable on the right side of an = without first setting a value for it. Maybe in a For statement (ie For X = 1 to 10) but even there, the = sign is the tip-off. If i searched the line for an = then worked backwards from that point to the variable I could toss a DIM X at the top of the procedure.

    I might make a new project with Option Explicit and then add one form at a time to keep the scope of the work more manageable.

    Anyway if you guys come up with an idea let me know.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    Hey Darkbob,

    Here's something I just threw together from another project I was messing with. For now, I've just got it where it examines one-module-at-a-time. So, the whole issue of project-level-scoped-variables still isn't addressed.

    But, per-module, this should possibly give you something to get started. Also, this does NOT make any changes to your source-code.

    The utility is attached. You can just run it from the IDE. Not really any need to compile. I also attached a piece of code for testing: Test.bas

    That's just a piece of my code that I removed the Option Explicit from, and then went through and sort of randomly deleted many of the "Dim" statements from the top of the procedures.

    You can use that Test.bas to see how it would work. Here's a screen-shot of the results from Test.bas:

    Name:  Vars.jpg
Views: 286
Size:  48.5 KB

    Maybe That'll Help,
    Elroy
    Attached Files Attached Files
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    558

    Re: Declaring Variables

    Thanks so much for that!! I'll give it a shot and report back.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    558

    Re: Declaring Variables

    Hey Elroy. I ran it on one of my modules (My frmAbout.frm). This is certainly a great start and it's going to be a huge time saver. It's not catching everything but it's doing very well.

    As an example, I made a quick form with just one loop.

    Code:
    Private Sub Form_Load()
       For T = 1 To 10
         X = T + 1
       Next T
    End Sub
    The sample program found the variable X was undeclared but it didn't find the variable T as undeclared. I'll go through your code and see if I can add this case and see if I can find others.

    Thanks so much for the head start! This will help a lot.

    Update:
    I was able to modify your excellent code to find both T and X.

    In CleanTheModule

    Add:

    sModCode = Replace$(sModCode, "For ", "")

    Remove:

    ' BlankLinesStartingWith "For ", sModCode

    And presto it works.
    Last edited by Darkbob; Aug 11th, 2018 at 06:31 PM.

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Declaring Variables

    Quote Originally Posted by Darkbob View Post
    It's not like I've never used DIM. I just don't use it consistently. * sigh *.
    You should set the option on the editor to require variable declaration. This way every new file you create gets Option Explicit inserted as the first line and you will be notified every time you use a var that is not properly dimmed. This is something everyone should set as soon as they install VB and leave it active. It can save a lot of headaches down the road.

  13. #13
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    Hi Darkbob,

    Yeah, that "For T = 1 To 10" isn't a "Let" statement. I didn't go after those. There are 100s of ways a variable (without Option Explicit) could be declared. I think we'd almost re-writing the compiler to catch them all. I don't mind trying to catch a few more of the common ones though. I'll put in some code for the "For Var = ...". If you want to give me other examples that are common in your code, I'll put "catches" in for them.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  14. #14
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Declaring Variables

    @Elroy. I was playing with something that can scan a project and find oh-ohs too.

    Undeclared variables are problematic, but can be mostly done once it's broken down a bit.

    1. Ignore stuff don't need to worry about, i.e.,
    comments
    quoted strings
    numbers

    2. Stuff left/right of operators. These can be a method/enum/constant/hardcoded number/string or an object property and if not, should be a variable. Hardest part since in many cases, the left/right side of operators are interchangeable, except when assignment is in play, i.e.,
    - If t < vbRed Then
    - If vbRed >= t Then
    -- Edited: constants/enums are nearly impossible to distinguish from undeclared variables, via parsing, because they may be contained within a VB reference (ocx/dll) or TLB

    4. Look at loop variables: For, Do, While, etc

    5. Look at stuff that is part of a function/property call, i.e., CallByName x, y, z. The parameters should be variables, but can be methods too.

    All method parameters can be suspect. These can be complex, including nested methods with/without undeclared variables. Methods include API, custom methods and VB ones, i.e., InStr, Choose, IIF, etc

    Any how, if you are getting most of the those undeclared variables, then Darkbob's job will be easier to judge effort needed per file. Still expect it to be a pain for him if he's gotta manually write all the DIM statements

    edited: after some sleep and re-thinking. This is really impossible to do outside of the project. If some unknown "word" were parsed, is it an undeclared variable? is it a VB key word? is it a public constant or function or property in a referenced dependency or TLB? How can a parser determine that without being able to load all dependencies and TLBs while expending the effort to parse all those too (for constants, enums, methods) just so you can fully identify some "word" as being an undeclared variable. And if you do manage to do that, would that mean you can auto-fix the project and add DIM statements? Sure, but you are going to probably declare them as Variant which in the end didn't add a whole lot. The code owner is still going to need to spend significant amount of time looking over the auto-corrections and possibly adjusting them.
    Last edited by LaVolpe; Aug 12th, 2018 at 10:23 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  15. #15
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Declaring Variables

    Hello. What I would do:

    Set the variable declaration as required in the VB's IDE settings, so any new modules appear already with Opcion Explicit (as others already suggested).

    Take this work just a part at a time. For example, one day if you found a bug, lets suppose it is in a form and that it also uses a module. While fixing that bug (or before doing it, or after) put the Option Explicit in that form and in that module.

    Run the program pressing Control+F5.
    You'll get an error message telling about an undeclared variable.
    Go and declare that variable (and any other that you can see around there).
    Keep trying to run the program pressing Control+F5, and declaring variables, until it actually runs.

    If you feel like it, do some more forms (and/or modules) that day.

  16. #16
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Declaring Variables

    The assumption that variables *must* appear as LHS in assign statement before any other usage is quite flawed.

    Consider this code
    thinBasic Code:
    1. 'Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. '    Dim MyData
    5.     If CalcData(MyData) Then
    6.         Debug.Print "Wat?"
    7.     End If
    8. End Sub
    9.  
    10. Private Function CalcData(ByVal lData As Long) As Boolean
    11.     CalcData = True
    12. End Function
    13.  
    14. Private Function MyData() As Long
    15.     MyData = 42
    16. End Function
    MyData might by a local variable in Form_Load or a function that is defined later in the same module or a global variable/function or an API declare or a typelib function or a const.

    Declaring MyData as local variable would be premature and totally alter the code semantics.

    There is no simple and remotely reliable way to find undeclared local vars but to let the compiler find these by setting Option Explicit on. No way short of re-implementing VB6 compiler's front-end that is.

    cheers,
    </wqw>

  17. #17
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Declaring Variables

    @wqweto; That could actually be parsed and known that MyData & CalcData is a function. First process modules, classes, UCs, etc. However if either of those functions were outside the project (TLB and/or dependency functions/constants/enums), no way of knowing via parsing without somehow late-binding all of that project's dependencies and finding their functions/constants/enums.

    I mentioned in my 1st reply to maybe to this little by little as time permits. What I didn't mention, because I assumed, was that Darkbob knew about Ctrl+F5 mentioned by Eduardo. Granted that hundreds of project files will take time, but Darkbob can still do this piecemeal at his own convenience, one file at a time.

    What could be interesting, would be to create an add-in that could check for undeclared variables. But not that familiar with add-in creation. The idea is to recognize which code pages don't have Option Explicit, add it, run with Ctrl+F5, and if the add-in can be notified of errors, it could possible enter break mode and auto-fix the declaration? All done via the add-in. Though I think that would be somewhat limited to declaring everything as Variant without some human intervention. Just thinking out loud & don't know if that scenario is even possible.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  18. #18
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    Ok WOW, this has turned into quite the discussion. I need to study some of what has been said. But, I woke up this morning and did a bit more work on my little project:

    • Added the loop to process all modules in a VBP.
    • Put some more "catches" in for variables created when starting For.., Do While.., Do Until.., Loop While.., Loop Until.., and Redim.
    • Added a "filter" for ignoring certain globals -- see the big Select Case in the FindUnDeclaredVariables procedure. This section could be customized on a per-project basis.


    The latest incarnation of this thing is attached. I think I'm close to done with this thing, as it runs fairly cleanly on my large primary project. And, for DarkBob, I think it could help him a great deal (but not 100%) if he decides to get serious about cleaning this up.

    Let me also address a few comments (although I still need to read some things with more though, but breakfast first).

    Quote Originally Posted by wqweto View Post
    The assumption that variables *must* appear as LHS in assign statement before any other usage is quite flawed.
    Yeah, I never claimed this thing was 100%. That's one reason I didn't make any attempts to modify scanned source code. Personally, I think this clean-up should be done manually, even though it will be a large undertaking. I just don't see any other way to make sure you're not breaking something that's not broken.

    However, if we can develop a tool that will get DarkBob 90% of the way there, it seems that that would be pretty huge.


    @Eduardo: I couldn't agree more with your comments. This thing is too huge to tackle all at once. DarkBob, if it were me, I'd certainly nibble at this thing, doing bits, making sure I didn't break anything. However, if you're anything like me, once I got rolling, I'd start biting off larger and larger chunks, probably getting a bit obsessed until it got done.

    @LaVolpe: I need to read some of your ideas more carefully before commenting.

    Y'all Take Care,
    Elroy
    Attached Files Attached Files
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  19. #19
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Declaring Variables

    Quote Originally Posted by Elroy View Post
    @LaVolpe: I need to read some of your ideas more carefully before commenting.
    No need. Though I believe in my tag line: Insomnia is just a byproduct of, "It can't be done", I've pretty much come to the conclusion that trying to identify and ultimately patch undeclared variables is not worth the effort, via automation. You are likely going to miss many without some sort of aggressive & very accurate parser, and those you don't miss may be misidentified because they are public items in dependencies and TLBs and elsewhere in the project.

    If just identifying them is the goal, Ctrl+F5 after adding Option Explicit is the easiest solution. Fix them and move on to the next code page. If you can't finish that code page due to time, rem out the Option Explicit (if desired) and finish it another time.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  20. #20
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Declaring Variables

    Quote Originally Posted by LaVolpe View Post
    Though I think that would be somewhat limited to declaring everything as Variant without some human intervention.
    Hummm, but what would be the advantage of doing that compared to just leaving the variables undeclared and without Option Explicit?

    I think that we should clear what is the problem(s) that we are addressing.
    What is the problem of having the variables undeclared?
    One problem that I see is to misspell one variable name and accidentally create another variable unintentionally.

    Code:
    For MyCounter = 1 to 1000
       If MyConter = 100 then...
    Next
    In that case to automatically declare the variables won't be a solution, since it will declare two variables automatically and you won't notice the bug.

  21. #21
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Declaring Variables

    Quote Originally Posted by Eduardo- View Post
    Hummm, but what would be the advantage of doing that compared to just leaving the variables undeclared and without Option Explicit?
    Exactly. I updated post #14 with that same observation.

    One problem that I see is to misspell one variable name and accidentally create another variable unintentionally.

    Code:
    For MyCounter = 1 to 1000
       If MyConter = 100 then...
    Next
    In that case to automatically declare the variables won't be a solution, since it will declare two variables automatically and you won't notice the bug
    In that case, the code wasn't working in the first place. But that is fine example of why you don't want to automate DIM statements for undeclared variables. Hopefully VB stopping on that line of code with "Option Explicit" added and run with Ctrl+F5 would make that type of error stand out
    Last edited by LaVolpe; Aug 12th, 2018 at 10:43 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    558

    Re: Declaring Variables

    Thanks so much Elroy for your hard work! It's bound to help a lot of people. I pointed it at the .VBP and it chewed through a few dozen forms before it finally choked up and died.

    I broke your program with this line: Function CheckDay ( Dt$ ):

    Not sure why my program function has a colon there. It shouldn't be there. I removed it and your program completed perfectly.

    Sadly for me it found 38,842 undeclared variables. I now have a new full time job for the next 20 years defining variables. Ugggh. Or perhaps I could write something to automatically insert DIM statements. Now that I have a list of variables I'm miles ahead.

    You know... it might just be time to re-write this thing in a new compiler.
    Attached Images Attached Images   
    Last edited by Darkbob; Aug 12th, 2018 at 11:00 AM.

  23. #23
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Declaring Variables

    Quote Originally Posted by LaVolpe View Post
    In that case, the code wasn't working in the first place.
    Yes. And that is the kind of problems that the OP is facing with his program: bugs (some things that don't work as expected or that produce errors).

  24. #24
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    Darkbob,

    I've got no idea how important or widely-used your large project is. But, if it were me, and I had some degree of moderate financial support, I'd try and sell the idea that this needs to be cleaned up. Personally, I've gotten over feeling bad about things I did before I knew better, and I'm not above telling people that ... and that they need to pay me to fix it. Everything I do these days is open-source, and I'm quite clear that I need to get paid for any specific change requests, including clean-up. Fortunately, for me, I've got a couple of sites that do excellent Beta Testing. At one site, they maintain a complete "test" dataset/database and run it through all its paces on every new roll-out.

    Another question that comes to mind though ... how well are your globally scoped variables? Are there just a few of them? Or do they represent a large percentage of that 38,842 number? If it were me, and if there were a manageable number of globally scoped variables, I'd continue with your ideas of something to automate the insertion of Dim statements.

    Also, you might take a look at duplicate variable names. For instance, does every-other procedure have an "i" variable in it? If so, I'd consider just putting a "Dim i As Long" at the top of every procedure. Doing that a few times could potentially knock out a large number of that 38,842.

    IDK, I guess I'm still just thinking there's a reasonable way to tackle this thing.

    @LaVolpe (specifically post #14):
    • Comments, yeah, the first thing I filter out.
    • Quoted strings (i.e., string literals). Yeah, the second thing I filter out.
    • Numbers. They really didn't bother me.
    • Enums & Types. Yeah, I reworked them to look more like functions with arguments, just to make them easier to deal with.

    EDIT: And some other obvious things: Combine continued lines, split lines with multiple statements per line (i.e., ":" used).

    From there, things started to get more difficult. It was fairly easy to identify all the declared variables. From there, it was just a matter of finding the variables, and comparing them to the "declared" list. That was the most difficult part, and I'm not sure it'll ever be 100%. Although the VB6 compiler certainly does it.


    @Darkbob: At this point, I'm probably going to back away from this one a bit. You're certainly welcome to take my little project and do what you like with it. Best of luck to you.


    Elroy
    Last edited by Elroy; Aug 12th, 2018 at 11:27 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  25. #25
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Declaring Variables

    Quote Originally Posted by Elroy View Post
    Another question that comes to mind though ... how well are your globally scoped variables? Are there just a few of them? Or do they represent a large percentage of that 38,842 number?
    It has been a long time since I last programmed without declaring variables, but I think (and a simple test seemed to confirm it) that undeclared variables are always local in scope.

  26. #26
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Declaring Variables

    Quote Originally Posted by Eduardo- View Post
    It has been a long time since I last programmed without declaring variables, but I think (and a simple test seemed to confirm it) that undeclared variables are always local in scope.
    That is true, otherwise, they'd be declared globally/publicly or at module/class-level and wouldn't be undeclared
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    558

    Re: Declaring Variables

    I do have a few Global variables. I'll see if i can make a list and write a routine to remove them from the list of 38000. Of course... if some kind person would look for any .BAS file and find the Global <VAR> statements that would be nice.

    Oh and I've found a new (and annoying) way to create a variable. Unfortunately it's one I often use and your program doesn't detect it. I'll see what I can do about that.

    Input #1, RejectReason$

  28. #28
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    Darkbob,

    If you're still using my little tool, look at the "Select Case" statement in the "FindUnDeclaredVariables" procedure. That's where I knock out globals in my large project. Just add your globals to that Select Case, and they'll be ignored.

    I'll work in a "Catch" for the "Input #1, RejectReason$" case. Give me a few...

    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  29. #29
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    Here ya go (attached).

    I now catch the "Input" and "Line Input" cases.

    Also, I reworked the global variable exclusion section into a separate procedure. Take a look at the procedure named IgnoreThisOne, and rework it to suit your needs.

    Enjoy,
    Elroy
    Attached Files Attached Files
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  30. #30

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    558

    Re: Declaring Variables

    This has been a HUGE help. I did a cut and paste of all my Globals into a text file, loaded them into an array and modified the IgnoreThisOne routine to scan the array. Worked great. Now I'm going to write a program to read the output text file and make up a DIM statement for each routine in each module. Should be simple enough to insert it.

    After that, I'll add an Option Explicit to each module one by one and compile. The compiler will probably catch a few I've missed. This project has gone from Impossible to a 20 year job to I'll get it done in a week or so.

    Doesn't seem to cut it but I'll toss a huge "THANKS!!!!" your way.

  31. #31
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Declaring Variables

    Hey Darkbob,

    Through the years, I've gotten soooo much help from the guys around VBForums, I'll never be able to make up for it. I just think of this place as a pay-it-forward kind'a place.

    It's actually exciting to hear about a project like yours getting cleaned up. Hey ... LONG LIVE VB6! *laughs*

    You Take Care,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  32. #32
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    759

    Re: Declaring Variables

    Not a solution, but something to help highlight the problem:

    Code:
    Option Explicit 
    DefObj A-Z
    This tells VB to implicitly declare any variable starting with the letters A..Z (i.e .all of them) "As Object".

    This will generally foul up your code Big Time, throwing up all sorts of compilation errors. You can then use to start tracking your rogue variables and explicitly declaring.
    Of course, this method rather backfires if you make extensive use of Objects. If so, just try another "default" Data Type, like

    Code:
    DefByte A-Z
    Regards, Phill W.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width