-2

my problem is that a is never assigned to,and will have its default value 0

public partial class Form1 : Form
{
    int a, b, c, d, thickness;
   
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        a = Convert.ToInt16(textBox1.Text);
    }

  private void button1_Click_1(object sender, EventArgs e)
    {
        
        Pen blackpen = new Pen(colorDialog1.Color);

        blackpen.Width = thick;
        drawline1.DrawLine(blackpen, a);
    }

the program run but not working its like that dosnt get numbers i gave it to it it works like this tho

    {
        a = Convert.ToInt16(textBox1.Text);
   
 
        drawline1.DrawLine(blackpen, a;
    }
  • These are warnings the compiler uses to tell you about unused variables. From your posted code… `b`, `c`, `d` and `thickness` are never used. The code should still run… it’s just a warning. – JohnG Dec 13 '20 at 08:22
  • I suggest you peruse the SO [Tour](https://stackoverflow.com/tour) section as it shows how SO works. The [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) may help. In addition, you may find the SO [Asking](https://stackoverflow.com/help/asking) section useful. – JohnG Dec 13 '20 at 08:23
  • 1
    You're shadowing `a` in `textBox1_TextChanged`. By that I mean: `int a = ...` creates a new variable called `a`. That variable is completely unrelated to the field `int a` declared outside of the method. You should replace `int a = ` with `a = ` if you want to assign a value to the class-level field. – ProgrammingLlama Dec 13 '20 at 08:29
  • 1
    By the way, if `textBox1.Text` is empty, you'll also get an error from `Convert.ToInt16` (_"Input string was not in a correct format."_). You should either check if it's empty before performing the conversion, or (and this is the better option) use [`Int16.TryParse`](https://learn.microsoft.com/en-us/dotnet/api/system.int16.tryparse?view=net-5.0) instead (see the link for examples on how to use this). – ProgrammingLlama Dec 13 '20 at 08:31
  • 1
    Based on your latest edit, it does sound like your event handler isn't linked up for textBox1_Changed, as Olivier said. Try setting a breakpoint on it to see if it gets called when you type. – ProgrammingLlama Dec 13 '20 at 08:40

1 Answers1

1

You need to write:

a = Convert.ToInt16(textBox1.Text);

Because if you write:

int a = Convert.ToInt16(textBox1.Text);

It creates a local variable named a that masks the class member a so you don't assign the class variable a used in the other method, but the local.

Also you should care about how you name the types, classes, members and variables to avoid such thing:

C# Coding Standards and Naming Conventions

General Naming Conventions

C# Coding Conventions (C# Programming Guide)

For example, you may write for example:

int Value1;
int Value2;
int Value3;
int Value4;
int Thickness;

By changing Value with the real word that design what the data do or is used for.

Also you can manage errors thrown by Convert or use int.TryParse and show a message too.

  • tnx but i just noticed that and changed it but still not working – Mehdi Pooladi Dec 13 '20 at 08:33
  • You're presumably still getting similar _warnings_ because you're not assigning values to (or indeed using) `b`, `c`, `d`, or `thickness` either. They are just that though: warnings. – ProgrammingLlama Dec 13 '20 at 08:34
  • @evanhansen Is the handler assigned to the textbox event using designer or by code? Maybe you lost the link, check it in the control property editor. –  Dec 13 '20 at 08:43