Share via

Refernce method from an instantiated class?

lkubler 41 Reputation points
2021-04-26T22:41:01.36+00:00

Hi,

In my MainForm class I have a method, Refresh(), that simply makes a call to the backend database and refreshes the data on the form.

In my MainForm I instantiate another class object and on this object are some form elements I display on the main form in a container. One of these is a Save button that writes any changes back to the database and I would also like it to also call the Refresh() method of the MainForm class to refresh all of the data.

My problem is I cannot figure out how to make this call, how do I reference the method from the MainForm class from an instantiated class?

Thanks in advance!

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2021-04-27T08:16:31.533+00:00

Consider the events too. In your another class you can define an event, then raise it after saving the changes to database:

class AnotherClass
{
   // . . .

   public event EventHandler Saved;

   void SaveToDatabase( )
   {
      // . . .

      Saved?.Invoke( this, null );
   }
}

I your main form you can handle this event:

AnotherClass c = . . .
c.Saved += ( s, a ) => Refresh( );

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,611 Reputation points
    2021-04-27T02:42:55.557+00:00

    Under normal circumstances, we have two ways to call a method in another class.

    One is to instantiate the class:

        class Program  
        {  
            static void Main(string[] args)  
            {  
                new MyClass().DoSomething();  
            }  
        }  
        class MyClass  
        {  
            public void DoSomething()   
            {  
                Console.WriteLine("Do something...");  
            }  
        }  
    

    The second is to set the method as a static method, and then access it through the class name:

        class Program  
        {  
            static void Main(string[] args)  
            {  
                MyClass.DoSomething();  
            }  
        }  
        class MyClass  
        {  
            public static void DoSomething()   
            {  
                Console.WriteLine("Do something...");  
            }  
        }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.