none
c# create two class instances with error RRS feed

  • Question

  • Hello,

    I want to create two class instances , but there is an error.

        class Animal
        {
            public static void testFun(string str)
            {
                Console.WriteLine(str);
                Console.ReadLine();
            }            
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Animal animalA = new Animal();
                Animal animalB = new Animal();
    
                AnimalA.testFun("aaaaaaa");
                AnimalB.testFun("bbbbbbb");
            }
        }

    Sunday, April 5, 2020 4:14 AM

Answers



  •     class Animal
        {
            public static void testFun(string str)
            {
                Console.WriteLine(str);
                Console.ReadLine();
            }            
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Animal animalA = new Animal();
                Animal animalB = new Animal();
    
                AnimalA.testFun("aaaaaaa");
                AnimalB.testFun("bbbbbbb");
            }
        }

    C# is case-sensitive.Try:

    animalA.testFun("aaaaaaa");
    animalB.testFun("bbbbbbb");

    If you want to use testFun from each instance, don't make it static.

    class Animal
    {
        //public static void testFun(string str)
        public void testFun(string str)
        {
            Console.WriteLine(str);
            Console.ReadLine();
        }            
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Animal animalA = new Animal();
            Animal animalB = new Animal();
    
            //AnimalA.testFun("aaaaaaa");
            //AnimalB.testFun("bbbbbbb");
            animalA.testFun("aaaaaaa");
            animalB.testFun("bbbbbbb");
        }
    }
    

    - Wayne

    Sunday, April 5, 2020 4:35 AM