none
Error CS0128_A local variable named 'premium' is already defined in this scope_text the true code RRS feed

  • Question

  • namespace text_the_true_code
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            public void generatebutton_Click(object sender, EventArgs e)
            {
                decimal personage = Convert.ToDecimal(agetextBox.Text);
                int duration = Convert.ToInt32(durationtextBox.Text);

                int premium = 0;

                 premium = 12;
                    if (planAradioButton.Checked == true & personage >= 0 && personage <= 6 & duration == 7) ;
                else premium = (0);

                {

                    premiumtextBox.Text = premium.ToString();

                }



                int premium = 0;         // in here it says that a local variable is already defined in this scope
                premium = 15;
               if (planAradioButton.Checked == true & personage >= 7 && personage <= 17 & duration == 7) ;
                else premium = (0);

                {

                        premiumtextBox.Text = premium.ToString();

                    }

            }
        }
    }

    i want to to use lots of duration and ages and get the result in premium text box

    but when it doesnt work then what to do ?

                       
    Tuesday, October 10, 2017 6:55 AM

Answers

  • Please see my previous link about writing 'if' statements. You do not put a ';' after the 'if' clause.

     if (planAradioButton.Checked == true & personage >= 0
         && personage <= 6 & duration == 7) // ; NO SEMI-COLON HERE!!!!
     {
         premiumtextBox.Text = "12";
     }
     else
     {
         premiumtextBox.Text = "0";
     }




    Tuesday, October 10, 2017 10:31 AM
  • Hi abdullah,

    That is because you put a semi colon ";" in "if" statement. Structure of if statement is like this

    if(condition)

    {

    //Do something

    }

    else

    {

    //Do something

    }

    Please remove the semi-colon from both of your if condition.

    if (planAradioButton.Checked == true & personage >= 0 && personage <= 6 & duration == 7) //Remove ";"

    if (planAradioButton.Checked == true & personage >= 7 && personage <= 17 & duration == 7) //Remove ";"



    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]



    Tuesday, October 10, 2017 10:28 AM
    Moderator

All replies

  • This is because you have declared a variable with the same name in the same scope (i.e. within the same step of curly brackets).

    By putting the datatype (int) in front of a name you are declaring a variable.

    int premium = 0;

    // ... code ...

    int premium = 0; // << Error: Cannot declare another variable called premium

    You cannot declare another variable with the same name in the same scope. Remove your second declaration.

    As an aside you have placed semi-colons after your 'if' and 'else' tests with various random brackets all over the place, which does not seem right and I doubt its what you intend. Though its a bit hard to read your code the ways it is pasted (you should use the 'Insert code block' button to paste in code).



    • Edited by RJP1973 Tuesday, October 10, 2017 8:47 AM
    Tuesday, October 10, 2017 8:30 AM
  • sir if i remove the 

    int premium = 0 ;

    it doesnt matter . it doesnt create problem 

    but i want to add many values in age and in duration 

    my project is about : to inter a value in the age and a value in the duration and select 1 radio button 

    and in result i want its premium

    and i have multiple ages and duration 

    how can i set them how can i define them in c # variable scope

    Tuesday, October 10, 2017 8:50 AM

  • namespace text_the_true_code
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
           
            public void generatebutton_Click(object sender, EventArgs e)
            {
                decimal personage = Convert.ToDecimal(agetextBox.Text);
                int duration = Convert.ToInt32(durationtextBox.Text);

                int premium;

                 premium = 12;
                    if (planAradioButton.Checked == true & personage >= 0 && personage <= 6 & duration == 7) ;
                else premium = (0);

                {

                    premiumtextBox.Text = premium.ToString();

                }



               
                premium = 15;
               if (planAradioButton.Checked == true & personage >= 7 && personage <= 17 & duration == 7) ;
                else premium = (0);

                {

                        premiumtextBox.Text = premium.ToString();

                    }
                
            }
        }
    }
    Tuesday, October 10, 2017 9:05 AM
  • 1) Please use the 'insert code block' button (second from the right) to paste in code. Its the one that looks like: 

    2) Variables of a given name can only be declared once within a code block, but can be used and reassigned as many times as you like within that. You were declaring one twice which was exactly the cause of your CS0128 error, so not sure why you are saying "it doesn't matter".

    3) See here for the syntax of how to use the if/else statement in C#. Note that you do not put semi-colons between the if clause or the else clause and the code block in curly {} brackets afterwards.

    Example:

    if (planAradioButton.Checked == true & personage >= 0 && personage <= 6 & duration == 7)
    {
      // .. code here will execute if above condition is true
    }
    else
    {
      // .. otherwise the code here will execute otherwise
    }
    
    


    • Edited by RJP1973 Tuesday, October 10, 2017 9:16 AM added example
    Tuesday, October 10, 2017 9:10 AM
  • sir i have done such this 

    but its not working 

    please give some information how to write whole code

                   

    namespace text_the_true_code
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            public void generatebutton_Click(object sender, EventArgs e)
            {
                decimal personage = Convert.ToDecimal(agetextBox.Text);
                int duration = Convert.ToInt32(durationtextBox.Text);


                if (planAradioButton.Checked == true & personage >= 0 && personage <= 6 & duration == 7) ;

                {
                    premiumtextBox.Text = "12";
                }                                             // here it says }is expected 

          else
                {
                    premiumtextBox.Text = "0"; ;
                }




                if (planAradioButton.Checked == true & personage >= 7 && personage <= 17 & duration == 7) ;
                { 
                premiumtextBox.Text = "15";
            }                                                  // here it says }is expected
                else 
                {
                        premiumtextBox.Text = "0";
                    }


                }

        }
    }

    Tuesday, October 10, 2017 10:15 AM
  • Hi abdullah,

    That is because you put a semi colon ";" in "if" statement. Structure of if statement is like this

    if(condition)

    {

    //Do something

    }

    else

    {

    //Do something

    }

    Please remove the semi-colon from both of your if condition.

    if (planAradioButton.Checked == true & personage >= 0 && personage <= 6 & duration == 7) //Remove ";"

    if (planAradioButton.Checked == true & personage >= 7 && personage <= 17 & duration == 7) //Remove ";"



    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]



    Tuesday, October 10, 2017 10:28 AM
    Moderator
  • Please see my previous link about writing 'if' statements. You do not put a ';' after the 'if' clause.

     if (planAradioButton.Checked == true & personage >= 0
         && personage <= 6 & duration == 7) // ; NO SEMI-COLON HERE!!!!
     {
         premiumtextBox.Text = "12";
     }
     else
     {
         premiumtextBox.Text = "0";
     }




    Tuesday, October 10, 2017 10:31 AM
  • dear sir thanks a lot you have solved my problem 

    once again thank you very much

    best regard

    abdullah ahmadyar from kabul afghanistan

    Tuesday, October 10, 2017 10:48 AM
  • Hi abdullah ahmadyar,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Proposed as answer by Fei HuModerator Wednesday, October 11, 2017 11:53 AM
    • Unproposed as answer by Fei HuModerator Wednesday, October 11, 2017 11:53 AM
    Tuesday, October 10, 2017 10:59 AM
    Moderator