7 Replies - 477 Views - Last Post: 14 October 2017 - 11:53 PM Rate Topic: -----

#1 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

How to call method from two classes without infinite loop?

Posted 09 July 2017 - 05:41 PM

Now, I am going to guess that I am going about things the wrong way again. But I am trying to run methods between classes.

Explanation;

Below is two classes, MyClassOne and MyClassTwo, both have there own methods which I want to be able to run between them.
To be able to run the method MyMethodTwo from MyClassOne, all I need to do is inistialize the MyClassTwo class in MyClassOne class and I can run the method from MyClassTwo, But now I have initialized MyClassTwo, I can't then initialize MyClassOne in MyClassTwo or I will get stuck in an endless loop.

This is an example of what I mean.

public class MyClassOne
    {
        public MyClassTwo MyClassTwo { get; set; }

        public MyClassOne()
        {
            MyClassTwo = new MyClassTwo();

            MyClassTwo.MyMethodTwo();
        }


        public void MyMethodOne()
        {
            //do stuff here
        }
    }

    public class MyClassTwo
    {
        public MyClassTwo()
        {
        }

        public void MyMethodTwo()
        {
            //do stuff here
        }

        public void RunMyMethodOne()
        {
            //run MyMethodOne() here???
        }
    }



So how do I go about running the method MyMethodOne() from MyClassTwo?

A caveat, I cannot use static methods. Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: How to call method from two classes without infinite loop?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5884
  • View blog
  • Posts: 20,091
  • Joined: 05-May 12

Re: How to call method from two classes without infinite loop?

Posted 09 July 2017 - 06:09 PM

Sounds like your two classes are tightly coupled. You should fix that at some point. Remember: tight cohesion, loose coupling.

In the meantime, just pass in MyClassTwo as parameter of the constructor of MyClassOne.

Another thing to consider is why must the constructor of MyClassOne immediately call MyMethodTwo right after construction? Good class design usually dictates that constructors b are lightweight and don't do any work beyond initialization.
Was This Post Helpful? 2
  • +
  • -

#3 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to call method from two classes without infinite loop?

Posted 09 July 2017 - 10:56 PM

Hey SkyDiver. It is far more complicated than what the example shows. That was just to illustrate what I was taking about. In the end I made an Interface to access the method from MyClassOne. Thanks as usual for the help.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5884
  • View blog
  • Posts: 20,091
  • Joined: 05-May 12

Re: How to call method from two classes without infinite loop?

Posted 10 July 2017 - 05:36 AM

Yes, but how did you get to an instance of MyClassOne that has the correct instance of MyClassTwo? Or does it not matter that MyClassOne is playing with a different instance of MyClassTwo?
Was This Post Helpful? 0
  • +
  • -

#5 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to call method from two classes without infinite loop?

Posted 10 July 2017 - 06:24 AM

Sorry, I am not following what you mean? Are talking about the Interface accessing MyClassOne?

Anyway, this is what I did,

The InterFace
public interface IReportProgress
    {
        void ReportProgress(int percentage);        
    }

    
    public class BackgroundReporter : IReportProgress
    {
        private readonly IReportProgress _viewModelMain;

        public BackgroundReporter(IReportProgress main)
        {
            _viewModelMain = main;
        }

        public bool IsActive { get; set; }

        public void ReportProgress(int percentage)
        {
            if (IsActive)
            {
                _viewModelMain.ReportProgress(percentage);
            }
        }         
    }


Then in MyClassOne , real name is ShiftListViewModel

public class ShiftListViewModel : CollectionViewModel<ShiftTimeWrapper>, IEmployeeProvider, IReportProgress
    {        
        public ShiftLogic ShiftLg { get; set; }
        
        public ShiftListViewModel()  
        {            
            ShiftLg = new ShiftLogic(this);  
            onselectedSectionchanged(SelectedSection)          
        }

        public void onselectedSectionchanged(Section section)
        {            
            RunInBackground(() =>
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");
                var reporter = new BackgroundReporter(this)
                {
                    IsActive = true
                };                
                ShiftLg.CountShiftData(SelectedDate, section.SubSection, reporter);                
            });           
        }
 
        public void ReportProgress(int percentage)
        {
            _worker?.ReportProgress(percentage);
        }
    }


public void RunInBackground(Action action)
        {
            //Setup background worker here
            // Assign necessary events for Progressbar
        }




Then In MyClassTwo, real name is ShiftLogic

public class ShiftLogic
    {       

        public ShiftLogic(IEmployeeProvider iEmployeeProvider)
        {
            Load()
        }
        
        public void CountShiftData(DateTime date, IReportProgress reportProgress = null)
        {
            foreach (var item in MyCollection)
            {
                // Do logic here
                //Work out how many times the loop has run to inform progress bar
                PercentageLeft = (lineCount++ / CountLines) * 100;

                // Use interface pass data into the other class
                reportProgress?.ReportProgress((int)PercentageLeft);
            }
        }
    }


I tried to cut out as much unnecessary code to explain what I am doing. But, what I was trying to do was inform a ProgressBar in the first class, my VeiwModel how long a process had run for from a method inside a worker class. The problem was, as I said earlier, the VeiwModel had already created an instance of the worker class
Was This Post Helpful? 0
  • +
  • -

#6 BenScharbach2017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-October 17

Re: How to call method from two classes without infinite loop?

Posted 14 October 2017 - 03:34 PM

I agree with the other poster, simply pass in the instance of the first into the ctr of the 2nd.

Ben Scharbach
[mod: link removed]
Was This Post Helpful? 0
  • +
  • -

#7 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to call method from two classes without infinite loop?

Posted 14 October 2017 - 06:56 PM

Nice plug there for yourself then.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,755
  • Joined: 12-December 12

Re: How to call method from two classes without infinite loop?

Posted 14 October 2017 - 11:53 PM

BenScharbach2017 If you want to promote your link then edit your profile and add it to your signature, don't spuriously add it to a post, giving the false impression that it is somehow relevant to the question.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1