Now I still learning C#, and in one of the tutorials I found this program:

using System;

namespace Returning_a_Class_From_a_Method
{
    public class MVADate
    {
        private int dayOfBirth;
        private int monthOfBirth;
        private int yearOfBirth;

        public void SetDate(int d, int m, int y)
        {
            dayOfBirth = d;
            monthOfBirth = m;
            yearOfBirth = y;
        }

        public string ProduceDate()
        {
            string result = dayOfBirth + "/" + monthOfBirth + "/" +
                yearOfBirth;

            return result;
        }
    }

    public class MotorVehicleAdministration
    {
        public MotorVehicleAdministration()
        {
            birthdate = new MVADate();
        }

        private string fullName;
        private MVADate RequestDateOfBirth();
        private bool isAnOrganDonor;

1!     private MVADate RequestDateOfBirth()
        {
            MVADate date = new MVADate();

            Console.Write("Day of Birth:   ");
            int d = int.Parse(Console.ReadLine());

            Console.Write("Month of Birth: ");
            int m = int.Parse(Console.ReadLine());

            Console.Write("Year of Birth: ");
            int y = int.Parse(Console.ReadLine());

            date.SetDate(d, m, y);
            return date;
        }

        static void Main(string[] args)
        {
            MotorVehicleAdministration MVA =
                new MotorVehicleAdministration();

            Console.WriteLine("To process a registration, enter the information");
            Console.Write("Full Name:      ");
            MVA.fullName = Console.ReadLine();

            MVA.birthdate = RequestDateOfBirth();

            Console.Write("Is the application an organ donor (0=No/1=Yes)? ");
            string ans = Console.ReadLine();

            if (ans == "0")
                MVA.isAnOrganDonor = false;
            else
                MVA.isAnOrganDonor = true;

            Console.WriteLine("\n -=- Motor Vehicle Administration -=-");
            Console.WriteLine(" -=- Driver's License Application -=-");

            Console.WriteLine("Full Name:    {0}", MVA.fullName);
            Console.WriteLine("Dateof Birth: {0}", MVA.birthdate.ProduceDate());
            Console.WriteLine("Organ Donor?  {0}", MVA.isAnOrganDonor);

            Console.WriteLine();

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

When I tried compile this program I received in line (marked by me as 1!) error :
Type Returning_a_Class_From_a_Method.MotorVehicleAdministration
already defines a member called 'RequestDateOfBirth' with the same parameter types which points to RequestDateOfBirth()

I think that error caused because of declaration

private MVADate RequestDateOfBirth();

but I still can't find any solution to this. It's tutorial, but this program won't run. What I must do to correct error?

Recommended Answers

All 5 Replies

Yes you can remove the line you highlighted, and for the other 2 functions too.

Thanks for replay. All three private variables just a front highlighted line should be public - in this case program works fine.

Anyway thanks for replay.

I think there's a couple of ways you could solve the problem you are having. One of the options you could use would be to make MotorVehicleAdministration a separate object.

using System;

namespace Returning_a_Class_From_a_Method
{
    public class MVADate
    {
        private int dayOfBirth;
        private int monthOfBirth;
        private int yearOfBirth;

        public void SetDate(int d, int m, int y)
        {
            dayOfBirth = d;
            monthOfBirth = m;
            yearOfBirth = y;
        }

        public string ProduceDate()
        {
            string result = dayOfBirth + "/" + monthOfBirth + "/" +
                yearOfBirth;

            return result;
        }
    }

    public class MotorVehicleAdministration
    {
        public MotorVehicleAdministration()
        {
            birthdate = new MVADate();
        }

        private string fullName;
        private MVADate birthdate;
        private bool isAnOrganDonor;

        public void RequestDateOfBirth()
        {
            Console.Write("Day of Birth:   ");
            int d = int.Parse(Console.ReadLine());

            Console.Write("Month of Birth: ");
            int m = int.Parse(Console.ReadLine());

            Console.Write("Year of Birth: ");
            int y = int.Parse(Console.ReadLine());

            birthdate.SetDate(d, m, y);
        }

        public MVADate Birthdate
        {
            get
            { return birthdate; }
        }

        public bool IsOrganDonor
        {
            get
            { return isAnOrganDonor; }
            set
            { isAnOrganDonor = value; }
        }

        public string FullName
        {
            get
            { return fullName; }
            set
            { fullName = value; }
        }
    }

    public class Test
    {
        static void Main(string[] args)
        {
            MotorVehicleAdministration MVA =
                new MotorVehicleAdministration();

            Console.WriteLine("To process a registration, enter the information");
            Console.Write("Full Name:      ");
            MVA.FullName = Console.ReadLine();

            MVA.RequestDateOfBirth();

            Console.Write("Is the application an organ donor (0=No/1=Yes)? ");
            string ans = Console.ReadLine();

            if (ans == "0")
                MVA.IsOrganDonor = false;
            else
                MVA.IsOrganDonor = true;

            Console.WriteLine("\n -=- Motor Vehicle Administration -=-");
            Console.WriteLine(" -=- Driver's License Application -=-");

            Console.WriteLine("Full Name:    {0}", MVA.FullName);
            Console.WriteLine("Dateof Birth: {0}", MVA.Birthdate.ProduceDate());
            Console.WriteLine("Organ Donor?  {0}", MVA.IsOrganDonor);

            Console.WriteLine();

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

The issue you were experiencing came from the fact that you had the RequestDateOfBirth declared twice. It didn't like that you had

private MVADate RequestDateOfBirth();

declared before

private MVADate RequestDateOfBirth()
{
...
}

I'm pretty sure you would have to do something like this in say...C++...but not C#. Even after fixing that issue, there were a lot of bugs in the code that dealt with accessibility issues with the variables all being declared private and then trying to access them from a static method.

I didn't wrote this program. I find it on web pages tutorials from FunctionX. I am absolute beginer in Visual C# and so far I didn't found any book which is for absolute beginner (read: with no any contact with this programming).

Of course I found some books from which I was able to learn, but at some point this learning always stopped. On this web pages are true lessons for absolute beginners. But of course - from time to time - everyone can find some bugs...

But thanks for this answer...

then try the video training material at http://www.3dbuzz.com which is designed with people just like you in mind, and theres 3 sections all absolutely free for c#

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.