C# The this object cannot be used before all of its fields are assigned to - Solved

strut object cannot be used before assigned Backing field for automatically implemented property 'error_struct.Employee.LastName' must be fully assigned before control is returned to the caller.
Consider calling the default constructor from a constructor initializer.
The 'this' object cannot be used before all of its fields are assigned to

Add as shown in this struct snippet : this() to fix the error.

Download

Example Source Snippets - Struct_Class.zip

Error : The 'this' object cannot be used before all of its fields are assigned to


                    struct Employee
                    {
                        public int EmpId { get; set; }
                        public string FirstName { get; set; }
                        public string LastName { get; set; }

                        public Employee(int empid, string fname, string lname)
                        {
                            EmpId = empid;
                            FirstName = fname;
                            LastName = lname;
                        }
                    }
            

Solved : The 'this' object cannot be used before all of its fields are assigned to


                struct Employee
                {
                    public int EmpId { get; set; }
                    public string FirstName { get; set; }
                    public string LastName { get; set; }

                    public Employee(int empid, string fname, string lname)
                       : this() 
                    {
                        EmpId = empid;
                        FirstName = fname;
                        LastName = lname;
                    }
                }
                 

smartsnipps.ecomparefiles.com © 2019, All Rights Reserved | Disclaimer: smartsnipps.eCompareFiles.com is free to use any code snippets without guarantee