Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by MadMaxx
  • Administrators
Posted

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?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...