Valerie Meunier

Valerie Meunier

  • 958
  • 693
  • 72.1k

How to reach non-static method from Main

Jan 21 2023 10:53 PM

Hi

following my previous question, i added a non-static method to the code, but now i can't access it from Main:

I get two errors at bold lines (myObj does not exist ...)

Thanks

using System;
class Stm
{
    string name;

    static void Main(string[] args)
    {
        Stm[] myObj = new Stm[3];

        for (int i = 0; i < myObj.Length; i++)
            myObj[i] = new Stm();

        myObj[0].naam = "Macron";
        myObj[1].naam = "Biden";
        myObj[2].naam = "Sunak";
        myObj.show();
    }

    void show()
    {
        for (int i = 0; i < myObj.Length; i++)
            Console.WriteLine(name);
    }
}


Answers (2)