Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C#] Multiplication table program hanging

  1. Jun 8, 2014 #1

    adjacent

    User Avatar
    Gold Member

    This is my code:
    Code (Text):
    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?
     
  2. jcsd
  3. Jun 8, 2014 #2

    DavidSnider

    User Avatar
    Gold Member

    Code (Text):

                    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: Jun 8, 2014
  4. Jun 8, 2014 #3

    adjacent

    User Avatar
    Gold Member

    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
     
  5. Jun 8, 2014 #4

    DavidSnider

    User Avatar
    Gold Member

    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.
     
  6. Jun 8, 2014 #5

    adjacent

    User Avatar
    Gold Member

    What should I do then? I am very new to C# and programming.
     
  7. Jun 8, 2014 #6

    DavidSnider

    User Avatar
    Gold Member

    The ListBox control is not really well suited to large data sets. Try something like a DataGridView that supports paging.
     
  8. Jun 8, 2014 #7

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  9. Jun 8, 2014 #8

    adjacent

    User Avatar
    Gold Member

    Yes. If it can't be parsed as a double, then the TryParse should return false.I want only doubles here
    What? I don't understand at all. Should I use = instead of ==?
    Oh. I have seen people using that. If a boolean is true, we can simply just write that?
     
  10. Jun 8, 2014 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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) { ... }
     
  11. Jun 8, 2014 #10

    adjacent

    User Avatar
    Gold Member

    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?
     
  12. Jun 8, 2014 #11

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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: Jun 8, 2014
  13. Jun 9, 2014 #12

    adjacent

    User Avatar
    Gold Member

    Oh. I have corected this boolean things. Now please help me with this DataGridView thing
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [C#] Multiplication table program hanging
  1. C program (Replies: 4)

  2. C program (Replies: 4)

  3. C Program (Replies: 3)

  4. C program (Replies: 0)

  5. C programming (Replies: 9)

Loading...