stackoverflowexception when trying to load viewlodel

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

45 Replies - 2726 Views - Last Post: 10 February 2016 - 02:10 AM Rate Topic: -----

#1 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

stackoverflowexception when trying to load viewlodel

Posted 29 January 2016 - 02:25 PM

Hi

I am trying to make my viewmodel 'VM_Enquiry_New' load as soon as the viewmodel 'VM_Enquiry' is loaded instead of when the user clicks a button but i am getting a stackoverflowexception:

An unhandled exception of type 'System.StackOverflowException' occurred

And i can't work out why because it works fine when the user clicks the button but when i add it to run as soon as 'VM_Enquiry' is loaded i get the stackoverflowexception.

My code is below:

VM_Enquiry:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using TrackingApplication.Models;
using TrackingApplication.Models.Code;
using System.Windows.Input;
using TrackingApplication.ViewModels;

namespace TrackingApplication.ViewModels.Enquiry
{
    class VM_Enquiry : VM_BASE
    {
        //Getting the Model for this ViewModel
        private M_Enquiry _M_Enquiry;

        public VM_Enquiry()
        {
            _M_Enquiry = new M_Enquiry();

            Load_New_Enquiry_Screen();
        }

        #region Variables from Model
        private VM_Enquiry _CurrentScreen_Enquiry;
        public VM_Enquiry CurrentScreen_Enquiry
        {
            get { return _CurrentScreen_Enquiry; }
            set
            {
                if (value != _CurrentScreen_Enquiry)
                {
                    _CurrentScreen_Enquiry = value;
                    onpropertychanged("CurrentScreen_Enquiry");
                }
            }
        }
        #endregion

        #region Commands
        //////// 'New Enquiry' button - Command ////////
        //////// Switches screen to 'New Enquiry' when the 'New Enquiry' button is pressed.
        public DelegateCommand<System.Windows.Point> SwitchScreen_New_Enquiry_Command
        {
            get { return _SwitchScreen_New_Enquiry_Command = new DelegateCommand<System.Windows.Point>(Execute_SwitchScreen_New_Enquiry_Command, CanExecute_SwitchScreen_New_Enquiry_Command); }
        }
        private DelegateCommand<System.Windows.Point> _SwitchScreen_New_Enquiry_Command;
        public bool CanExecute_SwitchScreen_New_Enquiry_Command(System.Windows.Point param)
        {
            //Always return true
            return true;
        }
        public void Execute_SwitchScreen_New_Enquiry_Command(System.Windows.Point e)
        {
            Load_New_Enquiry_Screen();
        }

        //////// 'Edit Enquiry' button - Command ////////
        //////// Switches screen to 'Edit/View Enquiry' when the 'Edit/View Enquiry' button is pressed.
        public DelegateCommand<System.Windows.Point> SwitchScreen_Edit_Enquiry_Command
        {
            get { return _SwitchScreen_Edit_Enquiry_Command = new DelegateCommand<System.Windows.Point>(Execute_SwitchScreen_Edit_Enquiry_Command, CanExecute_SwitchScreen_Edit_Enquiry_Command); }
        }
        private DelegateCommand<System.Windows.Point> _SwitchScreen_Edit_Enquiry_Command;
        public bool CanExecute_SwitchScreen_Edit_Enquiry_Command(System.Windows.Point param)
        {
            //Always return true
            return true;
        }
        public void Execute_SwitchScreen_Edit_Enquiry_Command(System.Windows.Point e)
        {
            Load_Edit_Enquiry_Screen();
        }

        //////// 'Enquiry Tools' button - Command ////////
        //////// Switches screen to 'Enquiry Tools' when the 'Enquiry Tools' button is pressed.
        public DelegateCommand<System.Windows.Point> SwitchScreen_Tools_Enquiry_Command
        {
            get { return _SwitchScreen_Tools_Enquiry_Command = new DelegateCommand<System.Windows.Point>(Execute_SwitchScreen_Tools_Enquiry_Command, CanExecute_SwitchScreen_Tools_Enquiry_Command); }
        }
        private DelegateCommand<System.Windows.Point> _SwitchScreen_Tools_Enquiry_Command;
        public bool CanExecute_SwitchScreen_Tools_Enquiry_Command(System.Windows.Point param)
        {
            //Always return true
            return true;
        }
        public void Execute_SwitchScreen_Tools_Enquiry_Command(System.Windows.Point e)
        {
            Load_Tools_Enquiry_Screen();
        }
        #endregion

