none
The name "txttest2" does not exist in the current context RRS feed

  • Question

  • Hello everyone,

    Newbie here just started VS a few days ago. I am creating a student form with first name, last name, ID, and two test scores. Everything seemed to be going okay until txtTest2.Text started to throw an error. Been trying to figure this out since yesterday below is the code. 


        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void btnComputeGrade_Click(object sender, EventArgs e)
            {
                // read the two test scores
                int test1 = int.Parse(txtTest1.Text);
                int test2 = int.Parse(txtTest2.Text);
           
                // create a student object, set the
                // fields in it and call compute grade
                Student s1 = new Student();
                s1.Test1 = test1;
                s1.Test2 = 91;
                string grade = s1.ComputeGrade();
                MessageBox.Show("Grade =" + grade);

    The bolded part is the problem. When I use the suggestions it seems to work but it says string not valid. Any suggestions?

    Thank you in advance 

    Thursday, May 10, 2018 4:40 PM

All replies

  • First Check txtTest2 Textbox Exists on Form1 then if it exists & if problem continues

    Problem is due to one of the exceptions listed at

    https://msdn.microsoft.com/en-us/library/b3h1hf19%28v=vs.110%29.aspx

    Use int.TryParse instead as

                 // read the two test scores
           

    int test1, test2; int.TryParse(txtTest1.Text, out test1); int.TryParse(txtTest2.Text, out test2);






    Thursday, May 10, 2018 6:34 PM
  • I found it it was actually because the text box name was not txttest2 now I'm running into a different problem, it is saying one of the lines is not a valid string. 

                int test1 = int.Parse(txtTest1.Text);

    I don't know why its throwing that error code but its pointing to that line above any suggestions?

    Thursday, May 10, 2018 8:40 PM
  • "not a valid string" is probably not the error you got.  Please cut and paste the exact error message you got.  And are you getting the message at compile-time or at run-time?

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Thursday, May 10, 2018 9:03 PM
  • Well, I think I found the solution to the problem, the text box names don't match. However when I go to change the text name it says "property value not valid". Any suggestions? I know it doesn't have to do with the code because it was a copy and paste from class (Masters in computer science class). 

    Thank you in advance,

    Thursday, May 10, 2018 9:25 PM
  • when you get dialog of 'property value not valid' click details

    you can get idea of what is not correct E.g.

    1.The name txtTest1 is already in use by another component. [Object with same name already exists]

    2.Identifier 'txt Test1' is not valid.  [Blank space not allowed in name],etc.

    Check & resolve the appropriate problem


    Friday, May 11, 2018 2:31 AM
  • thanks got it to work! 
    Friday, May 11, 2018 10:21 PM