Click to See Complete Forum and Search --> : Global variables


Dreamon
March 25th, 2003, 08:31 PM
I'm in startup phase of creating a Yatzee game in .NET, using Windows Forms and C#.

I apologize for moron-like questions here, but I'm a web programmer....

From what I understand, there is no such thing as a global variables (as in Visual Basic 6.....bas files) in .NET. Am I right?

Ok, if this is the case, how would I have to approach the situation in my game where I have to store players scores throughout a game? I suppose I can use the labels in my form, but this just seems like a pain in the butt, because then I have to go through all labels whenever I need to calculate total scores etc.

It would be nice to have an array of objects (Player objects) where I store a player's data (name, score, how many choices left, and so on). Also, what kind of game that has been chosen at startup (straight down, random) needs to be stored somewhere.

Would one possibility be using textfiles to store information for each player during a game (delete them when game is done)? Sounds to me like that would be a really screwed up way of doing it though.

Can anyone give me a pointer?

Thanks so much!

dreyln
March 25th, 2003, 10:47 PM
Well, you don't need any global variables. If all you want is to do is store user information until the application is closed then just use a member instance variable (some type of Collection/Array that holds Player objects or something). For example, your Form can have a member variable that holds player scores in any manner that you want. If you want this data to be accessible each time your program is run, then you obviously have to save the data to a file. I hope this helps.

Dreamon
March 26th, 2003, 01:17 AM
Ok, and say I have a own form where you register player names and what kind of game you want to play, and initiate the array of Player objects there.... would that array still be "active" when that form is closed and you go back to the main form? Or is there a way of passing it to the other form?

Anyway, your answer helped alot. I am just not all into how windows forms works. I'm all into web form thinking.

Thanks!

dreyln
March 26th, 2003, 01:51 AM
Think in terms of objects. I'm guessing that your Main form has the Main function which is the entry point to your application. The .NET CLR instanciates your Main form as an object (inside your main form this would be referenced using the "this" keyword). If you have another form where users enter information, then that form is probably instanciated in your main class, and whatever you called it is your reference to that object. So, you can do whatever you want with these objects. Since you probably have a reference to the other form in your main form you can get anything from it as long as you provide public access, getters and setters, or (best way) properties for the members of your other form. I hope that does not sound too complicated.

Let's say you have Mainform named mainform and EnterForm named enterform and EnterForm has a TextBox named namebox. I would provide a getter property for namebox:
public string Name
{
get
{
return namebox.Text
}
}

Let's also assume that enterform is a member of mainform and that enterform has a reference to mainform (in essence, they both have references to each other now). So, you could do the following to get the name after a user has submitted it:

//in submit_click method of EnterForm
mainform.Show();
this.Hide();
//maybe trigger some event in mainform
//here


//in some function in MainForm
//do anything you want with Name that was entered
string temp = enterform.Name;

I hope this helps.

Dreamon
March 26th, 2003, 02:01 AM
Great! Yeah, that helped alot. Basically it is the same way as web forms. I just had a little hard time picturing the whole model in my head. But, now I can, thanks to you!

Thanks alot!