-2
namespace LEARNING
{
    public class Human
    {
        public string nameOne;
        public string nameTwo;
        public int ageOne;
        public bool hasagetwo;
        public int ageTwo;
        public bool isalive;

        public void Info()
        {
            Console.WriteLine("Name =" + nameOne + "," + nameTwo);
        }
    }
    class Program
        {
            static void Main(string[] args)
            {

            /* Class Stuff */

            Human humanOne = new Human();

            Human.nameOne = "Adrian";
            Human.hasagetwo = true;
            Console.WriteLine(humanOne.nameOne);

            Human.Info();

            /* Basic Things Down There */
            const string stringOne = "String";
                const string notes = "The Fact that String One has the Value of 'String One' is :";

                Console.WriteLine(notes);

                Console.WriteLine(stringOne == "StringOne");

                int a = 10;
                int b = 3;
                int c = 11;

                Console.WriteLine(++a);
                Console.WriteLine(a - b);
                Console.WriteLine(a == c);
                Console.WriteLine(b != a);

                Console.WriteLine(a = c);

                Console.WriteLine(a != c && a == 10);
                Console.WriteLine(b != 6 || a == c);

                Console.WriteLine(!(a == b));

                /* Classes Down There */
                Human Adrian = new Human();
            }

        }

}

CS0120 C# An object reference is required for the non-static field, method, or property

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
  • 4
    `Human.nameOne` => `humanOne.nameOne` (and so on for all the other uses of `Human.`) – UnholySheep Jan 06 '20 at 17:12
  • 1
    Does this answer your question? [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop) – jcs Jan 06 '20 at 17:14

2 Answers2

3

You're trying to set object property values, but you are doing it through the class by mistake

        Human humanOne = new Human();

        Human.nameOne = "Adrian";
        Human.hasagetwo = true;
        // ....
        Human.Info();

should be

        Human humanOne = new Human();

        humanOne.nameOne = "Adrian";
        humanOne.hasagetwo = true;
        // ...
        humanOne.Info();

Think about it this way, what if you made 2 human objects? If Human. was the way to access, how would you know which of the two you were setting? The exception to this is static fields and methods, but to access the actual instances you refer to those instances themselves.

0

Change

    Human.nameOne = "Adrian";
    Human.hasagetwo = true;
    Console.WriteLine(humanOne.nameOne);

    Human.Info();

to

    humanOne.nameOne = "Adrian";
    humanOne.hasagetwo = true;
    Console.WriteLine(humanOne.nameOne);

    humanOne.Info();

You are trying to assign instance variables to a type (Human), you have to assign them to the instance of the type (humanOne) instead.