Friday, May 27, 2016

FormatException was unhandled , Input string was not in a correct format

This sample shows a Person class that has two properties: Name (string) and Age (int). Both properties are read/write.

Without Errors Code Msdn Properties Tutorial


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Person
    {
        private string myName;
        private int myAge;
        //Declare Name Property of a String
        public String Name
        {
            get
            {
                return myName;
            }
            set
            {
                myName = value;
            }

        }
        //Declare Age Property of a integer
        public int Age
        {
            get
            {
                return myAge;
            }
            set
            {
                myAge = value;

            }
        }
        public override string ToString()
        {
            return "Name = " + Name + ", Age = " + Age; ;
            }


        static void Main(string[] args)
        {
            Person person = new Person();

            Console.WriteLine("Person details -{0}", person);

            //Set some values on person objects
            person.Age = 25;
            person.Name = "Awareness";
            Console.WriteLine("Person details -{0)", person);

            person.Age += 20;
            Console.WriteLine("Person Details -{0}", person);
              Console.ReadKey();
        }
    }

}

Note : Red Color Mark Indicates Cause Of Error

Conclusion: Change the bracket to Flower Bracket in this Program.

output:
Person details -Name = , Age = 0
Person details -Name = Awareness, Age = 25
Person Details -Name = Awareness, Age = 45

After Execution Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Person
    {
        private string myName;
        private int myAge;
        //Declare Name Property of a String
        public String Name
        {
            get
            {
                return myName;
            }
            set
            {
                myName = value;
            }

        }
        //Declare Age Property of a integer
        public int Age
        {
            get
            {
                return myAge;
            }
            set
            {
                myAge = value;

            }
        }
        public override string ToString()
        {
            return "Name = " + Name + ", Age = " + Age; ;
            }


        static void Main(string[] args)
        {
            Person person = new Person();

            Console.WriteLine("Person details -{0}", person);

            //Set some values on person objects
            person.Age = 25;
            person.Name = "Awareness";
            Console.WriteLine("Person details -{0}", person);

            person.Age += 20;
            Console.WriteLine("Person Details -{0}", person);
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment