MadMaxx Posted June 14, 2005 Posted June 14, 2005 (edited) Hey everyone. I seem to be having trouble with a public variable and I can't figure out what is going on for the life of me. Here is some code. 'Declarations.vb - Module Public ASSEM_NUM, TEMP_ASSEM as String ASSEM_NUM gets set when the user selects an Assembler from the combo box. Now the problem I am having is that an updated program for the parts we produce is coming into effect. So I have a duplicate form but with different part numbers. When the user completes a bin the program then calls the label printing module. Because of the duplicate form I have to change the ASSEM_NUM variable so the scan guns can still pick up the label that was printed. 'PrintLabel.vb - Module If ASSEM_NUM.Substring(4) = "T900" Then TEMP_ASSEM = ASSEM_NUM ASSEM_NUM = ASSEM_NUM.Substring(0, 4) End If 'Code that produces the label ........ 'When label is done change ASSEM_NUM Back ASSEM_NUM = TEMP_ASSEM If I put a message box after the last line ASSEM_NUM is correct. I also put a message box after the call to the label print module and it is what I set it to before the label was printed. for example if ASSEM_NUM = "4391T900" then it will get changed to "4391" until the label is printed and then it gets changed back to "4391T900" so the screen can refresh correctly. But as soon as the call is complete to the label printing module somehow ASSEM_NUM gets changed back to "4391". Any ideas! Edited June 14, 2005 by MadMaxx Quote
Administrators PlausiblyDamp Posted June 14, 2005 Administrators Posted June 14, 2005 Two things: One - does the label printing module do anything to manipulate the global variable? Two - globals are bad and easily prone to problems like you are having. Rather than a global variable could each form / routine not use a local variable or a parameter? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MadMaxx Posted June 14, 2005 Author Posted June 14, 2005 Yes the printing module changes the global variable. It's the second bit of code I posted. Quote
mskeel Posted June 16, 2005 Posted June 16, 2005 Two - globals are bad and easily prone to problems like you are having. Rather than a global variable could each form / routine not use a local variable or a parameter?True. A little refactoring would be the ideal here. Becuase the global public variable can be changed anywhere by any code, it is going to be nearly impossible to track down what is giving you the unexpected value. Try hiding your data a little better by making that variable private and/or by passing the data directly via parameters in methods. A few simple tricks like that will prevent this problem from bothering you in the future. Have you tried walking through the entire process with the debugger directly? It will probably take a little while and be very prone to errors, but you might find the problem that way. The reccomendation is still to refactor. Quote
MadMaxx Posted June 16, 2005 Author Posted June 16, 2005 It works if I reset the variable outside of the module. Strange. Quote
Administrators PlausiblyDamp Posted June 16, 2005 Administrators Posted June 16, 2005 You don't have two variables called ASSEM_NUM do you? Again I would recomend trying to remove the global though - could the printing routine not be modified to accept a parameter rather than rely on the global? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.