[C#] Multiplication table program hanging

Click For Summary

Discussion Overview

The discussion revolves around a C# program for generating a multiplication table, specifically addressing issues related to performance and code structure. Participants explore the implications of using large input values and the efficiency of GUI components when handling large datasets.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the program freezes when a large value (e.g., 1000000) is entered, questioning why this occurs if the computer is capable of handling it.
  • Another participant suggests that the use of global variables for calculations may not be optimal and proposes making them local to avoid unwanted side effects.
  • There is a discussion about the suitability of the ListBox control for large datasets, with suggestions to use a DataGridView instead, which supports paging.
  • Participants express confusion over the use of boolean comparisons in the code, with some suggesting that the comparison to true is unnecessary and marks the coder as inexperienced.
  • One participant highlights the potential for bugs when using assignment instead of comparison in conditional statements, emphasizing the importance of clarity in code.

Areas of Agreement / Disagreement

Participants generally agree that using global variables is not advisable and that the ListBox control may not be suitable for large datasets. However, there is no consensus on the best approach to handle large inputs or the specifics of implementing a DataGridView.

Contextual Notes

Limitations include the potential for exceeding the 64 kilobyte limit of a ListBox when handling large strings, and the unresolved nature of how to effectively implement a DataGridView in this context.

Who May Find This Useful

New C# programmers, developers interested in GUI performance optimization, and those looking to understand best practices in variable scope and data handling in programming.

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
 

Similar threads

  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 8 ·
Replies
8
Views
1K
Replies
20
Views
2K
Replies
8
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
63
Views
6K
  • · Replies 25 ·
Replies
25
Views
3K