Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6027771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:38:29+00:00 2026-05-23T04:38:29+00:00

Below you can see my .xaml.cs code. The app opens fine. There are 4

  • 0

Below you can see my .xaml.cs code. The app opens fine. There are 4 textboxes which the user can change. When you edit one of the default values in the textboxes and then click off to not select it, the app crashes with the System.StackOverflowException error, saying I have an infinite loop or recursion on either the get{} or Calc() function, depending on which textbox is edited. I want the app to calculate the numbers according to the Calc() function every time a textbox is not being edited. I also would like to define some starting values for the textboxes, but haven’t figured out how to do that yet.

Any ideas how I can fix the loop error and initial value defines?

The textboxes in the .xaml code follow this pattern: Text="{Binding DistanceBox}"

The full behind code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace ConverterApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new Variables();
        }
    }

    public class Variables : INotifyPropertyChanged
    {
        // Declare the PropertyChanged event.
        public event PropertyChangedEventHandler PropertyChanged;

        private double m_distanceBox;
        public double DistanceBox
        {
            get { return m_distanceBox; }
            set
            {
                //If value hasn't changed, don't do anything
                //if (m_distanceBox == value)
                //    return;
                m_distanceBox = value;
                // modify calc to read the text values
                Calc();
                // Call NotifyPropertyChanged when the source property
                // is updated.
                //if(PropertyChanged!= null)
                    NotifyPropertyChanged("DistanceBox");
            }
        }

        private double m_widthBox;
        public double WidthBox
        {
            get { return m_widthBox; }
            set
            {
                m_widthBox = value;
                // modify calc to read the text values
                Calc();
                // Call NotifyPropertyChanged when the source property
                // is updated.
                NotifyPropertyChanged("WidthBox");
            } 
        }

        private double m_lengthBox;
        public double LengthBox
        {
            get { return m_lengthBox; }
            set
            {
                m_lengthBox = value;
                // modify calc to read the text values
                Calc();
                // Call NotifyPropertyChanged when the source property
                // is updated.
                NotifyPropertyChanged("LengthBox");
            }
        }

        private double m_lensBox;
        public double LensNeeded
        {
            get { return m_lensBox; }
            set
            {
                m_lensBox = value;
                // modify calc to read the text values
                Calc();
                // Call NotifyPropertyChanged when the source property
                // is updated.
                NotifyPropertyChanged("LensNeeded");
            }
        }

        public void Calc()
        {
            double WidthBased = 2.95 * (DistanceBox / WidthBox);
            double LengthBased = 3.98 * (DistanceBox / LengthBox);

            if (WidthBased < LengthBased)
            {
                LensNeeded = Math.Round(WidthBased,2);
            }else{
                LensNeeded = Math.Round(LengthBased,2);
            }
        }

        // NotifyPropertyChanged will raise the PropertyChanged event,
        // passing the source property that is being updated.
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:38:30+00:00Added an answer on May 23, 2026 at 4:38 am

    In your setter of LensNeeded you call Calc, calc in turn sets LensNeeded, the setter of LensNeeded calls Calc, etc. this circularity leads to the StackOverflow.

    Do not calculate properties in setters like that, make the respective properties “virtual” (calculate the value on the fly rather than getting the value from a backing field), e.g.

    public double WidthBased
    {
        get { return 2.95 * (DistanceBox / WidthBox); }
    }
    

    To make the bindings to such properties update you can raise property changed events in all related properties’ setters, here that would be the setters of DistanceBox and WidthBox.

    private double m_distanceBox;
    public double DistanceBox
    {
        get { return m_distanceBox; }
        set
        {
            if (m_distanceBox != value)
            {
                m_distanceBox = value;
                NotifyPropertyChanged("DistanceBox");
                NotifyPropertyChanged("WidthBased");
            }
        }
    }
    

    Initial values could be set in the contructor of your variables class or directly in the field declarations, e.g.

    private double m_distanceBox = 10;
    public double DistanceBox //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code below. As you can see I am passing two variables
I have some really funky code. As you can see from the code below
I can't get the code below to compile (see errors). Advice on correction would
I have a table defined (see code snippet below). How can I add a
Can't seem to figure this out, see code below. Trying to make a GET
How can I implement this one line XAML programmatically in code? (as I'm needing
As you can see below, in the constructor I'm instantiating a validation object so
I'm using graphviz (dot) to generate the graph you can see below. The node
i have a pretty complicated form. as you can see below: alt text http://img9.imageshack.us/img9/2465/test2xk.jpg
Trying not to lose it here. As you can see below I have assigned

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.