0
\$\begingroup\$

I'm currently trying to figure out how I can keep ever-changing variable values and make it available for other classes to access the said variables.

In my example, I have the following:

public class Item
{
    public int Quantity;
    public int BuyPrice;
    public int SellPrice;
}

The value for all the variables in Item class will change every day.

Let's say I'm entering Day #2, how do I save class Item's variables value for Day #1 so that other classes would be able to refer to Day #1 value on Day #2?

Further to the my earlier question, there will be multiple Vendor classes having their own instances of Item and these Vendor classes will be visited by Agent classes.

I would like for the Agent to loop through all instances of Item sell price owned by the last Vendor it visited. Agent will hold a record of when it last visited the Vendor and will request for Item sell prices on that particular date by that particular Vendor.

Hope that make sense.

\$\endgroup\$
2
  • \$\begingroup\$ Presumably you considered storing an array or list of Item instances, where each entry is one time sample? Where did you get stuck with this? \$\endgroup\$
    – DMGregory
    Commented Jul 24, 2019 at 2:46
  • \$\begingroup\$ @DMGregory, I'm still early in conceptualization stage and I kinda stuck at figuring out the best way to store such. My first thought was Dictionary<Item, BuyPrice>, Dictionary<Item, SellPrice> and another Dictionary<Item, Quantity> but that would get too complicated too soon. \$\endgroup\$ Commented Jul 24, 2019 at 2:59

1 Answer 1

0
\$\begingroup\$

You can store them in an array or List<>, up to the maximum number of days of history you want to keep.

There are several ways to structure such a representation, but given how you phrased your query, what I would suggest would be to first wrap the changing cost data into its own type (because, presumably, some aspects of the item such as perhaps the name won't change every day):

struct CostData {
  public int Quantity;
  public int BuyPrice;
  public int SellPrice;
}

Then modify Item to hold a list of CostData

class Item {
  List<CostData> Cost;
};

Cost[Cost.Length - 1] could be "today's" data, and Cost[Cost.Length - 1 - n] would be the data for \$n\$ days ago; when the day rolls over you just insert the new day's cost onto the back of the list. I'd suggest you actually write methods to access the data though, rather than relying on consumers of the API remembering that convention.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks Josh. What if there are two Vendors who had their own instance of Item? So in this case, on one particular date, there'll be VendorA Item and VendorB Item. \$\endgroup\$ Commented Jul 24, 2019 at 2:55
  • 1
    \$\begingroup\$ What do you want to happen? If each vendor has a List<Item> and the items in the lists are unique instances of Item, then each vendor has a unique history of prices for their unique items. If instead, the items may exist in both lists (since Item is a reference type), then history gets shared. You can make either scenario happen, but you need to decide what you want. It might be helpful if you describe the context of your items and vendors in more detail in your question. \$\endgroup\$
    – user1430
    Commented Jul 24, 2019 at 2:59
  • \$\begingroup\$ Josh, I get the general idea. Your suggestion to encapsulate price data in its own class is something that I missed during my rubber duck session. \$\endgroup\$ Commented Jul 24, 2019 at 6:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .