0

If i have a struct that enables a user to initialize some of the members of the struct but at a later time update other members (i.e below kittyPriceTomorrow) and this struct is inside an array. How would we access that struct inside the array to update the values with? To further this lets say tomorrow has arrived thus time has expired and the user calls this group to return the results which triggers an update function that goes into this specific group being reference and updates the kittyPriceTomorrow member.

//struct for users and group name
struct kitties {
    bytes groupName;
    account user1;
    account kitty1;
    uint kittyPriceToday;
    uint kittyPriceTomorrow;
}
    
//array to hold structs
kitties[] public allKitties;

I anticipated the following to locate the group being requested, verify the end date of the time has been reached, locate the price at the set endtime and update the struct with the price at that time inside the specific struct. This is based off other examples I've seen but visually and logically i am struggling with the comprehending on where i am suppose to input the group name that the user gave for the specific struct to be updated. I would think its allKitties.kitties.groupName.kittyPriceToday but I seem to be missing something fundamental to complete the update. please help!

function updateKittyStruct() public {
    //storing the struct to the array
    kitties storage allkitties = allkitties
    //pass in the updated values below
    allkitties.kittyPriceToday = xyzPrice;
    // something like below 
    allKitties.kitties.groupName.kittyPriceToday = xyzPrice;
}
1
  • Please, can you explain better your question? Commented Apr 3, 2022 at 17:10

1 Answer 1

0

Since kitties[] is a dynamically sized array, first you would push() a value to create the first element at index 0. You can then access this array index to update the struct properties. I would probably name the struct Kitty if it represent a single entity.

kitties memory _kitty;
_kitty.kittyPriceToday = 1 ether;
_kitty.kittyPriceTomorrow = 2 ether;
// ... etc

// push to the array
allKitties.push(_kitty);

// now we can access by index
allKitties[0].kittyPriceToday = 5 ether;

Note that the delete keyword will only set the index back to it's initial value and not resize the array, you will need to track the element indexes and pop() the one you don't want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.