        public void Load_New_Enquiry_Screen()
        {
            CurrentScreen_Enquiry = new ViewModels.Enquiry.VM_Enquiry_New();
        }

        public void Load_Edit_Enquiry_Screen()
        {
            CurrentScreen_Enquiry = new ViewModels.Enquiry.VM_Enquiry_Edit();
        }

        public void Load_Tools_Enquiry_Screen()
        {
            CurrentScreen_Enquiry = new ViewModels.Enquiry.VM_Enquiry_Tools();
        }
    }
}



VM_Enquiry_New:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using TrackingApplication.Models;
using TrackingApplication.Models.Code;
using System.Windows.Input;
using TrackingApplication.ViewModels;
using System.Windows;
using TrackingApplication.SQL;

namespace TrackingApplication.ViewModels.Enquiry
{
    class VM_Enquiry_New : VM_Enquiry
    {
        //Getting the Model for this ViewModel
        private M_Enquiry _M_Enquiry;

        public VM_Enquiry_New()
        {
            _M_Enquiry = new M_Enquiry();
        }

        #region Variables from Model - also executes '_Enquiry_Submit_Command.RaiseCanExecuteChanged();' when anything gets chnaged.
        //Getting the variables from the Model and declaring them in the ViewModel
        public string Enquiry_ContactName
        {
            get { return _M_Enquiry.Enquiry_ContactName; }
            set
            {
                if (value != _M_Enquiry.Enquiry_ContactName)
                {
                    _M_Enquiry.Enquiry_ContactName = value;
                    onpropertychanged("Enquiry_ContactName");
                    _Enquiry_Submit_Command.RaiseCanExecuteChanged();
                }
            }
        }

        public string Enquiry_Notes
        {
            get { return _M_Enquiry.Enquiry_Notes; }
            set
            {
                if (value != _M_Enquiry.Enquiry_Notes)
                {
                    _M_Enquiry.Enquiry_Notes = value;
                    onpropertychanged("Enquiry_Notes");
                    _Enquiry_Submit_Command.RaiseCanExecuteChanged();
                }
            }
        }

        public string Enquiry_CompanySize
        {
            get { return _M_Enquiry.Enquiry_CompanySize; }
            set
            {
                if (value != _M_Enquiry.Enquiry_CompanySize)
                {
                    _M_Enquiry.Enquiry_CompanySize = value;
                    onpropertychanged("Enquiry_CompanySize");
                    _Enquiry_Submit_Command.RaiseCanExecuteChanged();
                }
            }
        }

        public string Enquiry_ContactEmail
        {
            get { return _M_Enquiry.Enquiry_ContactEmail; }
            set
            {
                if (value != _M_Enquiry.Enquiry_ContactEmail)
                {
                    _M_Enquiry.Enquiry_ContactEmail = value;
                    onpropertychanged("Enquiry_ContactEmail");
                    _Enquiry_Submit_Command.RaiseCanExecuteChanged();
                }
            }
        }

        public string Enquiry_CompanyName
        {
            get { return _M_Enquiry.Enquiry_CompanyName; }
            set
            {
                if (value != _M_Enquiry.Enquiry_CompanyName)
                {
                    _M_Enquiry.Enquiry_CompanyName = value;
                    onpropertychanged("Enquiry_CompanyName");
                    _Enquiry_Submit_Command.RaiseCanExecuteChanged();
                }
            }
        }

        public string Enquiry_ContactPhone
        {
            get { return _M_Enquiry.Enquiry_ContactPhone; }
            set
            {
                if (value != _M_Enquiry.Enquiry_ContactPhone)
                {
                    _M_Enquiry.Enquiry_ContactPhone = value;
                    onpropertychanged("Enquiry_ContactPhone");
                    _Enquiry_Submit_Command.RaiseCanExecuteChanged();
                }
            }
        }
        #endregion

