private void UpdateUi() { numberOfThingies.Text = "You have " + myThingies.Count + " thingies."; valueOfLatestThingy.Text = string.Format("The latest thingy has the value {0}.", myThingies[myThingies.Count - 1].Value); Thingy biggestThingy = GetLargestThingy(); largestThingy.Text = string.Format( "The largest thingy is #{0}, with value {1}.", myThingies.IndexOf(biggestThingy) + 1, biggestThingy.Value); listOfThingies.Text = String.Empty; int thingyIndex = 1; foreach (Thingy thingy in myThingies) { listOfThingies.Text += string.Format( "Thingy #{2} has value {0}.{1}", thingy.Value, Environment.NewLine, thingyIndex++); } listOfThingies.Text = String.Empty; for (int i = 0; i < myThingies.Count; i++) { Thingy thingy = myThingies[i]; listOfThingies.Text += string.Format( "Thingy #{2} has value {0}.{1}", thingy.Value, Environment.NewLine, i + 1); } }
private void frobLatest_Click(object sender, EventArgs e) { if (myThingies.Count == 0) { MessageBox.Show("You have no thingies!"); return; } // (<type_name>) is the swear-to-god operator. // Dear Compiler: I swear to god that this is a Thingy. // Also known as an "explicit type conversion". object someObject = myThingies[myThingies.Count - 1]; // I can't do this, because a generic "object" doesn't // know how to be Frobbed. // someObject.Frob(); // I tell the compiler, "This particular generic object // is actually a Thingy, I swear to god." Thingy latestThingy = (Thingy)someObject; // Now, I can tell this thingy, "Frob yourself." latestThingy.Frob(); UpdateUi(); }
// private int GetValueFromThingy(Thingy thingy) // { // return thingy.Value; // } private Thingy GetLargestThingy3() { Thingy largestThingy = myThingies[0]; foreach (Thingy thingy in myThingies) { if (thingy.Value > largestThingy.Value) { largestThingy = thingy; } } return(largestThingy); }