[C#] Multiplication table program hanging

In summary, the conversation discusses code for a software that calculates financial amounts and has a bug when a large value is entered. The expert suggests using a lighter data type for the GUI object and making the global variables local for a more efficient program.
  • #1
adjacent
Gold Member
1,552
63
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
  • #2
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:
  • #3
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
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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?
 
  • #9
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
 

Related to [C#] Multiplication table program hanging

1. What is a multiplication table program in C#?

A multiplication table program in C# is a program that generates a table of multiplication values based on the user's input. It is commonly used as a learning tool for students to practice their multiplication skills.

2. Why is my multiplication table program hanging?

Your multiplication table program may be hanging due to a variety of reasons, such as an infinite loop, a logical error in the code, or insufficient memory. It is important to carefully review your code and debug any potential issues to resolve the hanging.

3. How can I optimize my multiplication table program in C#?

To optimize your multiplication table program in C#, you can use efficient algorithms and data structures, minimize the use of unnecessary loops, and avoid repetitive code. You can also consider using parallel processing techniques to improve the program's performance.

4. Can I customize the layout of the multiplication table in my program?

Yes, you can customize the layout of the multiplication table in your program by using formatting techniques, such as spacing and alignment, and by adding headings or borders to the table. You can also add color or other visual elements to make the table more visually appealing.

5. Is there a way to handle errors or invalid inputs in a multiplication table program?

Yes, you can handle errors or invalid inputs in a multiplication table program by using exception handling techniques. This allows you to anticipate and handle any unexpected errors or user input in a way that prevents the program from crashing or hanging.

Similar threads

  • Programming and Computer Science
Replies
8
Views
868
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
5
Views
857
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
Replies
63
Views
3K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
Back
Top