        #region Command
        //////// 'Submit' button - Command ////////
        //////// Submits the Enquiry to the database.
        public DelegateCommand<System.Windows.Point> Enquiry_Submit_Command
        {
            get { return _Enquiry_Submit_Command = new DelegateCommand<System.Windows.Point>(Execute_Enquiry_Submit_Command, CanExecute_Enquiry_Submit_Command); }
        }
        private DelegateCommand<System.Windows.Point> _Enquiry_Submit_Command;
        public bool CanExecute_Enquiry_Submit_Command(System.Windows.Point param)
        {
            //Return true if Enquiry_CompanyName is not null
            return Enquiry_ContactName != null & Enquiry_Notes != null & Enquiry_CompanySize != null & Enquiry_ContactEmail != null & Enquiry_CompanyName != null & Enquiry_ContactPhone != null;
        }
        public void Execute_Enquiry_Submit_Command(System.Windows.Point e)
        {
            Enquiry_Submit();
        }
        #endregion

        public void Enquiry_Submit()
        {
            if (Enquiry_ContactName != null && Enquiry_CompanySize != null && Enquiry_ContactEmail != null && Enquiry_CompanyName != null && Enquiry_ContactPhone != null)
            {
                SQL_Enquiry SQLForEnquiry = new SQL_Enquiry();

                bool InsertSuccessful = SQLForEnquiry.InsertNewEnquiry(Enquiry_ContactName, Enquiry_Notes, Enquiry_CompanySize, Enquiry_ContactEmail, Enquiry_CompanyName, Enquiry_ContactPhone);

                if (InsertSuccessful == true)
                {
                    MessageBox.Show("New enquiry created successfully.");

                    //Set everyhting to null
                    Enquiry_CompanyName = null;
                    Enquiry_CompanySize = null;
                    Enquiry_ContactEmail = null;
                    Enquiry_ContactName = null;
                    Enquiry_ContactPhone = null;
                    Enquiry_Notes = null;
                }
            }
            else
            {
                MessageBox.Show("You have not completed all of the required boxes." + Environment.NewLine + Environment.NewLine + "Please make sure you have entered data in all of them." + Environment.NewLine + Environment.NewLine + "Then try again.");
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: stackoverflowexception when trying to load viewlodel

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: stackoverflowexception when trying to load viewlodel

Posted 29 January 2016 - 02:30 PM

It would be super helpful to see where exactly the error is occurring, since you've posted 250+ lines of code.
Was This Post Helpful? 0
  • +
  • -

#3 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: stackoverflowexception when trying to load viewlodel

Posted 29 January 2016 - 02:40 PM

In 'VM_Enquiry' as soon as i change this:

public VM_Enquiry()
        {
            _M_Enquiry = new M_Enquiry();
        }


to this:

public VM_Enquiry()
        {
            _M_Enquiry = new M_Enquiry();

            Load_New_Enquiry_Screen();
        }


and i get:

An unhandled exception of type 'System.StackOverflowException' occurred

on this line:

public VM_Enquiry()

This post has been edited by danbywinby: 29 January 2016 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,228
  • Joined: 12-December 12

Re: stackoverflowexception when trying to load viewlodel

Posted 29 January 2016 - 02:53 PM

A stack overflow is most often caused by cyclical calls. Trace through your code starting from the call Load_New_Enquiry_Screen() in that constructor and you should be able to discover where code starts to repeat. Or set a breakpoint and step through your code.

Debugging Express

(I will also suggest that you are over-using the word Enquiry.)
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,228
  • Joined: 12-December 12

Re: stackoverflowexception when trying to load viewlodel

Posted 29 January 2016 - 03:03 PM

It is also worth emphasising that a derived class will call its parent's constructor.
    class ParentClass {
        public ParentClass() {
            Console.WriteLine("Parent cons.");
        }
    }
    class ChildClass : ParentClass {
        public ChildClass() {
            Console.WriteLine("Child cons.");
        }
    }

    class Program {
        static void Main(string[] args) {
            ChildClass kid = new ChildClass();

            Console.ReadKey();
        }
    }

Was This Post Helpful? 0
  • +
  • -

#6 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: stackoverflowexception when trying to load viewlodel

Posted 30 January 2016 - 03:29 AM

Ok so i put a break point on this line:

Load_New_Enquiry_Screen();


And found that when this line is called:

CurrentScreen_Enquiry = new ViewModels.Enquiry.VM_Enquiry_New();


It will go to this line in 'VM_Enquiry_New':

public VM_Enquiry_New()


And then back to this line in 'VM_Enquiry':

public VM_Enquiry()


And then it just loops and loops.

I am guessing this line in 'VM_Enquiry_New' is causing it:

class VM_Enquiry_New : VM_Enquiry


But if i change to this:

class VM_Enquiry_New


I get loads of these errors:

The name 'onpropertychanged' does not exist in the current context


And this error:

Cannot implicitly convert type 'TrackingApplication.ViewModels.Enquiry.VM_Enquiry_New' to 'TrackingApplication.ViewModels.Enquiry.VM_Enquiry'


On this line:

CurrentScreen_Enquiry = new ViewModels.Enquiry.VM_Enquiry_New();

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,973
  • Joined: 05-May 12

Re: stackoverflowexception when trying to load viewlodel

Posted 30 January 2016 - 04:02 PM

The question is why are you having your base class create an instance of your child class within the constructor of your base class?
Was This Post Helpful? 0
  • +
  • -

#8 Ratatoskr   User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 223
  • Joined: 02-January 16

Re: stackoverflowexception when trying to load viewlodel

Posted 30 January 2016 - 04:44 PM

Take this with 1/4th a grain of salt.... I have little idea what's going on here exactly but I feel like a hack(Not a fix) could be to create a bool along with in If Statement conditioning the LoadScreen to try and cease the endless looping you talked about.

Maybe a way to ensure it is only called once and prevent the cycle it is creating?

This post has been edited by Ratatoskr: 30 January 2016 - 04:44 PM

Was This Post Helpful? 0
  • +
  • -

#9 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: stackoverflowexception when trying to load viewlodel

Posted 30 January 2016 - 05:19 PM

View PostSkydiver, on 31 January 2016 - 12:02 AM, said:

The question is why are you having your base class create an instance of your child class within the constructor of your base class?


Because I want to have the new screen show without the user having to click a separate button to show it.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,973
  • Joined: 05-May 12

Re: stackoverflowexception when trying to load viewlodel

Posted 30 January 2016 - 05:59 PM

So why not have your new screen create an instance of the child class instead of the base class?

Again your lack of understanding of object oriented programming basics and C# while trying to tackle WPF at the same time is getting in your way. I will reiterate my recommendation that you set aside this project and start with the basics of C# and object oriented programming without getting distracted by WPF details.
Was This Post Helpful? 1
  • +
  • -

#11 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: stackoverflowexception when trying to load viewlodel

Posted 31 January 2016 - 02:08 AM

How would I do that?

And any recommendations on best way to start with the basics?
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,973
  • Joined: 05-May 12

Re: stackoverflowexception when trying to load viewlodel

Posted 31 January 2016 - 11:45 AM

See sticky post at the top of the C# forum. It's got various resources for learning C#.
Was This Post Helpful? 0
  • +
  • -

#13 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: stackoverflowexception when trying to load viewlodel

Posted 31 January 2016 - 12:29 PM

Thank you but how would I fix the issue I am having at the moment?
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,973
  • Joined: 05-May 12

Re: stackoverflowexception when trying to load viewlodel

Posted 31 January 2016 - 01:57 PM

Don't create an instance of the child class in the base class. Have the view create an instance of the child class.
Was This Post Helpful? 0
  • +
  • -

#15 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: stackoverflowexception when trying to load viewlodel

Posted 31 January 2016 - 02:24 PM

My views are pure XAML and I'm pretty sure I wouldn't be able to create an instance of the class I need from the XAML or can I?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »