Call function from Main()
Asked Answered
B

3

5

I am new to C# and I'm having a little problem with calling a function from the Main() method.

class Program
{
    static void Main(string[] args)
    {
        test();
    }

    public void test()
    {
        MethodInfo mi = this.GetType().GetMethod("test2");
        mi.Invoke(this, null);
    }

    public void test2()
    { 
        Console.WriteLine("Test2");
    }
}

I get a compiler error in test();:

An object reference is required for the non-static field.

I don't quite understand these modifiers yet so what am I doing wrong?

What I really want to do is have the test() code inside Main() but it gives me an error when I do that.

Bernardobernarr answered 10/4, 2014 at 19:38 Comment(11)
As the error message tells you, you need an object reference to call a non-static member. So create an object reference.Trottier
From a method? How is that possible to make an object from a method? or do you mean I have to put the test() in a new class?Bernardobernarr
You need an instance of the type the method is defined in, to call an instance method of that type. The same is just as true in obj-c, I'm sure.Trottier
Why downvote? I didnt do anything wrong!Bernardobernarr
You didn't even do basic research before asking the question. Doing something as simple as performing a web search on the error message leads to plenty of information on exactly what you're doing wrong.Trottier
I tried this.test() but still error, thats the objc syntaxBernardobernarr
In static context you don't have this or any other 'attributes' belonging to an instance context.Kermitkermy
@Trottier I thought the error was too wide so I didnt really know what to search for.Bernardobernarr
@Bernardobernarr So you didn't even bother to look for an answer because you assumed that you wouldn't find one? Don't do that. Look for an answer. If, after spending a fair bit of time both trying to solve the problem and looking for a solution, then consider posting a question on Stack Overflow. If you are unwilling to do something as simple as perform a web search on your issue, you should expect downvotes. That someone would upvote a question for which the author is unwilling to even perform a single web search is very disappointing for me.Trottier
Why you just don't call Console.WriteLine("Test2"); from the Main method? All of this just to show a simple message. If you want to learn about reflection, try not to use a static context to do this.Fiendish
@GuilhermeOliveira Please dont ask why, this is just for testing, not what I want in the future.Bernardobernarr
B
7

Just put all logic to another class

 class Class1
    {
        public void test()
        {
            MethodInfo mi = this.GetType().GetMethod("test2");
            mi.Invoke(this, null);
        }
        public void test2()
        {
            Console.Out.WriteLine("Test2");
        }
    }

and

  static void Main(string[] args)
        {
            var class1 = new Class1();
            class1.test();
        }
Biosynthesis answered 10/4, 2014 at 19:43 Comment(0)
K
8

If you still want to have test() as an instance method:

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        p.test();
    }

    void Test()
    {
        // I'm NOT static
        // I belong to an instance of the 'Program' class
        // You must have an instance to call me
    }
}

or rather make it static:

class Program
{
    static void Main(string[] args)
    {
        Test();
    }

    static void Test()
    {
        // I'm static
        // You can call me from another static method
    }
}

To get the info of a static method:

typeof(Program).GetMethod("Test", BindingFlags.Static);
Kermitkermy answered 10/4, 2014 at 19:44 Comment(0)
B
7

Just put all logic to another class

 class Class1
    {
        public void test()
        {
            MethodInfo mi = this.GetType().GetMethod("test2");
            mi.Invoke(this, null);
        }
        public void test2()
        {
            Console.Out.WriteLine("Test2");
        }
    }

and

  static void Main(string[] args)
        {
            var class1 = new Class1();
            class1.test();
        }
Biosynthesis answered 10/4, 2014 at 19:43 Comment(0)
I
1

The method must be static in order to call it.

Indo answered 10/4, 2014 at 19:39 Comment(3)
Thats the problem I cant use the MethodInfo in a static method.Bernardobernarr
The method is not static.Raouf
@Bernardobernarr The type of a static method is always known at compile time; there is no need to get it dynamically.Trottier

© 2022 - 2024 — McMap. All rights reserved.