C# [C#] Multiplication table program hanging

AI Thread Summary
The discussion focuses on a C# program for generating multiplication tables, which hangs when large values are inputted. The main issues identified include the use of global variables instead of local ones, and the inefficiency of using a ListBox for large datasets, which can exceed Windows' 64KB limit. Participants suggest using a DataGridView for better handling of large data and emphasize the importance of avoiding unnecessary boolean comparisons in code. Recommendations include simplifying the TryParse usage and limiting variable scope to prevent side effects. Overall, the conversation highlights best practices for efficient programming in C#.
adjacent
Gold Member
Messages
1,552
Reaction score
62
This is my code:
Code:
double of = 0;
        double from = 0;
        double to = 0;
        double ans = 0;
      
        private void button1_Click(object sender, EventArgs e)
        {
            Table.Items.Clear();
            if (double.TryParse(Of.Text, out of) == true && double.TryParse(From.Text, out from) == true && double.TryParse(To.Text, out to) == true)
            {
                of = double.Parse(Of.Text);
                from = double.Parse(From.Text);
                to = double.Parse(To.Text);

                for (double i = from; i <= to; i++)
                {
                    ans = i * of;
                    Table.Items.Add(of + " x " + i + " = " + ans.ToString());
                }
                
            }
            else
            {
                MessageBox.Show("Please enter a valid value");
            }
        }
When I write a large value like 1000000 in the To.Textbox, the software freezes. I don't know why. If the computer is strong enough, it should be able to calculate it right?
 
Technology news on Phys.org
adjacent said:
This is my code:
Code:
double of = 0;
        double from = 0;
        double to = 0;
        double ans = 0;
      
        private void button1_Click(object sender, EventArgs e)
        {
            Table.Items.Clear();
            if (double.TryParse(Of.Text, out of) == true && double.TryParse(From.Text, out from) == true && double.TryParse(To.Text, out to) == true)
            {

                of = double.Parse(Of.Text);
                from = double.Parse(From.Text);
                to = double.Parse(To.Text);

                for (double i = from; i <= to; i++)
                {
                    ans = i * of;
                    Table.Items.Add(of + " x " + i + " = " + ans.ToString());
                }
                
            }
            else
            {
                MessageBox.Show("Please enter a valid value");
            }
        }

Code:
                of = double.Parse(Of.Text);
                from = double.Parse(From.Text);
                to = double.Parse(To.Text);
You don't need this part. TryParse already stored the value.

What is the datatype of "Table"? Is it a GUI object? If so, use something more lightweight like a list or dataset and then bind the GUI object to that.

Also, it looks like you have of, from, to and ans defined as global variables. They should probably be local.
 
Last edited:
DavidSnider said:
You don't need this part. TryParse already stored the value.
Oh. Thanks. I was having some problems with tryparse-the first time using it.
DavidSnider said:
What is the datatype of "Table"? Is it a GUI object? If so, use something more lightweight like a list or dataset and then bind the GUI object to that.
It is a list box.
DavidSnider said:
Also, it looks like you have of, from and to and ans defined as global variables. They should probably be local.
Why is it global? How can I make it local and why should I?
DavidSnider said:
A million integers is a not a big deal. A million GUI objects is. So typically you'll do the heavy lifting in lightweight datastructures and then have your GUI objects sample a small portion of that dataset.
I am not adding any GIUs there, just adding a string to the list object
 
adjacent said:
Oh. Thanks. I was having some problems with tryparse-the first time using it.

It is a list box.

Why is it global? How can I make it local and why should I?

I am not adding any GIUs there, just adding a string to the list object

You can make it local by defining it inside the function that uses it. You should try to limit the scope of variables as much as possible to avoid unwanted side effects of functions sharing state.

When you add an item to a listbox that adds a listboxitem gui element. For large datasets you generally shouldn't load everything at once.
 
DavidSnider said:
When you add an item to a listbox that adds a listboxitem gui element. For large datasets you generally shouldn't load everything at once.

What should I do then? I am very new to C# and programming.
 
adjacent said:
What should I do then? I am very new to C# and programming.

The ListBox control is not really well suited to large data sets. Try something like a DataGridView that supports paging.
 
From this page, http://msdn.microsoft.com/en-us/library/ms997541.aspx, "Windows limits the total amount of text in a list box to 64 kilobytes."

One million strings is obviously going to exceed that 64k limit.A comment about your code: DavidSnider is right. Avoid global variables.

Another issue that marks you as new to programming is your comparing a boolean to true (e.g., double.TryParse(Of.Text, out of) == true ) -- Why do that?

Or, taking it to the other extreme, why stop with just one comparison? == is an operator, just like +,-,*, and /. The result of the operator is a boolean. So why not use (double.TryParse(Of.Text, out of) == true) == true, or ((double.TryParse(Of.Text, out of) == true) == true) == true, or ...

Or better yet, just use double.TryParse(Of.Text, out of). There is no need to compare the result to true.
 
D H said:
Another issue that marks you as new to programming is your comparing a boolean to true (e.g., double.TryParse(Of.Text, out of) == true ) -- Why do that?
Yes. If it can't be parsed as a double, then the TryParse should return false.I want only doubles here
D H said:
== is an operator The result of the operator is a boolean. So why not use (double.TryParse(Of.Text, out of) == true) == true, or ((double.TryParse(Of.Text, out of) == true) == true) == true, or ...
What? I don't understand at all. Should I use = instead of ==?
D H said:
Or better yet, just use double.TryParse(Of.Text, out of). There is no need to compare the result to true.
Oh. I have seen people using that. If a boolean is true, we can simply just write that?
 
adjacent said:
Oh. I have seen people using that. If a boolean is true, we can simply just write that?
You missed my point. Omit the == true. There is no need for that comparison, and it marks you as a noob. There's no reason to compare a function that returns a boolean to a boolean, or a boolean variable to a boolean. For a function that returns a boolean, just use

if (boolean_function (argument_list)) { ... }

For a boolean variable, just use

if (boolean_variable) { ... }The above applies to those situations where you want to perform the body of the if statement when the boolean function returns true or when the boolean variable is true. If you want to perform the body of the if statement if the function returns false, use

if (! boolean_function (argument_list)) { ... }

Similarly, for a boolean variable, if you want to perform the body when the variable is false, use

if (! boolean_variable) { ... }
 
  • #10
I have seen people using if(boolean_variable=false) too. That's in no way different than using !boolean_variable IMO
P.S can you tell me how I should use Datagridview here?
 
  • #11
adjacent said:
I have seen people using if(boolean_variable=false) too. That's in no way different than using !boolean_variable IMO
Your question shows one reason to avoid that. Obviously you know the difference between "==false" and "=false", but that bug is SO hard to spot (don't ask me how I know). It adds an unnecessary and treacherous bit of code that you should avoid if you can.

[EDIT]I just noticed that you asked earlier about "==" versus "=" in the logic. Your original version, "==", was correct. The single "=" is a bug that I have introduced several times and regretted it. It makes an assignment to the left side variable and is always true in a logic statement.
 
Last edited:
  • #12
Oh. I have corected this boolean things. Now please help me with this DataGridView thing
 
Back
Top