Search code examples
c#.netprotocol-buffers

Property or indexer of type RepeatedField cannot be assigned to — it is read only


I am new to Google ProtoBuf file. I have below message in a ProtoBuf file.

 message AvailabilityOfLockersResp{
  uint32 NumberOfAvailableLockers;
  repeated uint32 lockerIds = 1;
 }

I have generated the corresponding ProtoBuf C# class using protoc.exe and added that generated C# class file inside my .NET project file.

When I assign a value to the generated LockerIds field I get the error "Property or indexer 'AvailabilityOfLockersResp.LockerIds' cannot be assigned to -- it is read only":

screenshot of the error as shown in Visual Studio

It is showing me that it's a read-only field. But, I want to assign a value to this field. How can I add things to this field?


Solution

  • I found an answer to my own question I asked above.

            List<uint> lockerIds = new List<uint>();
    
            ProtoPacket protoPacketResponse = new ProtoPacket 
            {               
                AvailabilityOfLockersResp = new AvailabilityOfLockersResp { NumberOfAvailableLockers = (uint)lockerIds.Count() }//LockerIds = lockerIds,
            };
    

    Outside of new instance, I have assigned the value to the LockerIds like below,

    protoPacketResponse.AvailabilityOfLockersResp.LockerIds.AddRange(lockerIds);