Use of unassigned variable error when I am trying to assign values to variables using IF statements

Ninja James

I am writing a program in Windows Forms which I am trying to assign values to variables using IF statements depending on which radiobuttons in the form are clicked. I am getting an error "use of unassigned variable" on the lines below in bold. The issue is that TotalWeeks has apparently not been assigned, however I assigned it a value in the IF statement above that one. Can anyone suggest a possible fix or idea to try and sort out the issue?

private void button1_Click(object sender, EventArgs e)
{
    //Assigning variables
    int WeeklyRate; //To assign integer values for "WeeklyRate" for future calculation use. If Radio button clicked it has value of radio button etc. 
    if (radioButton1.Checked)
    {
        WeeklyRate = 10;
    }
    else if (radioButton2.Checked)
    {
        WeeklyRate = 15;
    }
    else if (radioButton3.Checked)
    {
        WeeklyRate = 20;
    }

    int TotalWeeks;//assign values to total weeks based on number of months selected, to use in calculations for total cost.
    if (radioButton4.Checked)
    {
        TotalWeeks = 12;
    }
    else if (radioButton5.Checked)
    {
        TotalWeeks = 52;
    }
    else if (radioButton6.Checked)
    {
        TotalWeeks = 104;
    }

    int Payments;//assigning the amount of payments based on frequency radiobutton selected. 
    if (radioButton7.Checked)
    {
        **Payments = TotalWeeks;**
    }
    else if (radioButton8.Checked)
    {
        **Payments = TotalWeeks / 4;**
    }
}
}
}
Kacper Wyczawski

When neither of radioButton4, radioButton5, radioButton6 is checked, then you can't for example divide it by 4.

You should use this when initializing variable

int TotalWeeks = 0;

(result: Payments will be 0 when radioButtons aren't checked)


or this when initializing

int? TotalWeeks = null;

and this when assigning the amount of payments

if (radioButton7.Checked && TotalWeeks is not null)
{
    Payments = TotalWeeks;
}
else if (radioButton8.Checked && TotalWeeks is not null)
{
    Payments = TotalWeeks / 4;
}

(result: Now Payments variable is unassigned, when radioButtons aren't checked)

Edit: Thanks GSerg, I forgot about ? after int to make it nullable

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the reason for "Use of unassigned local variable" error?

Suppress "Use of unassigned local variable" error?

Use of unassigned local variable error for currency converter? I'm Sorry

How am I supposed to use prepared statements in Wordpress with variables in a query?

I am getting error when I am trying to get video links of YouTube playlist using Paffy

I am trying to get rid of rows using multiple values, but I am getting an error

How to assign values of different types to a variable inside if statements and then use this variable after the if statements?

When I am trying to increment a variable, unable to compare the variable with values in dictionary

I am trying to use pointers to change the values in a struct via a function, but I get an error when inputting adress the in the function

Why am I getting the error "using uninitialized memory 'i'" and "uninitialized local variable 'i' used" when trying to make i = i*i

I am trying to make a GUI for a app I am working on but when I try print a global variable I get an error. Why?

What am I doing wrong when trying to assign a value in hlsl?

Use of unassigned local variable when using async/await

Error when i am trying to remove an element

How can I use if-else statements (or a better way) to assign absolute values to days in a year (using R)?

Why am I getting "Use of unassigned local variable"?

Receiving error "use of local unassigned Variable"

Error "Use of unassigned local variable"?

use of unassigned local variable when using a foreach Loop

I am trying to use "sort" method but I am getting an error

Use of unassigned local variable when trying to return bool

Why am I getting an error when trying to compare to dictionary values?

Getting Error when i am trying to use props in Navigation

I am trying to learn and properly use variables/values in HTML within PHP

Why am I unable to use a variable when using the map() function?

Why am I getting a syntax error when trying to use -v flag for variables in psql command?

I am getting a type error when I am trying to replace

"use of unassigned variable" compile error that will never happen

Why am I getting syntax error when trying to assign secret key in flask?