Skip to content

Instantly share code, notes, and snippets.

@PeterJCLaw
Created May 21, 2012 20:04
Show Gist options
  • Select an option

  • Save PeterJCLaw/2764328 to your computer and use it in GitHub Desktop.

Select an option

Save PeterJCLaw/2764328 to your computer and use it in GitHub Desktop.
C# protected and an assault
using System;
namespace Bacon
{
class Victim
{
protected int money = 100;
public override string ToString() {
return this.money.ToString();
}
public void reclaim(Offender offender)
{
var diff = 100 - this.money;
offender.money -= diff;
this.money = 100;
}
}
class Offender : Victim
{
public void assault(Victim victim)
{
this.money += victim.money;
victim.money = 0;
}
}
class Prog
{
public static void Main()
{
var victim = new Victim();
var offender = new Offender();
offender.assault(victim);
Console.WriteLine(victim);
victim.reclaim(offender);
Console.WriteLine(victim);
}
}
}
/* Compiler errros:
goo.cs(26,34): error CS1540: Cannot access protected member `Bacon.Victim.money' via a qualifier of type `Bacon.Victim'. The qualifier must be of type `Bacon.Offender' or derived from it
goo.cs(8,23): (Location of the symbol related to previous error)
goo.cs(27,20): error CS1540: Cannot access protected member `Bacon.Victim.money' via a qualifier of type `Bacon.Victim'. The qualifier must be of type `Bacon.Offender' or derived from it
goo.cs(8,23): (Location of the symbol related to previous error)
Compilation failed: 2 error(s), 0 warnings
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment