Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
this is my example of Structure and i can't understand Error happened!

C#
struct Book
    {
        // Property
        public string Author { get; set; }
        public string Title { get; set; }
        private int copyright;
        public int Copyright
        {
            set
            {
                if (value < 2000)
                    Console.WriteLine("\n this copyright is not valid");
                else
                    this.copyright = value;
            }
            get { return this.copyright; }
        }
        // Constructors
        public Book(string a, string t, int c)
        {
            Author = a; // Error happened!
            Title = t;
            Copyright = c;
        }
    }

Error: Backing field for automatically implemented property '.Book.Author' must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer.
Posted
Updated 22-Oct-12 23:31pm
v2

 
Share this answer
 
Hi,

Somehow, automatic properties in struct do have some issue.

Instead of
public string Author { get; set; }

use following

C#
private int _Author ; 
public int Author 
{         
get { return _Author ; }         
set { _Author = value; }     
} 


See http://social.msdn.microsoft.com/Forums/en/vcsharpexpress2008prerelease/thread/31904232-e43b-455d-bd6a-c04c92c334ce[^] for more details.

Hope that helps, If it does, mark the answer as solution/upvote.

Thanks
Milind
 
Share this answer
 
The problem is your copyright field:
C#
public Book(string a, string t, int c)
    {
    copyright = c;
    Author = a;
    Title = t;
    Copyright = c;
    }
Will fix it!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900