Beginner: string to int question

Kpoof

New member
Joined
Jan 10, 2022
Messages
3
Programming Experience
Beginner
Hello,

I have been all over the internet and booted by StackOverflow for trying to solve this but I cannot seem to turn a string into an integer using TryParse or Parse. I am trying to create a simple unique calculator for work that will calculate numbers entered on click. variable "uc2x4" is the string i wish to turn into an int. Thanks for any help
C#:
namespace Test
{
    public partial class Form1 : Form
    {
        string uc2x4 = "0";
        int x = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "0" && textBox1.Text != null)
            {
                uc2x4 = textBox1.Text;
                int x = Int32.Parse(uc2x4);
            }
            else
            {
                label1.Text = "no";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = x * 2;
           
        }
    }
}
 
Last edited by a moderator:
Your parsing is working correctly. The issue is that you are storing the parsed value into the local variable x declared on line 17 instead of the class member variae x.
 
Ten seconds of debugging would have shown you that the parsing was fine. Problem is you're just throwing away the result. And didn't the compiler give a warning about declaring x on line 17 ?
 
You're problem is basically similar to this:
C#:
int x = 23;
{
    int x = 42;
}
Console.WriteLine(x);

Notice the temporary variable on line 3 which is only good for the scope of lines 2-4.
 
You're problem is basically similar to this:
C#:
int x = 23;
{
    int x = 42;
}
Console.WriteLine(x);

Notice the temporary variable on line 3 which is only good for the scope of lines 2-4.
That code actually triggers a compiler error, not a warning as I thought it would 😁
But yeah, it sort of summarizes the problem.
In the original code, line 17 should have produced a warning about x being assigned but not used. That should have given a clue.
 
That code actually triggers a compiler error, not a warning as I thought it would 😁
Yup, we're old fashioned C/C++ programmers who would expect to see a warning from an "intelligent" compiler. :) Apparently the C# compiler goes beyond "intelligent" and outrightly marks this as an error.

Anyway, here's something that would compile:
C#:
public class Program
{
    int x = 23;

    void DoIt()
    {
        int x = 12;
        Console.WriteLine(x);
    }

    void Run()
    {
        DoIt();
        Console.WriteLine(x);
    }

    static void Main()
    {
        new Program().Run();
    }
}
 
Yes I seem to remember C allows redeclaring a variable in an inner block. Dangerous practice though, and C# is right IMO to put a stop to it.
But the above code, which compiles clean, seems equally dangerous. I thought the complier should at least issue a warning about hiding a class variable (I vaguely seem to remember seeing such warnings elsewhere, perhaps that was in Java).
 
Back
Top Bottom