简体   繁体   English

如何用Solidity操作Solidity结构?

[英]How to manipulate a Solidity struct with Solidity?

I'm writing a smart contract where I'm creating an object (struct) in one function, and then I want to alter the properties in a different function. 我正在编写一个智能合约,其中我要在一个函数中创建一个对象(结构),然后要更改另一个函数中的属性。 One of the struct's properties is added in a mapping and that's how I track them, but even though I have created a function to get the properties of the struct from the mapping, I still cant quite figure out how to change them. 结构的一个属性被添加到映射中,这就是我跟踪它们的方式,但是即使我创建了一个函数来从映射中获取结构的属性,我仍然仍然无法弄清楚如何更改它们。

This is the struct 这是结构

struct Component {

    uint compID;
    string compManufacturer;
    string compDetails;
    address owner;
    address[] pastOwners;
    bool transfer;
}

This is an example of a function where I try to change the values in the struct. 这是一个函数的示例,我尝试更改该结构中的值。

function transfer (address factory, uint id) public {

    address compad;
    address[] compowners;
    bool boolean;

    (,,, compad, compowners, boolean) = getComponent(id);

    require(component.transfer == true);

    component.owner = factory;
    pastOwners = pastOwners.push(factory);
}

The function getComponent returns everything that is in the struct and here I initialize some local variables to store the ones that I need. 函数getComponent返回结构中的所有内容,在这里我初始化一些局部变量来存储所需的局部变量。 However, I'm not sure what I do afterwards works, because this way the local variables seem useless, and if I change the local variables the changes won't go through in the actual object. 但是,我不确定以后要做什么,因为这样局部变量似乎无用,而且如果我更改局部变量,更改将不会在实际对象中进行。 Please correct me if I'm wrong. 如果我错了,请纠正我。 Any help appreciated. 任何帮助表示赞赏。

Assuming you have a mapping such as: 假设您有一个映射,例如:

mapping(uint => Component) public components;

You can manipulate like this: 您可以这样操作:

components[id].compDetails = "comp details";

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

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