user5586668
user5586668

Reputation:

Declaring variables outside the scope C#

Really stupid question because I don't think you can do this but

 namespace Parking_Ticket_Fines
 {
   public partial class frmParking : Form
   {
    public frmParking()
    {
        InitializeComponent();
    }

    private void btnCal_Click(object sender, EventArgs e)
    {
        //Variable
        int Total = 0;

        //Checks which radio button is checked
        if (radExpired.Checked)
        {
            Total = 35;
        }
        else if (radParking.Checked)
        {
            Total = 75;
        }
        else if (radDriveway.Checked)
        {
            Total = 150;
        }
        else if (radHandicap.Checked)
        {
            Total = 500;
        }
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        //Clears everything 
        lblTotal.Text = "";
        radRepeat.Checked = false;
        radExpired.Checked = true;
    }

Is there a way I can use the "Total" variable in both private voids without declaring it in twice?

Upvotes: 1

Views: 59

Answers (1)

Habib
Habib

Reputation: 223227

Is there a way I can use the "Total" variable in both private voids without declaring it in twice?

Yes declare it at class level.

public partial class frmParking : Form
   {
    private int Total; //Here 
    public frmParking()
    {
        InitializeComponent();
    }

This Total will be a field read more about Fields (C# Programming Guide)

Upvotes: 3

Related Questions