0

Learning C# and hit a snag.

Why isn't the variable 'number' used here?

    public partial class Form1 : Form
    {

    static string labelText = "";
    static string number = "";

    public Form1()                                  
    {
        InitializeComponent();
    }

    private void serialNumber_TextChanged(object sender, EventArgs e)
    {
        string number = this.serialNumber.Text;
    }

I keep getting a warning that field 'number' is assigned but not used.

8
  • Because you're not using it, you're just assigning it -- just like the error says. Commented May 31, 2013 at 14:13
  • It means what it says. You have assigned it but you are not using it anywhere. Its the compiler's way of saying you don't need to assign it if you are not going to use it. Commented May 31, 2013 at 14:13
  • You are masking your class variable in a local scope. Commented May 31, 2013 at 14:14
  • Because you are only assign a value to it (string number = this.serialNumber.Text;), but you never use it in any operation. Commented May 31, 2013 at 14:14
  • 2
    Lots of fastest-gun-in-the-west answers being posted... We would all be much better off if prospective answerers took time to write something detailed and instructive. The race for being first is over, everyone lost. Commented May 31, 2013 at 14:15

7 Answers 7

3

string number = this.serialNumber.Text; this line creates a new string.

try this to avoid the warning

public partial class Form1 : Form {

static string labelText = "";
static string number = "";

public Form1()                                  
{
    InitializeComponent();
}

private void serialNumber_TextChanged(object sender, EventArgs e)
{
    number = this.serialNumber.Text;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Lots of you had the answer. I do want the static portion of the declaration, as far as I know. I want the latest value to be remembered for use in other parts of my code.
2

string number declares a new local variable which hides the static member variable.

Comments

1

Change these lines:

static string number = "";

private void serialNumber_TextChanged(object sender, EventArgs e)
{
    string number = this.serialNumber.Text;
}

to

private string number = "";

private void serialNumber_TextChanged(object sender, EventArgs e)
{
    number = this.serialNumber.Text;
}

1 Comment

It is still a class level field. I just said to update the lines to the code I wrote.
1

In your serialNumber_TextChanged method you declare a local variable called number. So if that is your complete code you never actually assign anything to Form1.number apart of the static initialization.

Comments

0

That's exactly what's happening: you are assigning a value to the variable number, and then you don't do anything with that variable.

Comments

0

First off, the warning is valid, and relates to the static member, which, in fact, is assigned and never used. The one in serialNumber_TextChanged is local to that method and by definition, different.

This: "Why isn't the variable 'number' used here?"...I do not understand.

2 Comments

There's another number variable at class-scope that is being hidden by the local number variable. That's the other number variable he's talking about.
@CodyGray Probably, but that's what I'm asking OP to clarify.
0

It happens because of the string in the instruction string number = this.serialNumber.Text;, which is declaring a new variable, different from the class field, despite having the same name. Remove the string modifier and the instruction will refer to the class field already declared.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.