Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# classes - basic example [closed]

Tags:

c#

class

This is basically my first attempt to understand the classes in C#. I've went through several tutorials on the internet, but the thing I'm missing the most and what I haven't found anywhere yet, is a simple good example.

I have some idea how my basic program should look like and I would appreciate 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 more than anything else it would help me if you can correct it to the working example...

like image 562
Jeyekomon Avatar asked Jan 31 '13 13:01

Jeyekomon


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

like image 185
laszlokiss88 Avatar answered Nov 17 '22 06:11

laszlokiss88


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();
        }
    }
}
like image 27
Ken Kin Avatar answered Nov 17 '22 07:11

Ken Kin


The problem is that you're referring to a constructor that doesn't exist:

Person Mother = new Person(35, Alice);

The first argument here is an int, the second should be a string as far as I understand. But a string literal should be marked with double quotes, so that line should be:

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

Same for the following line.

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

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

And, finally, you should make your Age and Name fields accessible to the other class, by marking them internal or public:

    public int Age;
    public string Name;

After that, you should be good to go.

like image 3
Theodoros Chatzigiannakis Avatar answered Nov 17 '22 07:11

Theodoros Chatzigiannakis


First of all: new Person(35, "Alice") implies that class Person defines a constructor public Person(int age, string name). Alternatively, you'll have to call new Person() { Age = 35, Name = "Alice" } which only works as long as you have not defined a constructor, or have defined a constructor that takes 0 arguments, such as public Person() (notice how I put "Alice" within quotation marks? That's because you didn't define a string called Alice, so Alice is an unknown object)

Next we have Mother.Name = "Lucy", which won't work, because Name is not discoverable. The class Person does define a Name, but since you didn't specify an access modifier, such as public or private, class Program doesn't even know it exists and thus cannot access it. So you have to use public string Name instead of string Name. It is also considered to be good style to always specify your access modifier. The same applies to public int Age and public int DateOfBirth() as well.

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

like image 1
Nolonar Avatar answered Nov 17 '22 07:11

Nolonar