简体   繁体   English

如何使用 function 重新分配结构值

[英]How to reassign a struct value with a function in solidity

I am trying to change the value of a boolean in the struct to toggle the accessibility of a function.我正在尝试更改结构中 boolean 的值以切换 function 的可访问性。 But after the function runs it seems not to be reassigning the boolean value.I tried replacing the boolean with an enum since its only two inputs but it still doesn't work.但是在 function 运行之后,它似乎没有重新分配 boolean 值。我尝试用枚举替换 boolean,因为它只有两个输入,但它仍然不起作用。 How do i do this.我该怎么做呢。

        struct Hotel{
            uint256 roomNo;
            string suiteLevel;
            bool isOccupied;    
        }

        enum Status{
            Vacant,
            Occupied
        }
        Hotel[] public hotelroomsarray; 

        function newhotel(uint256 _num, string calldata _str, bool _isoccupied) public {
            Hotel memory room = Hotel(_num, _str, _isoccupied);
            hotelroomsarray.push(room);
        }-------i pass in false into this function

        function bookroom1() public payable uptofee{
            Hotel memory booked = hotelroomsarray[0];                   
            require(booked.isOccupied == false, "This room is alraeady occupied");              
            payable(owner).transfer(msg.value); 
            booked.isOccupied = true;------------part that isn't working                            
            bookertoroom[msg.sender] = hotelroomsarray[0];
            bookers.push(msg.sender);       
            addrtoamntpaid[msg.sender] = msg.value; 
            count++;                
        }

you are just updating the reference.您只是在更新参考。 you have to update in the array.你必须在数组中更新。

soln:解决方案:

         function bookroom1() public payable uptofee{                  
            require(booked.isOccupied == false, "This room is alraeady occupied");              
            payable(owner).transfer(msg.value); 
            hotelroomsarray[0].isOccupied = true;                          
            bookertoroom[msg.sender] = hotelroomsarray[0];
            bookers.push(msg.sender);       
            addrtoamntpaid[msg.sender] = msg.value; 
            count++;                
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM