Accessing a variable outside of an 'if' statement

c#

Solution

Define it outside of the if statement.

double insuranceCost;
if (this.comboBox5.Text == "Third Party Fire and Theft")
        {
          insuranceCost = 1;
        }

If you are returning it from the method then you can assign it a default value or 0, otherwise you may get an error, "Use of unassigned variable";

double insuranceCost = 0;

or

double insuranceCost = default(double); // which is 0.0

Problem

How can I make `insuranceCost` available outside the `if` statement? ``` if (this.comboBox5.Text == "Third Party Fire and Theft") { double insuranceCost = 1; } ```

Original source