+ 1

[SOLVED] (Thank you) C# taking the result of a sum in an if statement and using it outside that statement.

Hi, I'm still pretty new to coding, I'm currently trying to complete a small project, that i would appreciate a little bit of help on please? I'm writing a fairly basic battle simulation program and I've encountered a snag. https://code.sololearn.com/cA180a8a2a3a A quick explanation of the code above: section 1 takes input about units for 2 armies. it wants numbers for differing unit types for your army, and the enemy army, and then outputs that information in a summation section 2 uses the information gathered in section one to set up some battle parameters - namely the size of the battlefield, via checks on troop types for range in a series of if-else statements. section 3 will involve the two armies facing each other from the lines drawn in section 2 as their start point and will start moving towards each other to engage in battle. and this is where I've hit my snag. Ideally what I need to do from here is use the information gathered in the section 2 as a basis for calculations on range, that will effect movement across the battlefield, but the variable I've been using in the if statements: "battleFieldSize" isn't transferable out of the if statements in section 2. it throws up the following error when I try to use it: "The name 'battleFieldSize' does not exist in the current context" how can i transfer the information gathered from the section 2 if statements to section 3 please? FYI, in code playground the code doesn't seem to have the same functionality as in VS. to get it to work as intended to this point, input 6 numbers at 0 or greater. I've also commented out an if statement in section 3, that I've used as an example of what I mean.

23rd May 2021, 6:30 AM
Andrew Hall
Andrew Hall - avatar
2 odpowiedzi
+ 1
Brilliant! thank you! like I said, I am still very new to this, this is the first time I've tried writing some code from scratch. I have no doubt I'm going to make many more total newb errors like this one! hehe I've adjusted the code in the link to suit the changes made.
23rd May 2021, 7:48 AM
Andrew Hall
Andrew Hall - avatar