1

Here is the code on how to grab from and array:

struct Collection {
    uint collectionId;
    uint collectionType;
    address collectionOwnerAddress;
    uint value;
}

Collection[] public collections;

function getCollection (uint _collectionId) view public returns (uint) {
     return collections[_collectionId].value;
}

My question is: I'm curious if I can grab the collection by the collection owners address. Something like this? Can I select by both msg.sender and collectionType?

function getCollection (uint _collectionType) view public returns (uint) {
     // select by _collectionType and msg.sender here
     return collections[msg.sender].value;
}

I'm trying to incrementally increase the value. So during addCollection if the array doesn't exist with that member then it creates a new array.push row. But, if the user already has an index in the array it collections[_collectionId].value++ instead of adding another array index row for that user. This above question relates because I cant loop through the whole collections array to see if that member already started a collection.

1 Answer 1

2

What you want to do is basically the functionnality proposed by a mapping. Why not use a mapping then?

struct Collection {
    uint collectionId;
    uint value;
}
mapping (address => Collection) collections;

Then you can check in O(1) if an address already has a collection.

5
  • What about the example where I want to select by 2? For example CollectionType being 1 and collectionOwnerAddress being msg.sender Mar 23, 2018 at 23:45
  • Well, you can do a mapping of a mapping in this case. Like : mapping(address => mapping (uint => Collection)) collections, where the second mapping is the CollectionType. Mar 24, 2018 at 10:12
  • If possible can you answer it in context? Mar 26, 2018 at 17:21
  • 1
    I did. You can think a bit about what your problem was, and what solution I proposed, play with it, write down examples, anything you want. I am not here to serve you a hot freshly prepared premade answer so you can copy/paste it. Mar 26, 2018 at 17:27
  • 1
    Valid point.... Mar 26, 2018 at 17:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.