Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR the property or indexer cannot be used in this context because the set accessor is inaccessible

Tags:

c#

I have the following code:

using System;
using System.Collections.Generic;
using System.Text;
using ProjectBase.Utils;

namespace EnterpriseSample.Core.Domain
{
    public class Notifikasi : DomainObject<string>, IHasAssignedId<string>
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public DateTime CreatedDate { get; set; }
        public string CreatedBy { get; set; }
        public DateTime UpdateDate { get; set; }
        public string UpdateBy { get; set; }

        public void SetAssignedIdTo(string assignedId)
        {
            Check.Require(!string.IsNullOrEmpty(assignedId), "assignedId may not be null or empty");

            // As an alternative to Check.Require, which throws an exception, the 
            // Validation Application Block could be used to validate the following
            Check.Require(assignedId.Trim().Length == 5, "assignedId must be exactly 5 characters");

            ID = assignedId.Trim().ToUpper();
        }

        public override int GetHashCode()
        {
            return (GetType().FullName + "|" +
                    ID + "|" +
                    Name).GetHashCode();

        }
    }
}

I use the Notifikasi Class in a different class, but oNoti.ID = "12"; generates an error:

The property or indexer cannot be used in this context because the set accessor is inaccessible.

Can anybody help me solve this? (I use an ORM if that's relevant).

INotifikasiDao dao = DaoFactory.GetNotifikasiDao();

dao.closeSession();
dao.openSession();
EnterpriseSample.Core.Domain.Notifikasi oNoti = new EnterpriseSample.Core.Domain.Notifikasi();
int no = 1;
oNoti.Name = "ETL Account History";
DateTime startDate = DateTime.Today;
DateTime expiryDate = startDate.AddDays(30);
oNoti.StartDate = startDate;
oNoti.EndDate = expiryDate;
oNoti.Description = "ACC_HIST" + startDate + "Failure";
oNoti.ID = "12"; //this is where the error happens

dao.Update(oNoti);
like image 477
user1999691 Avatar asked Jan 22 '13 09:01

user1999691


1 Answers

Your question is missing the important definition of DomainObject, but the error message indicates that the property ID is declared like this:

public string ID
{
    get;
    protected set;
}

This means that the value of ID can be read by anyone, but it can only be set from inside instances of DomainObject and derived classes.

Using SetAssignedIdTo isn't an option either, because it requires 5 characters, the ID you want to assign is only two characters long. I assume this method exists to be able to manually assign IDs to rows in a table that normally has an auto-incrementing key. Forcing 5 characters for this ID makes sure - to a certain degree - that the manually assigned keys won't conflict with the automatic created ones.

like image 137
Daniel Hilgarth Avatar answered Oct 23 '22 00:10

Daniel Hilgarth