Question Head First C# stuck in chapter 2

Rody

Member
Joined
Jul 16, 2023
Messages
6
Programming Experience
Beginner
Hi everybody,

I hope i posted this in the right section.

I'm trying to learn C# with the book Head First C# and i'm having a great time doing this, but i'm stuck at the moment.

I'm in chapter 2 around page 187/188 "Add a textbox to your control". I have put in the label, textbox and textblock like the book says. When i have to run my program the book shows that there will be an exception and that i have to find out where the problem is.
When i run the program i don't get the exception, but the program runs and shows a screen with my label, textbox and textbox like i put in :unsure:. I really have no clue at this moment and can't find what i'm doing wrong.
I deleted the whole project and started again, but i keep getting the same issue.
It's a bit frustrating because i had such a great time and it's no problem that i will run into problems during my learning, but to be stuck like this is not fun anymore.

My code say's the following:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ExperimentWithControls
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void numberTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
          
        }
    }
}

The book on the otherhand says:

Exception.jpg


And when i run the program i don't get the exception above but i get the following:

Run.jpg
 
Last edited by a moderator:
Given that the line of code that the book shows as throwing the exception does not appear in your code, it's no wonder your code doesn't throw that exception.
 
Given that the line of code that the book shows as throwing the exception does not appear in your code, it's no wonder your code doesn't throw that exception.
Yes i know, i can't seem to find why it's not in my code. I tried the excersise 3 times and i can't seem to get the code in there. I walked trough every step they show but still not the same lines there.
I can't imagine that i have to put it in by hand if they don't ask me to that in the book.
 
You have to put it there yourself. I don't know what the specific instructions are - the likelihood of us having that book is low and you haven't provided the instructions - but there's no reason for that line of code to appear of its own accord.
 
You have to put it there yourself. I don't know what the specific instructions are - the likelihood of us having that book is low and you haven't provided the instructions - but there's no reason for that line of code to appear of its own accord.
Ok....So that is something i still have to learn. I don't know yet which line will be placed automatic and which ones is have to put in by hand.
But when i put in the line:

{
number.Text = numberTextBox.Text;
}

it still juts runs the program without the exception. I can fill in the box, but it will not give me the exception.
 
There's not much we can do for you given that we have no idea what these instructions you're following actually are. The screen shot of the exception window explicitly says:
number was null.
If you're not seeing the exception then presumably number is not null, meaning that you have assigned something when you weren't expected to. Whether you didn't follow the instructions or the instructions were bad is not something we have enough information to determine.
 
So what value does number hold?

This is a prime learning experience, how to debug the unexpected. number has to be assigned, the question is where, thats up to you to find.
 
it still juts runs the program without the exception
That code appears in a TextChanged event handler for a textbox. In order to cause code in a TextChanged handler to actually execute, two things must be true:


1) you must change the text of the textbox, which causes the textbox to fire off any code associated with its textchanged event

2) the event handler code must actually be wired up to the event of the control (it's easy for this linkage to disappear but for the TextChanged handler code to remain in the form's .cs file). Writing code of "public void blahChanged...." does not automatically associate the code with the event of the control. You have to select the textbox in the designer view, and look int he proeprties window, in the lighning bolt list, and verify that your blahChanged is wired up to the TextChanged event. The fastest way to register a textchanged event for a textbox is to double click on the textbox in the deisgn view. VS will create an empty code block, and wire it up to the textchanged event, and transport you to code view ready to write the logic to handle the event
 
There's not much we can do for you given that we have no idea what these instructions you're following actually are. The screen shot of the exception window explicitly says:

If you're not seeing the exception then presumably number is not null, meaning that you have assigned something when you weren't expected to. Whether you didn't follow the instructions or the instructions were bad is not something we have enough information to determine.
Correct, and that's what i did without knowing it. The order is worng in the book. The book tells me what to make and in which order, but when i looked closer in the book the order was different. That's why my code initialyzed the textBlock because of the "right" order.
 

Attachments

  • Exception2.jpg
    Exception2.jpg
    46.4 KB · Views: 21
  • TextBlock.jpg
    TextBlock.jpg
    39.3 KB · Views: 21
So what value does number hold?

This is a prime learning experience, how to debug the unexpected. number has to be assigned, the question is where, thats up to you to find.

That code appears in a TextChanged event handler for a textbox. In order to cause code in a TextChanged handler to actually execute, two things must be true:


1) you must change the text of the textbox, which causes the textbox to fire off any code associated with its textchanged event

2) the event handler code must actually be wired up to the event of the control (it's easy for this linkage to disappear but for the TextChanged handler code to remain in the form's .cs file). Writing code of "public void blahChanged...." does not automatically associate the code with the event of the control. You have to select the textbox in the designer view, and look int he proeprties window, in the lighning bolt list, and verify that your blahChanged is wired up to the TextChanged event. The fastest way to register a textchanged event for a textbox is to double click on the textbox in the deisgn view. VS will create an empty code block, and wire it up to the textchanged event, and transport you to code view ready to write the logic to handle the event
I know that i had to find out the issue, but when i looked further in the book i came up with the next pages. The page on the right should be in front of it all, because i cannot know yet that i have to write this code. Without the code on this page i could not get the exception. When i looked at these pages i saw what the problem was and how to "fix" it.
 

Attachments

  • Book.jpg
    Book.jpg
    332.3 KB · Views: 28
Last edited:

Latest posts

Back
Top Bottom