Sabrina N. Stewart
Sabrina N. Stewart

Reputation: 11

Visual studio C# error CS0649 Validation error?

I have looked and looked for an answer but I just cannot find one. Yes this is for a class of mine, but my teacher doesn't know the answer either. He always asks if we have checked google first. He is working through Visual Studio with us. So, please help us figure out whats wrong. I constantly get the errors:

Field 'Program.birthday' is never assigned to, and will always have its Mod3_Self..... default value null.

And the same for Program.Validate.

Error codes: CS0649

using System;

namespace Mod3_Self_Assessment
{
    class Program
    {
        public static dynamic firstName;
        public static dynamic lastName;
        private static dynamic birthday;
        public static dynamic tfName;
        public static dynamic tlName;
        public static dynamic course;
        public static dynamic program;
        public static dynamic degree;
        private static dynamic validate;
        public static dynamic validatebday;

        static void Main(string[] args)
        {
            GetUserInformation();
            PrintStudentDetails(firstName, lastName, birthday);
            GetSchoolInformation();
            PrintSchoolInformation(tfName, tlName, course, program, degree);
        }

        static void GetUserInformation()
        {
            Console.WriteLine("Enter the student's first name: ");
            firstName = Console.ReadLine();
            Console.WriteLine("Enter the student's last name");
            lastName = Console.ReadLine();
            //Code to finish getting the rest of the student data
            Console.WriteLine("Enter your bithdate");
            DateTime birthday = validate(Console.ReadLine());
        }

        private static DateTime Validatebday(string date)
        {
            try
            {
                DateTime birthday = DateTime.Parse(date);

                if (birthday.AddYears(18).CompareTo(DateTime.Today) >0)
                {
                    Console.WriteLine("The student is younger than 18 years of age.");
                }
                return birthday;
            }
            catch (FormatException)
            {
                Console.WriteLine("Invalid date format.");
                return DateTime.Today;
            }
        }

        static void PrintStudentDetails(string firstName, string lastName, string birthday)
        {
            Console.WriteLine("{0} {1} was born on: {2}", firstName, lastName, birthday);
            Console.ReadLine();
        }

        static void GetSchoolInformation()
        {
            Console.WriteLine("Enter the teachers first name: ");
            tfName = Console.ReadLine();
            Console.WriteLine("Enter the teachers last name: ");
            tlName = Console.ReadLine();
            Console.WriteLine("Enter the course name: ");
            course = Console.ReadLine();
            Console.WriteLine("Enter the program name: ");
            program = Console.ReadLine();
            Console.WriteLine("Enter the degree name: ");
            degree = Console.ReadLine();
        }

        static void PrintSchoolInformation(string tfName, string tlName, string course, string program, DateTime degree)
        {
           Console.WriteLine(
             "Teacher: {1}, {0} " +
             "\n Course: {2} " +
             "\n Program: {3} " +
             "\n Degree: {4}"
             , tlName, tfName, course, program, degree);
        }
    }
}

Upvotes: 0

Views: 206

Answers (1)

Natassia Tavares
Natassia Tavares

Reputation: 81

There are two "birthday" variables in this code. The first one is declared as an public static dynamic attribute of the class. In method GetUserInformation you are declaring a local variable called "birthday" and you are assigning the "validate(Console.ReadLine());" to this local variable. You are never assigning value to the attribute of the class. In "GetUserInformation" remove the declaration of the second birthday variable. It should look like:

Console.WriteLine("Enter your bithdate");
birthday = validate(Console.ReadLine());

Pretty sure this will solve the issue.

Upvotes: 2

Related Questions