C # classes - basic example

This is basically my first attempt at understanding classes in C #. I have gone through several tutorials on the internet, but what I am missing the most and which I have not yet found is a simple good example.

I have an idea of ​​what my main program should look like and I would be grateful for your help:

using System;

namespace Introduction_to_classes
{
    class Person
    {
        int Age;
        string Name;

        int DateOfBirth()
        {
            return 2013 - Age;
        }
    }

    class Program
    {
        public static void Main()
        {
            Person Mother = new Person(35, Alice);
            Person Son = new Person(12, Johny);

            Mother.Name = "Lucy";  // Just changing the value afterwards

            if(Mother.Age > Son.Age)
            {
                int year = Mother.DateOfBirth();
                Console.WriteLine("Mom was born in {0}.", year);
            }

            Console.ReadLine();
        }
    }
}

      

It's just an idea, it definitely doesn't work. But it would help me the most if you could fix it in a working example ...

+3


source to share


4 answers


class Person
{
    public int Age { get; set; }
    public string Name { get; set; }

    public Person(int age, string name)
    {
        Age = age;
        Name = name;
    }

    public int DateOfBirth()
    {
        return 2013 - Age;
    }
}

        class Program
        {
            public static void Main()
            {
                Person Mother = new Person(35, "Alice");
                Person Son = new Person(12, "Johny");

                Mother.Name = "Lucy";  // Just changing the value afterwards

                if (Mother.Age > Son.Age)
                {
                    int year = Mother.DateOfBirth();
                    Console.WriteLine("Mom was born in {0}.", year);
                }
            }
        }

      



Some useful links: properties , constructor

+6


source


using System;

namespace Introduction_to_classes {
    class Person {
        public int Age;
        public string Name;

        public int DateOfBirth() {
            return 2013-Age;
        }
    }

    class Program {
        public static void Main() {
            Person Mother=new Person {
                Age=35,
                Name="Alice"
            };

            Person Son=new Person {
                Age=12,
                Name="Johny"
            };

            Mother.Name="Lucy";  // Just changing the value afterwards

            if(Mother.Age>Son.Age) {
                int year=Mother.DateOfBirth();
                Console.WriteLine("Mom was born in {0}.", year);
            }

            Console.ReadLine();
        }
    }
}

      



+3


source


The problem is that you are referring to a constructor that does not exist:

Person Mother = new Person(35, Alice);

      

The first argument here is int

, the second should be string

, as I understand it. But the string literal must be quoted with double quotes, so the string must be:

Person Mother = new Person(35, "Alice");

      

Ditto for the next line.

Now, you probably want a constructor that takes the types of these arguments, and you want to store those values ​​for a new object, I assume. So add this to your class Person

:

public Person(int a, string n)
{
    this.Age = a;
    this.Name = n;
}

      

And finally, you need to make your field Age

and Name

accessible to other classes, noting their internal

or public

:

    public int Age;
    public string Name;

      

After that, you should be fine.

+3


source


First of all: new Person(35, "Alice")

implies what the class Person

constructor defines public Person(int age, string name)

. Alternatively, you'll have to call new Person() { Age = 35, Name = "Alice" }

that only works as long as you don't define a constructor, or define a constructor that takes 0 arguments, for example public Person()

(notice how I put "Alice" in quotes? Because you didn't specify a string with name Alice

, therefore Alice

- unknown object)

Next we have Mother.Name = "Lucy"

one that won't work because it Name

cannot be detected. class Person

defines Name

, but since you didn't specify an access modifier like public

or private

, class Program

it doesn't even know that it exists and therefore cannot access it. Thus, you should use public string Name

instead string Name

. It is also considered good style to always specify your access modifier. The same applies to public int Age

and public int DateOfBirth()

.

To learn more about access modifiers refer to http://msdn.microsoft.com/en-us/library/ms173121.aspx

+1


source







All Articles