none
MCTS Self-Paced Training Kit (Exam 70-536) RRS feed

  • Question

  • I have been going through this book code examples and a stumble across this error

    This project come from the CD code simple sold with the book.

    the path= \Desktop\Code\Chapter01\Lesson2\Exercise3\Partial\CS\ViewFile\Program.cs 26 21 ViewFile

    using System; using System.Collections.Generic; using System.Windows.Forms; using System.IO; using System.Diagnostics; namespace ViewFile { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); try { TextReader tr = new StreamReader(locationTextBox.Text);

    Error 1 The name 'locationTextBox' does not exist in the current context

    try { displayTextBox.Text = tr.ReadToEnd();

    Error 2 The name 'displayTextBox' does not exist in the current context  } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { tr.Close(); } } catch (System.IO.FileNotFoundException ex) { MessageBox.Show("Sorry, the file does not exist."); } catch (System.UnauthorizedAccessException ex) { MessageBox.Show("Sorry, you lack sufficient privileges."); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }


    Saturday, May 3, 2014 7:00 PM

Answers

  • Your code looks strange. It appears to be the Program.cs that starts up a Windows Forms application. But the standard Main in program.cs normally ends after the Application.Run. The rest of the code that you have below that statement appears to be something that you would ordinarily write into the Form, given that it accesses a couple of textbox controls (the ones where you are getting an error). Normally, the code that starts with try and ends with the last catch would be written inside the Form1.cs file (and the textbox controls that are in error would have been automatically created in the Form1.designer.cs from the controls that you dragged into the designer window).

    Please review the structure of your program. If you fail to fix it, the proper place to ask is in one of the programming forums, such as the one on Winforms (http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=winforms) or maybe even the C# forum (http://social.msdn.microsoft.com/Forums/vstudio/en-us/home?forum=csharpgeneral).

    Saturday, May 3, 2014 7:51 PM
  • Hi,

    You may also want to consider sending the publisher a message to let them know about this. The publisher keeps a list of errors that they identify for each title that they publish. Please visit the Microsoft Press site to report the issue.


    Mike Corkery, MCT, MVP (Office Systems), MCSD (Windows Store Apps), MCSD (Web Apps), MCITP, MCPD, MSF, etc. Please do not forget to click “Vote as Helpful” if any post helps you and &quot;Mark as Answer”if it solves the issue.

    Sunday, May 4, 2014 12:40 AM
    Moderator

All replies

  • Your code looks strange. It appears to be the Program.cs that starts up a Windows Forms application. But the standard Main in program.cs normally ends after the Application.Run. The rest of the code that you have below that statement appears to be something that you would ordinarily write into the Form, given that it accesses a couple of textbox controls (the ones where you are getting an error). Normally, the code that starts with try and ends with the last catch would be written inside the Form1.cs file (and the textbox controls that are in error would have been automatically created in the Form1.designer.cs from the controls that you dragged into the designer window).

    Please review the structure of your program. If you fail to fix it, the proper place to ask is in one of the programming forums, such as the one on Winforms (http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=winforms) or maybe even the C# forum (http://social.msdn.microsoft.com/Forums/vstudio/en-us/home?forum=csharpgeneral).

    Saturday, May 3, 2014 7:51 PM
  • Hi,

    You may also want to consider sending the publisher a message to let them know about this. The publisher keeps a list of errors that they identify for each title that they publish. Please visit the Microsoft Press site to report the issue.


    Mike Corkery, MCT, MVP (Office Systems), MCSD (Windows Store Apps), MCSD (Web Apps), MCITP, MCPD, MSF, etc. Please do not forget to click “Vote as Helpful” if any post helps you and &quot;Mark as Answer”if it solves the issue.

    Sunday, May 4, 2014 12:40 AM
    Moderator