Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function referring to an instance created by other function C#

Tags:

c#

instance

I have a question that may be silly, but I'm new to C#, so pardon my insolence. I am wondering whether it is possible for a function to refer to an instance, which has been created by another function.

I am including an exemplary code to illustrate what I mean:

class Program
{
    static void Main(string[] args)
    {
        Instantiator.Instantiate();
        Referent.Refer(instance);
        Console.ReadLine();
    }
}

public class Instance
{
    public void OnInstantiated()
    {
        Console.WriteLine("I have been instantiated.");
    }
    public void OnReferred()
    {
        Console.WriteLine("I have been referred to.");
    }
}

public class Instantiator
{
    public static void Instantiate()
    {
        Instance instance = new Instance();
        instance.OnInstantiated();
    }
}

public class Referent
{
    public static void Refer(Instance instance)
    {
        if(instance != null)
        {
            instance.OnReferred();
        }
        else
        {
            Console.WriteLine("No instance to refer to.");
        }
    }
}

What could I use to be able to refer to the "instance" instance (which is created by the Instantiator.Instantiate function) in the Referent.Refer function?

Thanks in advance for your pertinent comments!

like image 685
guy120334913 Avatar asked Jan 06 '23 02:01

guy120334913


2 Answers

Make Instantiator return the class when done

public class Instantiator
{
    public static Instance Instantiate()
    {
        Instance instance = new Instance();
        instance.OnInstantiated();
        return instance;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var instance = Instantiator.Instantiate();
        Referent.Refer(instance);
        Console.ReadLine();
    }
}

The pattern Instantiate() is doing is often called the "Factory Pattern"

like image 189
Scott Chamberlain Avatar answered Jan 08 '23 16:01

Scott Chamberlain


Another option you could use is the Singleton pattern. If you also need your instance to be only one, you can give the responsibility to create a new instance and return it afterwards to the class itself.

class Program
{
    static void Main(string[] args)
    {
        Instance.Instantiate();
        Referent.Refer(Instance.GetInstance());
        Console.ReadLine();
    }
}

public class Instance
{
    private static Instance myInstance;
    public void OnInstantiated()
    {
        Console.WriteLine("I have been instantiated.");
    }
    public void OnReferred()
    {
        Console.WriteLine("I have been referred to.");
    }
    public static void Instantiate()
    {
        myInstance = new Instance();
        myInstance.OnInstantiated();
    }
    public static Instance GetInstance()
    {
        return myInstance;
    }
}

public class Referent
{
    public static void Refer(Instance instance)
    {
        if (instance != null)
        {
            instance.OnReferred();
        }
        else
        {
            Console.WriteLine("No instance to refer to.");
        }
    }
}
like image 27
Luc Avatar answered Jan 08 '23 16:01

Luc