The name “x” does not exist in the current context

Israel

Active member
Joined
Jan 10, 2020
Messages
26
Programming Experience
Beginner
Hi, I wrote these codes because I need to make a condition (as you can see). But Its sturks where its highlighted. Means giving this error message :

The name “A” does not existi n the current context
The name “B” does not existi n the current context

C#:
using System;
using System.Windows.Form
namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void txtResultA_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtResultA.Text) && !string.IsNullOrWhiteSpace(txtResultB.Text))
            {
                float A = 0;
                float B = 0;
                float.TryParse(txtResultA.Text, out A);
                float.TryParse(txtResultB.Text, out B);
                if (A > B)
                {
                    txt_SoldForResultA.Text = (A - B).ToString("N2");
                    txt_SoldForResultB.Text = string.Empty;
                }
                else if (A < B)
                {
                    txt_SoldForResultB.Text = (B - A).ToString("N2");
                    txt_SoldForResultA.Text = string.Empty;
                }
            }
        }

        private void txtResultB_TextChanged(object sender, EventArgs e)
        {
            txtResultA_TextChanged(sender, e);
        }
    }
}
 
Last edited by a moderator:
I just pasted your code into a new project and, after adding the appropriate TextBox controls, it built without issue. Something else is going on in your case. Either there's something you haven't shown us or your project is broken. If the former is not the case, I'd suggest creating a new form and seeing whether it behaves the same way.

By the way, initialising those two variables is pointless. Initial values of parameters are always ignored. You have to initialise variables passed as arguments by value or those passed to parameters declared ref but, while it doesn't actually hurt, it serves no purpose for those passed to parameters declared out.
 
Back
Top Bottom