bramming
May 8th, 2009, 05:24 PM
Hey. Im currently writing a GUI app to calculate a scheme for Dungeons and dragons roleplay. I figured this would be a good start (since im new to c#). In general my programming experience is limited to working with some simple classes.
The program looks like this:
http://img26.imageshack.us/img26/5261/nyged.th.png (http://img26.imageshack.us/my.php?image=nyged.png)
Each stat (str, dex, etc) has values for "ability score", "ability modifier", "race modifier" and "cost".
All those are represented in global variables. Also each stats has a "+" and "-" button to raise and lower the "ability score".
The thing is, instead of having a billion of methods for each button click, i want to make all button-click-signals to refer to the same method. That method would then based on its sender, perform the calculations and then correct the "right" variables. For instance if i click the "+" button for "str" the variables "str" has (the "abilityscore", etc variables) will then get calculated and changed.
This can easily be done by having each button having its own method, and then calculate everything and change the variables defined in the method. However i think its pretty ineffecient having 2x6 methods running the same code, just writing to different variables.
So what im trying to ask help for is: The 1 method i wanna have to handle all the click signals, has to write to different variables depending on the sender. So coding would roughly look like this:
protected virtual void OnPressStats (object sender, System.EventArgs e)
{
// if the sender is the "+" or "-" button for "str"
if (sender == btnStrP || sender == btnStrM)
{
(some kind of pointer) = ASstr (the abilityscore variable for strength)
}
if (sender == btnDexP || sender == btnDexM)
{
(some kind of pointer) = ASdex (the abilityscore variable for dex)
}
(some kind of pointer) = (something based on calculations)
}
so that in the calculations i can use (some kind of pointer) to get values and set values to the different variables depending on which button is pressed.
This way, the same method can be used for all buttons, because they change different variables (ASstr, ASdex, etc) based on the sender.
I hope i have been able to express myself clearly enough for you to understand. English isnt my native language so there might be some grammatical errors etc.
Please let me know if i have to express myself more clearly,
and thanks in advance!
The program looks like this:
http://img26.imageshack.us/img26/5261/nyged.th.png (http://img26.imageshack.us/my.php?image=nyged.png)
Each stat (str, dex, etc) has values for "ability score", "ability modifier", "race modifier" and "cost".
All those are represented in global variables. Also each stats has a "+" and "-" button to raise and lower the "ability score".
The thing is, instead of having a billion of methods for each button click, i want to make all button-click-signals to refer to the same method. That method would then based on its sender, perform the calculations and then correct the "right" variables. For instance if i click the "+" button for "str" the variables "str" has (the "abilityscore", etc variables) will then get calculated and changed.
This can easily be done by having each button having its own method, and then calculate everything and change the variables defined in the method. However i think its pretty ineffecient having 2x6 methods running the same code, just writing to different variables.
So what im trying to ask help for is: The 1 method i wanna have to handle all the click signals, has to write to different variables depending on the sender. So coding would roughly look like this:
protected virtual void OnPressStats (object sender, System.EventArgs e)
{
// if the sender is the "+" or "-" button for "str"
if (sender == btnStrP || sender == btnStrM)
{
(some kind of pointer) = ASstr (the abilityscore variable for strength)
}
if (sender == btnDexP || sender == btnDexM)
{
(some kind of pointer) = ASdex (the abilityscore variable for dex)
}
(some kind of pointer) = (something based on calculations)
}
so that in the calculations i can use (some kind of pointer) to get values and set values to the different variables depending on which button is pressed.
This way, the same method can be used for all buttons, because they change different variables (ASstr, ASdex, etc) based on the sender.
I hope i have been able to express myself clearly enough for you to understand. English isnt my native language so there might be some grammatical errors etc.
Please let me know if i have to express myself more clearly,
and thanks in advance!