-4

I have two issues with this piece of code. I 'm having trouble because the submit button event doesn't recognize the variable calculated in the text box event, and because the text box event isn't recognizing my if statements as statements. You can see where I'm having trouble in the comments below.

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;

 namespace WindowsFormsApplication11 
 {
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        int points;

        int userInput = int.Parse(textBox1.Text);

        if (userInput == 0)

        {
            points == 5; //I CANNOT COMPILE BECAUSE APPARENTLY I AM NOT ALLOWED TO USE
                         THIS AS A STATEMENT? 
        }

        if (userInput == 1)

        { 
            points == 10;
        }

        if (userInput == 2)

        {
            points == 20;
        } 

        if (userInput ==3)

        {
            points == 30;
        }

        else

        {
            points == 40;

        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show = ("You have been awarded" + textBox1.points + "points");
    } //I WANT TO BE ABLE TO RETRIEVE THE POINTS CALCULATED USING THE CALCULATION IN
      TEXT BOX, BUT I CANNOT COMPILE THE BUTTON EVENT DOES NOT RECOGNIZE THE POINTS
      VARIABLE



    private void label1_Click(object sender, EventArgs e)
    {

    }
 }
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Kerry G
  • 917
  • 4
  • 12
  • 21
  • 1
    Use single = as a assignment operator; when you declkared variable inside method - it is local variable, to use it in other parts of programm this variable has to be global - declare it outside of methods body – JleruOHeP Sep 27 '12 at 07:10
  • 2
    [Assignment](http://msdn.microsoft.com/en-us/library/sbkb459w.aspx). But there are several issues with this code that just relate to basic C# syntax, I don't know where to begin. – Damien_The_Unbeliever Sep 27 '12 at 07:11

5 Answers5

5

The == symbol is a comparison symbol not an assignment symbol

You need to use

if (userInput == 2) // this is a comparison
{ 
    points = 20; // this is an assignment
} 
Ria
  • 10,237
  • 3
  • 33
  • 60
1

First you have declared points local to the TextChanged event, so it won't be accessible in your button click event.

textBox1.points is not right since int points declaration has nothing to do with the TextBox, you may declare points as a class variable, something like

public partial class Form1 : Form
{
    int points =0;
    public Form1()
    {
       InitializeComponent();
    }
    //......// 

this would then work out as

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show( string.Format("You have been awarded {0} points",this.points));
}

Also you do assignment using = sign, so points = 5; would be the right thing to do

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
0

To add value to variable you should write:

points = 5;
0
using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;



namespace WindowsFormsApplication11 
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

public  int points=0;

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {


        int userInput = int.Parse(textBox1.Text);

        if (userInput == 0)

        {
            points = 5; 
        }

        if (userInput == 1)

        { 
            points = 10;
        }

        if (userInput == 2)

        {
            points = 20;
        } 

        if (userInput ==3)

        {
            points = 30;
        }

        else

        {
            points = 40;

        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show = ("You have been awarded" + points.ToString() + "points");
    } 



    private void label1_Click(object sender, EventArgs e)
    {

    }
 }
}
m4ngl3r
  • 552
  • 2
  • 17
  • I can compile with this, but if I change MessageBox.Show to textBox2.Text nothing appears after pressing Button 1. – Kerry G Sep 27 '12 at 08:23
0

As already stated - you confussed with assignment operator and with global/local variables.

But there is several other "errors" in your code. User input might be simple text - so there will be exception, you should use int.TryParse instead of int.Parse. Also there are plenty of ifs in your code - but they cannot fire all together, I recomend use switch. And of course you should try to name your constants somehow, it will make your code much more readable!

Overral your code might look like this:

int pointsAvarded = 0;

private void textBox1_TextChanged(object sender, EventArgs e)
{
    pointsAvarded = 0; //so you can be sure to use the latest input
    int userInput = 0;
    if (int.TryParse(textBox1.Text, out userInput))
      switch (userInput)
      {
          case 0:
            points = 5;
            break;
          case 1:
            points = 10;
            break;
          ...
          default:
            points = 40;
            break;
      }
}

private void button1_Click(object sender, EventArgs e)
{
    if (pointsAvarded != 0)
      MessageBox.Show("You have been awarded" + pointsAvarded + "points");
}
JleruOHeP
  • 10,106
  • 3
  • 45
  • 71