After some research into C# and some test with LINQ with easy examples. I wanted to apply this knowledge into my problem.
My DataStructure as seen from the variable screen (Visual Studio)
- wm
- Nations
- [0]
- Name "USA"
- stockpile
- [0]
- Name "Coal"
- Quantity "quantity"
- Value "value"
- [1] //Same as above
My attempt to access "Coal" has been:
var result = wm.Nations.Find(r => r.Name == "USA")
Console.WriteLine(result + " Result");
But only returns [EconomyTest.NationBuilder]
which is an object. How can i extract a string from that object or at least point to USA and access to the stockpile?
Edit1: Data Structure Declaration
//Declarations of Lists
public List<ResourceTemplate> stockpile = new List<ResourceTemplate>();
public List<Money> money = new List<Money>();
public List<PopulationTemplate> population = new
List<PopulationTemplate>();
public NationBuilder(string name)//Constructor
{
this.Name = name;
stockpile = new List<ResourceTemplate>();
money = new List<Money>();
population = new List<PopulationTemplate>();
}
//World Market (Where the actual storage happens)
public WorldMarket()//Constructor
{
//Declaration list
Nations = new List<NationBuilder>();
}
internal List<NationBuilder> Nations {get; set;}
public void AddToWorldMarket(NationBuilder nation)
{
Nations.Add(nation);
}
//Finally how it is managed/used:
WorldMarket wm = new WorldMarket();//Helps with adding those newly
created nations into a List
foreach (string name in NationNames)
{
NationBuilder nation = new NationBuilder(name);//Creates new
Objects nations and used the name provided before
nation.AddResource("Coal", 500, 10);
nation.AddResource("Water", 100, 10);
nation.AddMoney(100);
nation.AddPopulation(1000);
wm.AddToWorldMarket(nation);
Edit2: Function asked in the comments
public void AddResource(string itemName, int quantity, float
value)//Adds Resources to the stockpile
{
stockpile.Add(new ResourceTemplate {Name = itemName, Quantity =
quantity, Value = value });
}