Assigning Variable to a Label in Global? (Sort of)

TheLaw

Member
Joined
Oct 30, 2010
Messages
13
Programming Experience
Beginner
Hey guys,

I'm feeling like an idiot here, so please excuse me. I'm working on my final for VB.NET, and maybe it's just late...but here's how it goes. I need to make a variable named intMoney and it will be used to keep track of how much money you have and the value will be displayed in a label. Since this 'game' will have a lot of things going on, it needs to be available in a lot of places, not just one click event.

But I need to set intMoney to a starting value of 200 and then assign intMoney to a label called lblMoney. I can't assign intMoney to lblMoney in global. I get build errors. Where should I put the lblMoney.Text = intMoney?

Thanks.
 
Are you saying that this Label needs to be updated when you set the variable from anywhere in the app?

In a well architected app, there's almost never a need for global variables. As such, the first order of business should be to get rid of that global variable if at all possible. Can you explain where this Label is and where else this value is required, along with the relationship between them, e.g. the form with the Label creates another form in which the user can edit the value.
 
Yes. So if you buy something from a "shop", the variable needs to be updated accordingly so that the value is reduced to symbolize a loss of money. This variable then needs to be displayed in lblMoney to show the user how much money he has available. I don't believe I can feasibly fit the entire thing under a single button. I can somehow get the "shops" to be all run under the same button. That's simple. But if you are receiving money, it would be an "automatic payment", and it would depend on a lot of other variables that the player will have. I'm still planning as you can see, but it's tricky because we never learned about a program that can truly do more than a single operation.

Perhaps I could reassign the name, position, etc of the button so that it would appear that it is actually a different button, while infact it is the same button that was used in the shop. Hmm....

Thanks!
 
We only just touched on modules. Care to explain a theoretical setup?

Thanks.

Add a module to your project just as you would a form or class. Declare the variable in the module just as you would anywhere else. That variable is then accessible anywhere in the project.

Like I said though, this type of thing is rarely needed. In a real-world app, the form containing the Label would usually create some other form where something would happen and then, when that form closed, the calling form would update itself. In other cases, the form where the changes are made would raise an event to notify the calling form of the change and, again, the calling form would update itself. These are more advanced designs that may not be easy for a beginner though.
 
Thanks guys. Maybe in the future I'll try some of that more advanced stuff. However, I need a quick and dirty way to do it now. And provided we didn't even really do Modules, I might get enough brownie points of that anyway.

Thanks for the responses.
 
Back
Top