Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 435
Vignesh Avatar asked Nov 29 '17 15:11

Vignesh


2 Answers

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);
like image 124
Vignesh Avatar answered Oct 14 '22 02:10

Vignesh


AvailabilityOfLockersResp avail = new AvailabilityOfLockersResp();
avail.LockerIds.AddRange(new List<int>{1,2,3});
like image 32
afrose Avatar answered Oct 14 '22 01:10

afrose