locked
windows phone app crashing RRS feed

  • Question

  • Hey guys. Im kind of new at the whole app development scene for windows phone and C# in general. Any pointers on how I can make it so only the dark theme works in my app, and also point out how to make it so my app doesnt crash every time someone executes a calculation with an empty textbox. Thanks.

     

        private void button5_Click(object sender, RoutedEventArgs e)
        {
          decimal y2, y1, x2, x1;
    
          y2 = decimal.Parse(textBox11.Text);
          y1 = decimal.Parse(textBox12.Text);
          x2 = decimal.Parse(textBox13.Text);
          x1 = decimal.Parse(textBox14.Text);
    
          decimal step1 = y2 - y1;
          decimal step2 = x2 - x1;
          decimal step3 = step1 / step2;
          textBlock29.Text = "Slope = " + step3;
    
    
    
        }

    Tuesday, July 19, 2011 9:39 AM

Answers

  • Hi aarhus,

    Try to use Try catch chamber:

    private void button5_Click(object sender, EventArgs e)
        {
          try
          {
            decimal y2, y1, x2, x1;
    
            y2 = decimal.Parse(textBox11.Text);
            y1 = decimal.Parse(textBox12.Text);
            x2 = decimal.Parse(textBox13.Text);
            x1 = decimal.Parse(textBox14.Text);
    
            decimal step1 = y2 - y1;
            decimal step2 = x2 - x1;
            decimal step3 = step1 / step2;
            textBlock29.Text = "Slope = " + step3;
          }
          catch (DivideByZeroException)
          {
            MessageBox.Show("Attempted to divide by zero");
          }
          catch (FormatException)
          {
            MessageBox.Show("Input string was not in a correct format!");
          }
        }
    


    or try this link:

    http://www.functionx.com/vcsharp/index.htm

    Hope it helps.

    Hardz

    • Marked as answer by aarhus Tuesday, July 19, 2011 8:52 PM
    Tuesday, July 19, 2011 10:07 AM

All replies

  • Hello aarus,

    Hey guys. Im kind of new at the whole app development scene for windows phone and C# in general. Any pointers on how I can make it so only the dark theme works in my app, and also point out how to make it so my app doesnt crash every time someone executes a calculation with an empty textbox. Thanks.

     

     

     private void button5_Click(object sender, RoutedEventArgs e)
     {
      decimal y2, y1, x2, x1;
     y2 = decimal.Parse(textBox11.Text);
     y1 = decimal.Parse(textBox12.Text);
     x2 = decimal.Parse(textBox13.Text);
     x1 = decimal.Parse(textBox14.Text);
    
     decimal step1 = y2 - y1;
     decimal step2 = x2 - x1;
     decimal step3 = step1 / step2;
     textBlock29.Text = "Slope = " + step3;
     }

     


    try to use Decimal.TryParse , see this example

     private void button5_Click(object sender, RoutedEventArgs e)
     {
      decimal y2, y1, x2, x1;
    
      if (decimal.TryParse(textBox11.Text,y2))
      {
      // your code
      }
    
      if (decimal.TryParse(textBox12.Text, y1))
      {
      // your code
      }
    
      if (decimal.TryParse(textBox13.Text, x2))
      {
      // your code
      }
    
      if (decimal.TryParse(textBox14.Text, x1))
      {
      // your code
      }
    

    more information here http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx

    Bye
     


     http://community.visual-basic.it/carmelolamonica/

    • Proposed as answer by Hardz Tarrayo Wednesday, July 20, 2011 12:20 AM
    Tuesday, July 19, 2011 9:46 AM
  • Hi aarhus,

    Try to use Try catch chamber:

    private void button5_Click(object sender, EventArgs e)
        {
          try
          {
            decimal y2, y1, x2, x1;
    
            y2 = decimal.Parse(textBox11.Text);
            y1 = decimal.Parse(textBox12.Text);
            x2 = decimal.Parse(textBox13.Text);
            x1 = decimal.Parse(textBox14.Text);
    
            decimal step1 = y2 - y1;
            decimal step2 = x2 - x1;
            decimal step3 = step1 / step2;
            textBlock29.Text = "Slope = " + step3;
          }
          catch (DivideByZeroException)
          {
            MessageBox.Show("Attempted to divide by zero");
          }
          catch (FormatException)
          {
            MessageBox.Show("Input string was not in a correct format!");
          }
        }
    


    or try this link:

    http://www.functionx.com/vcsharp/index.htm

    Hope it helps.

    Hardz

    • Marked as answer by aarhus Tuesday, July 19, 2011 8:52 PM
    Tuesday, July 19, 2011 10:07 AM
  • Thank you very much Hardz, I have been trying to understand how catch works for a while now.
    Tuesday, July 19, 2011 8:53 PM