Sandokan
Sandokan

Reputation: 1105

How to pass class instance to a static method?

I actually managed this creating this code:

class Foo{
    public void test(){
         Keyboard.input(this);
    }

    public void menu(){
          Console.WriteLine("This is a menu");
    }
}

class Keyboard{
     public static string input(object class_name){
           new class_name().menu(); //call from dynamic object the method
     }
}

How you can see I have a class called Foo this contains the method test that read some input from the user keyboard, essentially this method allow me to recognize if the user press a particular key the code call the method menu. In this example it's just called the method menu. Now I pass the instance of the class Foo to the Keyboard class that have a static method called input, this method should create a new instance of the passed class and call the menu method. But I actually got an error on this line:

new class_name()

object is an object but is used as a type

what I did wrong?

Upvotes: 0

Views: 4845

Answers (2)

Mikhail Tulubaev
Mikhail Tulubaev

Reputation: 4261

You need to use generic method with new constraint and pick menu method into base abstract class:

public abstract class MenuClass
{
    public abstract void menu();
}

class Keyboard
{
    //if you can pass class name
    public static string input<TClassName>() where TClassName : MenuClass, new()
    {
        new TClassName().menu();
    }

    //if you can not pass class name
    public static string input<TClassName>(TClassName obj) where TClassName : MenuClass, new()
    {
        new TClassName().menu(); //if you want to create new instance
        obj.menu(); //if you want to use already existing object
    }
}

You can use first method if you can pass required class name, and if you cannot - use the second method without generic parameter:

Keyboard.input(yourObj);

Compiler will automatically get type parameter from usage


UPDATE

As you have not abstract classes or interfaces you should cast your object to required class before calling method:

 public static string input(object class_name){
        if (class_name as Foo != null)
        {
            ((Foo)class_name).menu();
        }
 }

or if you do not know the input type of an object and can not use generics, but you should to create new instance of the same class, use reflection:

 public static string input(object class_name){
        if (class_name != null)
        {
            Foo newObj = Activator.CreateInstance(class_name.GetType());
            newObj.menu();
        }
 }

Upvotes: 2

J3soon
J3soon

Reputation: 3143

Since class_name is an instance of class not a type, you should cast it to Foo type and call the function.

Change

new class_name().menu();

to

((Foo)class_name).menu();

Update

If you wish it to use on multiple classes, you should create an interface for them to inherit.

// All classes passed into 'Keyboard.Input' should inherit this interface
interface IMenu
{
    public void menu();
}

class Foo : IMenu
{
    public void test()
    {
        Keyboard.input(this);
    }
    public void menu()
    {
        Console.WriteLine("This is a menu");
    }
}

and change the lines from

public static string input(object class_name)
{
    new class_name().menu(); //call from dynamic object the method
}

to

public static string input(IMenu class_name)
{
    ((IMenu)class_name).menu();
    return "Something you want to return";
}

Upvotes: 2

Related Questions