How to subscribe an EventHandler? [Answered]

I don't understand how to subscribe this. I don't get it. I honestly don't even know why I used an EventHandler instead of an Action or something.
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();

public class CharberryTree
{
public event EventHandler<RipenedEventArgs>? Ripened;

public CharberryTree()
{

}
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
}
}
}

public class Notifier
{
public Notifier(CharberryTree tree)
{
tree.Ripened += OnRipened();
}
public void OnRipened()
{

}
}

public class Harvester
{
public Harvester(CharberryTree tree)
{

}
}

public class RipenedEventArgs : EventArgs
{
public bool Ripe { get; set; }
public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();

public class CharberryTree
{
public event EventHandler<RipenedEventArgs>? Ripened;

public CharberryTree()
{

}
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
}
}
}

public class Notifier
{
public Notifier(CharberryTree tree)
{
tree.Ripened += OnRipened();
}
public void OnRipened()
{

}
}

public class Harvester
{
public Harvester(CharberryTree tree)
{

}
}

public class RipenedEventArgs : EventArgs
{
public bool Ripe { get; set; }
public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
230 Replies
BigggMoustache
BigggMoustacheOP3y ago
actually at this point I'm sure I'm doing the exercise wrong.
sibber
sibber3y ago
tree.Ripened += OnRipened; you dont want to invoke the method, you want the method itself
BigggMoustache
BigggMoustacheOP3y ago
yeah that's right. I just changed the EventHandler to an Action? also
sibber
sibber3y ago
also Notifier should be called NotificationHandler, it doesnt notify anything sure
BigggMoustache
BigggMoustacheOP3y ago
Oh really okay
sibber
sibber3y ago
or preferably change the handler signature
BigggMoustache
BigggMoustacheOP3y ago
Idk what that means lol
sibber
sibber3y ago
public void OnRipened(object? sender, RipenedEventArgs args)
{

}
public void OnRipened(object? sender, RipenedEventArgs args)
{

}
BigggMoustache
BigggMoustacheOP3y ago
Okay yeah I could have done that.
sibber
sibber3y ago
the handler's signature has to match the event delegate's signature
BigggMoustache
BigggMoustacheOP3y ago
yeah thanks. Give me one second to figure out next question.
sibber
sibber3y ago
which is EventHandler<RipenedEventArgs>
BigggMoustache
BigggMoustacheOP3y ago
Okay I remember reading that EventArgs is how data is passed
sibber
sibber3y ago
yeah thats one way to pass data its the convention tho
BigggMoustache
BigggMoustacheOP3y ago
and my interpretation was using that to pass the Ripe value of the tree did I do that correctly?
sibber
sibber3y ago
sure yeah
BigggMoustache
BigggMoustacheOP3y ago
okay I'm changing back to EventHandler then lol sec
sibber
sibber3y ago
maybe name it IsRipe to be clearer
BigggMoustache
BigggMoustacheOP3y ago
in the eventarg?
sibber
sibber3y ago
and the class
BigggMoustache
BigggMoustacheOP3y ago
cause the top portion is part copied from example ye i dun wanna do that lol
sibber
sibber3y ago
ah fair
BigggMoustache
BigggMoustacheOP3y ago
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();
public class CharberryTree
{
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
}
}
}
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();
public class CharberryTree
{
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
}
}
}
This is the reference it gave
Objectives:
• Make a new project that includes the above code.
• Add a Ripened event to the CharberryTree class that is raised when the tree ripens.
• Make a Notifier class that knows about the tree (Hint: perhaps pass it in as a constructor parameter) and subscribes to its Ripened event. Attach a handler that displays something like “A charberry fruit has ripened!” to the console window.
• Make a Harvester class that knows about the tree (Hint: like the notifier, this could be passed as a constructor parameter) and subscribes to its Ripened event. Attach a handler that sets the tree’s Ripe property back to false.
• Update your main method to create a tree, notifier, and harvester, and get them to work together to grow, notify, and harvest forever
Objectives:
• Make a new project that includes the above code.
• Add a Ripened event to the CharberryTree class that is raised when the tree ripens.
• Make a Notifier class that knows about the tree (Hint: perhaps pass it in as a constructor parameter) and subscribes to its Ripened event. Attach a handler that displays something like “A charberry fruit has ripened!” to the console window.
• Make a Harvester class that knows about the tree (Hint: like the notifier, this could be passed as a constructor parameter) and subscribes to its Ripened event. Attach a handler that sets the tree’s Ripe property back to false.
• Update your main method to create a tree, notifier, and harvester, and get them to work together to grow, notify, and harvest forever
This is the instruction and my first post is what I've done
sibber
sibber3y ago
i think the harvester is the one that should subscribe to the event
BigggMoustache
BigggMoustacheOP3y ago
So my problem with the EventHandler thing is I don't sub the event through the ctor like instructed
sibber
sibber3y ago
wdym
BigggMoustache
BigggMoustacheOP3y ago
the instruction suggest taking the tree through the other classes constructor to sub the event in the CharberryTree class
sibber
sibber3y ago
yeah whats the problem with that
BigggMoustache
BigggMoustacheOP3y ago
I'd have to set a property / field to hold the what the constructor paramter takes in, and then reference that in the class taking the EventArg? I'm just not sure how it works lol. The EventArg example didn't have any stuff poppin' in a constructors parameters.
sibber
sibber3y ago
well you only need to subscribe to the event which you already did
public class Notifier
{
public Notifier(CharberryTree tree)
{
tree.Ripened += OnRipened();
}
public void OnRipened()
{

}
}
public class Notifier
{
public Notifier(CharberryTree tree)
{
tree.Ripened += OnRipened();
}
public void OnRipened()
{

}
}
so all thats left for this class is to print a message to the console saying the tree has ripened anyway gtg its 2 am goodluck
BigggMoustache
BigggMoustacheOP3y ago
ty lol gn homie well I gott ajet anyways. I guess I'll finish this later too. xD. Thanks again homie.
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();

public class CharberryTree
{
public event EventHandler<RipenedEventArgs> ?Ripened;

public CharberryTree()
{

}
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
}
}
}

public class Notifier
{
public CharberryTree Tree { get; set; }
public Notifier(CharberryTree tree)
{
Tree = tree;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree) Console.WriteLine("Tree has ripened!");
}
}

public class Harvester
{
public CharberryTree Tree { get; set; }
public Harvester(CharberryTree tree)
{
Tree = tree;
}
public void OnRipened(object sender, RipenedEventArgs args)
{

}
}

public class RipenedEventArgs : EventArgs
{
public bool Ripe { get; set; }
public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();

public class CharberryTree
{
public event EventHandler<RipenedEventArgs> ?Ripened;

public CharberryTree()
{

}
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
}
}
}

public class Notifier
{
public CharberryTree Tree { get; set; }
public Notifier(CharberryTree tree)
{
Tree = tree;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree) Console.WriteLine("Tree has ripened!");
}
}

public class Harvester
{
public CharberryTree Tree { get; set; }
public Harvester(CharberryTree tree)
{
Tree = tree;
}
public void OnRipened(object sender, RipenedEventArgs args)
{

}
}

public class RipenedEventArgs : EventArgs
{
public bool Ripe { get; set; }
public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
My event in Notifier is not being called. it's getting to my Ripened?.Invoke() line and going back to the while loop at start I'm noticing my EventHandler<RipenedEventArgs> ?Ripened; is null when invoked and I'm not sure why that is either.
ero
ero3y ago
You never subscribe anything to tree.Ripened
BigggMoustache
BigggMoustacheOP3y ago
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();

public class CharberryTree
{
public event EventHandler<RipenedEventArgs> ?Ripened;

public CharberryTree()
{

}
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
}
}
}

public class Notifier
{
public CharberryTree Tree { get; set; }
public Notifier(CharberryTree tree)
{
Tree = tree;
tree.Ripened += OnRipened;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree) Console.WriteLine("Tree has ripened!");
}
}

public class Harvester
{
public CharberryTree Tree { get; set; }
public Harvester(CharberryTree tree)
{
Tree = tree;
}
public void OnRipened(object sender, RipenedEventArgs args)
{

}
}

public class RipenedEventArgs : EventArgs
{
public bool Ripe { get; set; }
public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();

public class CharberryTree
{
public event EventHandler<RipenedEventArgs> ?Ripened;

public CharberryTree()
{

}
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
}
}
}

public class Notifier
{
public CharberryTree Tree { get; set; }
public Notifier(CharberryTree tree)
{
Tree = tree;
tree.Ripened += OnRipened;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree) Console.WriteLine("Tree has ripened!");
}
}

public class Harvester
{
public CharberryTree Tree { get; set; }
public Harvester(CharberryTree tree)
{
Tree = tree;
}
public void OnRipened(object sender, RipenedEventArgs args)
{

}
}

public class RipenedEventArgs : EventArgs
{
public bool Ripe { get; set; }
public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
If I understand correctly tree.Ripened += OnRipened; in the public Notifier(CharberryTree tree) subscribes the event? Sorry I forgot to update code
ero
ero3y ago
and what part of your code calls the Notifier constructor?
BigggMoustache
BigggMoustacheOP3y ago
I thought the MaybeGrow() method did with Ripened.Invoke()
ero
ero3y ago
no, none of that code contains a call to any constructor
BigggMoustache
BigggMoustacheOP3y ago
Oh I never made a Notifier or Harvester so I can't use them lol tyvm. Hey so quick question about what was happenig
BigggMoustache
BigggMoustacheOP3y ago
before I created the new objects this was always null
BigggMoustache
BigggMoustacheOP3y ago
I don't understand.
ero
ero3y ago
.
BigggMoustache
BigggMoustacheOP3y ago
so the event being null is having nothing subscribed okay that's the first time I've ran into anything that was null because of a relationship to something else iirc so I have another question I guess well one moment let me collect mythoughts so the goal of the exercise is to have the tree ripen, notify, and havest (Ripe = false again) how to order events?
MODiX
MODiX3y ago
Ero#1111
REPL Result: Success
Foo foo = new();

foo.BarNull();
foo.Bar += (s, e) => { };
foo.BarNull();

class Foo
{
public event EventHandler<string>? Bar;
public void BarNull() => Console.WriteLine(Bar is null);
}
Foo foo = new();

foo.BarNull();
foo.Bar += (s, e) => { };
foo.BarNull();

class Foo
{
public event EventHandler<string>? Bar;
public void BarNull() => Console.WriteLine(Bar is null);
}
Console Output
True
False
True
False
Compile: 780.351ms | Execution: 89.895ms | React with ❌ to remove this embed.
ero
ero3y ago
i'm not super familiar with event patterns unfortunately
BigggMoustache
BigggMoustacheOP3y ago
I'll be hoenst that shoots entirely over my head no problem. could you explain the thing?
ero
ero3y ago
what thing? i assume foo.Bar += (s, e) => { }; is what confuses you?
BigggMoustache
BigggMoustacheOP3y ago
yes
ero
ero3y ago
just a very shorthand form of subscribing
BigggMoustache
BigggMoustacheOP3y ago
I don't get the output lol so maybe the whole thing confuses xD
ero
ero3y ago
oh i was just showing whether foo.Bar was null or not showing that it is null before subscribing anything, and not null after
BigggMoustache
BigggMoustacheOP3y ago
oh okay OH you were showing the error I had okay yeah tyvm. That was a new experience, where the relation itself is the content checked yeah that is illuminating. could you explain the shorthand? what is s, e?
ero
ero3y ago
like i'd personally just have Actions for this, i rarely use delegates or events sender and whatever type the EventHandler takes (in my case, string)
BigggMoustache
BigggMoustacheOP3y ago
I used EventHandler because it seemed like the easiest way to share the Ripe property in the event
ero
ero3y ago
i just woulnd't even share it like why would another class care about my tree growing and ripening
BigggMoustache
BigggMoustacheOP3y ago
well my goal was just to play with it because I'd never used it. well Harvester *will set *Ripe back to false so I was going to try to use the event data to determine that So just wanted to explore the functions I guess Why do you like Actions? Oh yeah I forgot EventHandler has two parameters
ero
ero3y ago
!e
Tree tree = new Tree();
tree.OnRipen = _ => Console.WriteLine("My tree ripened!");

while (true)
tree.TryGrow();


class Tree
{
public bool IsRipe { get; set; }
public Action<Tree>? OnRipen { get; set; }

public void TryGrow()
{
if (Random.Shared.Next() < 0.00000001 && !IsRipe)
{
IsRipe = true;
OnRipen?.Invoke(this);
}
}
}
Tree tree = new Tree();
tree.OnRipen = _ => Console.WriteLine("My tree ripened!");

while (true)
tree.TryGrow();


class Tree
{
public bool IsRipe { get; set; }
public Action<Tree>? OnRipen { get; set; }

public void TryGrow()
{
if (Random.Shared.Next() < 0.00000001 && !IsRipe)
{
IsRipe = true;
OnRipen?.Invoke(this);
}
}
}
MODiX
MODiX3y ago
Ero#1111
REPL Error
An error occurred while sending a request to the REPL service. This may be due to a StackOverflowException or exceeding the 30 second timeout. Details: The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing.
Tried to execute
ero
ero3y ago
whoops, probability too small
BigggMoustache
BigggMoustacheOP3y ago
It does seem easier overall to use lol
ero
ero3y ago
but maybe there are some patterns i don't know about
BigggMoustache
BigggMoustacheOP3y ago
shrug idk either lol. I just know from example reference EventHandler was made to pass data around for you so I wanted to give it a go. I'll ask for criticism once complete in a minute or two.
BigggMoustache
BigggMoustacheOP3y ago
BigggMoustache
BigggMoustacheOP3y ago
CharberryTree tree = new CharberryTree();
Notifier notifier = new Notifier(tree);
Harvester harvester = new Harvester(tree);

while (true)
tree.MaybeGrow();

public class CharberryTree
{
public event EventHandler<RipenedEventArgs> ?Ripened;

public CharberryTree()
{

}
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
}
}
}

public class Notifier
{
public CharberryTree Tree { get; set; }
public Notifier(CharberryTree tree)
{
Tree = tree;
tree.Ripened += OnRipened;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree) Console.WriteLine("Tree has ripened!");
}
}

public class Harvester
{
public CharberryTree Tree { get; set; }
public Harvester(CharberryTree tree)
{
Tree = tree;
tree.Ripened += OnRipened;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree && args.Ripe == true)
{
Console.WriteLine("Havesting Charberry.");
Tree.Ripe = false;
}
}
}

public class RipenedEventArgs : EventArgs
{
public bool Ripe { get; set; }
public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
CharberryTree tree = new CharberryTree();
Notifier notifier = new Notifier(tree);
Harvester harvester = new Harvester(tree);

while (true)
tree.MaybeGrow();

public class CharberryTree
{
public event EventHandler<RipenedEventArgs> ?Ripened;

public CharberryTree()
{

}
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
}
}
}

public class Notifier
{
public CharberryTree Tree { get; set; }
public Notifier(CharberryTree tree)
{
Tree = tree;
tree.Ripened += OnRipened;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree) Console.WriteLine("Tree has ripened!");
}
}

public class Harvester
{
public CharberryTree Tree { get; set; }
public Harvester(CharberryTree tree)
{
Tree = tree;
tree.Ripened += OnRipened;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree && args.Ripe == true)
{
Console.WriteLine("Havesting Charberry.");
Tree.Ripe = false;
}
}
}

public class RipenedEventArgs : EventArgs
{
public bool Ripe { get; set; }
public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
Pobiega
Pobiega3y ago
Re event vs action, there are a few tradeoffs events can have multiple listeners, for one
BigggMoustache
BigggMoustacheOP3y ago
So I'll be honest I'm confused why in the public class Harvester method OnRipened using Tree.Ripe = false; sets the tree object bool of Ripe back to false sorry for interrupting Pobiega
Pobiega
Pobiega3y ago
np and events sort of represent a less tightly coupled integration, imho
BigggMoustache
BigggMoustacheOP3y ago
okay 1 listener on Action sounds weird, why is that?
Pobiega
Pobiega3y ago
wdym? thats just how it is
BigggMoustache
BigggMoustacheOP3y ago
lol okay idk
Pobiega
Pobiega3y ago
`cs
Tree tree = new Tree();
tree.OnRipen = _ => Console.WriteLine("My tree ripened!");

while (true)
tree.TryGrow();


class Tree
{
public bool IsRipe { get; set; }
public Action<Tree>? OnRipen { get; set; }

public void TryGrow()
{
if (Random.Shared.Next() < 0.00000001 && !IsRipe)
{
IsRipe = true;
OnRipen?.Invoke(this);
}
}
}
Tree tree = new Tree();
tree.OnRipen = _ => Console.WriteLine("My tree ripened!");

while (true)
tree.TryGrow();


class Tree
{
public bool IsRipe { get; set; }
public Action<Tree>? OnRipen { get; set; }

public void TryGrow()
{
if (Random.Shared.Next() < 0.00000001 && !IsRipe)
{
IsRipe = true;
OnRipen?.Invoke(this);
}
}
}
this code that Ero posted for example once you set the OnRipen action, how would someone else add their listener to it? they can't. they'd replace the current one events support the += MyHandler tjhing out of the box
BigggMoustache
BigggMoustacheOP3y ago
I didn't try the multidelegate(probably wrong word) or chaining thing that was talked of in book is that event specific?
Pobiega
Pobiega3y ago
Action is a delegate type
BigggMoustache
BigggMoustacheOP3y ago
OH okay
Pobiega
Pobiega3y ago
nah you're correct events are supported by a multi delegate
BigggMoustache
BigggMoustacheOP3y ago
okay thanks for that. why event vs eventhandler then?
Pobiega
Pobiega3y ago
hm? well an event is an event honestly that part is a bit confusing
Pobiega
Pobiega3y ago
Handling and Raising Events
Learn to handle and raise .NET events, which are based on the delegate model. This model lets subscribers register with or receive notifications from providers.
Pobiega
Pobiega3y ago
class Counter
{
public event EventHandler ThresholdReached;

protected virtual void OnThresholdReached(EventArgs e)
{
EventHandler handler = ThresholdReached;
handler?.Invoke(this, e);
}

// provide remaining implementation for the class
}
class Counter
{
public event EventHandler ThresholdReached;

protected virtual void OnThresholdReached(EventArgs e)
{
EventHandler handler = ThresholdReached;
handler?.Invoke(this, e);
}

// provide remaining implementation for the class
}
EventHandler is a type, much like Counter or string etc so to make an event, you say event MyEventType MyEventName;
BigggMoustache
BigggMoustacheOP3y ago
right right
Pobiega
Pobiega3y ago
the type declares what parameters a handler must have
BigggMoustache
BigggMoustacheOP3y ago
Oh so it's a premade type of event
Pobiega
Pobiega3y ago
but since its all delegates in the background, you can assign any method to be your "handler"
BigggMoustache
BigggMoustacheOP3y ago
yeah okay
Pobiega
Pobiega3y ago
EventHandler is a premade delegate type, yes
BigggMoustache
BigggMoustacheOP3y ago
dude this is wild lol
Pobiega
Pobiega3y ago
mhm it can be confusing but ITS ALL DELEGATES 😄
BigggMoustache
BigggMoustacheOP3y ago
I know lmaooooo the book made sure to emphasize over and over that it was uh, delegates underneath
Pobiega
Pobiega3y ago
essentially, use event for actual events, like OnRipened etc
BigggMoustache
BigggMoustacheOP3y ago
made me think of inumerable under things so like Linq
Pobiega
Pobiega3y ago
I tend to use Action like "events" when I do testing stubs since I know I will never ever have more than one "listener/handler"
BigggMoustache
BigggMoustacheOP3y ago
okay yeah. I'm finding things up until this point were super easy but things like this are like, multi step processes you put on top of all the other stuff idk haha.
Pobiega
Pobiega3y ago
I seem to recall you asking some questions before, so not "super easy" :p
BigggMoustache
BigggMoustacheOP3y ago
I tried learning events a long time ago when I had a worse grasp of C# LOL yeah I'm just saying it's like a new layer of abstract concept to slap on the top of things
Pobiega
Pobiega3y ago
haha yeah it pretty much is
BigggMoustache
BigggMoustacheOP3y ago
could you offer whatever basic criticism of what I wrote?
CharberryTree tree = new CharberryTree();
Notifier notifier = new Notifier(tree);
Harvester harvester = new Harvester(tree);

while (true)
tree.MaybeGrow();

public class CharberryTree
{
public event EventHandler<RipenedEventArgs> ?Ripened;

public CharberryTree()
{

}
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
}
}
}

public class Notifier
{
public CharberryTree Tree { get; set; }
public Notifier(CharberryTree tree)
{
Tree = tree;
tree.Ripened += OnRipened;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree) Console.WriteLine("Tree has ripened!");
}
}

public class Harvester
{
public CharberryTree Tree { get; set; }
public Harvester(CharberryTree tree)
{
Tree = tree;
tree.Ripened += OnRipened;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree && args.Ripe == true)
{
Console.WriteLine("Havesting Charberry.");
Tree.Ripe = false;
}
}
}

public class RipenedEventArgs : EventArgs
{
public bool Ripe { get; set; }
public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
CharberryTree tree = new CharberryTree();
Notifier notifier = new Notifier(tree);
Harvester harvester = new Harvester(tree);

while (true)
tree.MaybeGrow();

public class CharberryTree
{
public event EventHandler<RipenedEventArgs> ?Ripened;

public CharberryTree()
{

}
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
}
}
}

public class Notifier
{
public CharberryTree Tree { get; set; }
public Notifier(CharberryTree tree)
{
Tree = tree;
tree.Ripened += OnRipened;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree) Console.WriteLine("Tree has ripened!");
}
}

public class Harvester
{
public CharberryTree Tree { get; set; }
public Harvester(CharberryTree tree)
{
Tree = tree;
tree.Ripened += OnRipened;
}
public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree && args.Ripe == true)
{
Console.WriteLine("Havesting Charberry.");
Tree.Ripe = false;
}
}
}

public class RipenedEventArgs : EventArgs
{
public bool Ripe { get; set; }
public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
OOHHH I asked earlier about that Tree.Ripe = false toward the bottom
Pobiega
Pobiega3y ago
public event EventHandler<RipenedEventArgs> ?Ripened; that questionmark really bothers me its part of the type, not the name
BigggMoustache
BigggMoustacheOP3y ago
Oh okay I always forget where those go tbh lol I thought it was for the variable specifically xD
Pobiega
Pobiega3y ago
well it is, but its part of the type you are saying this variable is of type EventHandler<RipenedEventArgs>? so it can be null so, this seems okay. I'd perhaps change the Tree.Ripe = false to be tree.Harvest() or something depends on how you wanna divvie up your responsibliites
BigggMoustache
BigggMoustacheOP3y ago
yeah ofc that makes sense
Pobiega
Pobiega3y ago
should a tree know how to be harvested, or should a harvester know about trees?
BigggMoustache
BigggMoustacheOP3y ago
OH BUT HEY that tree thing there how does referencing the class property change the other classes property
Pobiega
Pobiega3y ago
hm? wdym
ero
ero3y ago
Exactly that way
Pobiega
Pobiega3y ago
yeah exactly that way :p
BigggMoustache
BigggMoustacheOP3y ago
Tree in that case is of the Harvester class
ero
ero3y ago
It's a reference
BigggMoustache
BigggMoustacheOP3y ago
OH
Pobiega
Pobiega3y ago
its not a copy its a reference to the actual thing $refvsvalue
BigggMoustache
BigggMoustacheOP3y ago
shoot I did not realize that
ero
ero3y ago
Classes are always passed byref
BigggMoustache
BigggMoustacheOP3y ago
I think I need a cheat sheet to put that on lol
Pobiega
Pobiega3y ago
your "architecture" here is a bit unusual too in that your notifier/harvester both hold a hard ref to the tree instead of just registering their listeners and getting the tree that ripened from there what if you had one harvester responsible for 10 trees? the event args should contain the tree that ripened (its the sender in your case)
BigggMoustache
BigggMoustacheOP3y ago
yeah yeah okay so just the constructor param is enough? well the Harvester uses the reference
ero
ero3y ago
Perhaps you'd want a static harvester class which acts as an extension on the tree
BigggMoustache
BigggMoustacheOP3y ago
I don't understand that tbh but I'm excited to be exposed to the idea lol
Pobiega
Pobiega3y ago
public class Harvester
{
public Harvester(CharberryTree tree)
{
tree.Ripened += OnRipened;
}

public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree tree && args.Ripe == true)
{
Console.WriteLine("Havesting Charberry.");
tree.Ripe = false;
}
}
}
public class Harvester
{
public Harvester(CharberryTree tree)
{
tree.Ripened += OnRipened;
}

public void OnRipened(object sender, RipenedEventArgs args)
{
if (sender is CharberryTree tree && args.Ripe == true)
{
Console.WriteLine("Havesting Charberry.");
tree.Ripe = false;
}
}
}
BigggMoustache
BigggMoustacheOP3y ago
Also I tried using the sender object to get the tree and couldn't figure it out OH okay sender is not a reference to it
Pobiega
Pobiega3y ago
obviously, since you register your handler in the ctor, this isn't super useful you'd probably move that out to a new method no it is, but its being sent as object so you do a typecheck
ero
ero3y ago
I said static extension for a reason
Pobiega
Pobiega3y ago
if sender is CharberryTree tree yup
BigggMoustache
BigggMoustacheOP3y ago
hmm that's cool okay can you give example of static here??
Pobiega
Pobiega3y ago
your harvester doesnt really need to be what it is currently
BigggMoustache
BigggMoustacheOP3y ago
oh?
Pobiega
Pobiega3y ago
yeah all it does is change something on the source and print a message to the console
ero
ero3y ago
Ok i can't code this on my phone gimme a bit
Pobiega
Pobiega3y ago
that could easily be a static method an extension method to register it with would be neat
BigggMoustache
BigggMoustacheOP3y ago
Oh well I'm just following exercise instruction which was to make it a class
Pobiega
Pobiega3y ago
yeah, thats fine
BigggMoustache
BigggMoustacheOP3y ago
but I would love to know what you're talking about yeah dude I'm here to learn lmao, this is fucking great
Pobiega
Pobiega3y ago
because most of the time, you'll want it to be a class
BigggMoustache
BigggMoustacheOP3y ago
nice.
Pobiega
Pobiega3y ago
its just that because this example is so simple, and doesnt need any instance data... I'm sure Ero will post the code once they write it up
BigggMoustache
BigggMoustacheOP3y ago
Oh that's right lol. @Ero Take your time bud no rush. I really, really appreciate the instruction. so sender just grabs the object for a typecheck, it does not serve as a reference to the object as the type it is?
Pobiega
Pobiega3y ago
Also, regardless of how helpful you are, you really need to watch your snark. You are way to snarky way to often.
BigggMoustache
BigggMoustacheOP3y ago
so just to clarify, I can send bunches of EventArgs in params and set them, and they all get passed around with reference to the object they come from?
Pobiega
Pobiega3y ago
? look at how you invoke the event Ripened?.Invoke(this, new RipenedEventArgs(Ripe)); the first parameter this is the sender that gets exposed to any listener as does the event args, but its the same eventargs being passed to all listeners, one by one
BigggMoustache
BigggMoustacheOP3y ago
right. so I could put new RipenedEventArgs(Ripe, Age, Season, ...) in there?
Pobiega
Pobiega3y ago
yeah your eventargs is just an object with a bunch of props on it "here is the info related to the event"
BigggMoustache
BigggMoustacheOP3y ago
so its use is to pass all that around without needing a direct reference to the object? for decoupling?
Pobiega
Pobiega3y ago
for example, on an "OnClick" event it contains the mouse location, what button was pressed, etc
BigggMoustache
BigggMoustacheOP3y ago
ye
Pobiega
Pobiega3y ago
well you need the object ref to register the listener/handler but that might have happened via some abstraction yeah its a way to invert the dependency actually instead of your tree knowing about the harvester, its your harvester that knows about the tree at least initially
BigggMoustache
BigggMoustacheOP3y ago
That's so cool. 'hey look over here. It's a sender with args lol
Pobiega
Pobiega3y ago
do you know what the garbage collector is btw?
BigggMoustache
BigggMoustacheOP3y ago
oh yes I meant to ask how that'd work here unsubscribing in practice
Pobiega
Pobiega3y ago
yeah. unsubbing is the hard part events often lead to memory leaks, becuse if you dont unsub, the registered handler will keep the event source alive forever only when all the handlers get collected would the ref count reach 0 and the event source be collected
BigggMoustache
BigggMoustacheOP3y ago
yeah all of that is wooshing I mean I vaguely get it lol
Pobiega
Pobiega3y ago
the super simplified version of the garbage collector is that it just looks at how many references there are to an object
BigggMoustache
BigggMoustacheOP3y ago
can you show me in my code where it'd be practical given some additional circumstance if necessary?
Pobiega
Pobiega3y ago
if that number reaches 0, the object gets cleaned up ? what exactly?
BigggMoustache
BigggMoustacheOP3y ago
in my exercise, where would I unsub the events, and if not anywhere, what premise would provide a practical place?
Pobiega
Pobiega3y ago
uhm, well your tree grows forever in a while(true)
BigggMoustache
BigggMoustacheOP3y ago
yeah I know lol
Pobiega
Pobiega3y ago
so you dont have any leaks.
BigggMoustache
BigggMoustacheOP3y ago
I'm just saying to further my practical understanding of the matter so it's less of a floaty abstraction
Pobiega
Pobiega3y ago
imagine if you had multiple trees, and each tree had a chance to die each time it was harvested
BigggMoustache
BigggMoustacheOP3y ago
ya
Pobiega
Pobiega3y ago
upon death, you'd want to unsub any and all listeners the common way to do this is to just assign null to the event
ero
ero3y ago
Tree tree =
new Tree()
.AddNotifier<MyRipenNotifier>()
.AddHarvester<MyTreeHarvester>();

while (true)
tree.TryGrow();

class Tree
{
public bool IsRipe { get; set; }
public EventHandler<RipenEventArgs>? OnRipen;

public void TryGrow()
{
if (Random.Shared.Next() < 0.1 && !IsRipe)
{
IsRipe = true;
OnRipen?.Invoke(this, new(IsRipe));
}
}
}

static class TreeFarm
{
public static Tree AddNotifier<T>(this Tree tree) where T : IRipenNotifier
{
tree.OnRipen += T.OnNotify;
return tree;
}

public static Tree AddHarvester<T>(this Tree tree) where T : IHarvester
{
tree.OnRipen += T.OnHarvest;
return tree;
}
}

interface IRipenNotifier
{
static abstract void OnNotify(object? sender, RipenEventArgs e);
}

class MyRipenNotifier : IRipenNotifier
{
public static void OnNotify(object? sender, RipenEventArgs e)
{
if (sender is not Tree tree)
return;

Console.WriteLine("Tree is now ripe!");
}
}

interface IHarvester
{
static abstract void OnHarvest(object? sender, RipenEventArgs e);
}

class MyTreeHarvester : IHarvester
{
public static void OnHarvest(object? sender, RipenEventArgs e)
{
if (sender is not Tree tree)
return;

Console.WriteLine("Harvesting that tree!");
tree.IsRipe = false;
}
}

class RipenEventArgs : EventArgs
{
public RipenEventArgs(bool isRipe)
{
Ripened = isRipe;
}

public bool Ripened { get; }
}
Tree tree =
new Tree()
.AddNotifier<MyRipenNotifier>()
.AddHarvester<MyTreeHarvester>();

while (true)
tree.TryGrow();

class Tree
{
public bool IsRipe { get; set; }
public EventHandler<RipenEventArgs>? OnRipen;

public void TryGrow()
{
if (Random.Shared.Next() < 0.1 && !IsRipe)
{
IsRipe = true;
OnRipen?.Invoke(this, new(IsRipe));
}
}
}

static class TreeFarm
{
public static Tree AddNotifier<T>(this Tree tree) where T : IRipenNotifier
{
tree.OnRipen += T.OnNotify;
return tree;
}

public static Tree AddHarvester<T>(this Tree tree) where T : IHarvester
{
tree.OnRipen += T.OnHarvest;
return tree;
}
}

interface IRipenNotifier
{
static abstract void OnNotify(object? sender, RipenEventArgs e);
}

class MyRipenNotifier : IRipenNotifier
{
public static void OnNotify(object? sender, RipenEventArgs e)
{
if (sender is not Tree tree)
return;

Console.WriteLine("Tree is now ripe!");
}
}

interface IHarvester
{
static abstract void OnHarvest(object? sender, RipenEventArgs e);
}

class MyTreeHarvester : IHarvester
{
public static void OnHarvest(object? sender, RipenEventArgs e)
{
if (sender is not Tree tree)
return;

Console.WriteLine("Harvesting that tree!");
tree.IsRipe = false;
}
}

class RipenEventArgs : EventArgs
{
public RipenEventArgs(bool isRipe)
{
Ripened = isRipe;
}

public bool Ripened { get; }
}
i think this requires .net 7 and preview lang though
Pobiega
Pobiega3y ago
yup, static abstracts but its a nice example of how something like this could look in a "real" project given that your event handlers are simple generics + extension methods = superpowers
BigggMoustache
BigggMoustacheOP3y ago
so on tree death I'd just Ripened = null and that'd fix it?
Pobiega
Pobiega3y ago
for that case, yes there is a reverse thou, which is when the publisher (event source) lives longer than the subscriber if the subscriber upon death doesnt unsub itself
BigggMoustache
BigggMoustacheOP3y ago
I was just about to ask that lol and @Ero I'll ask about your thing in a moment. Tyvm
ero
ero3y ago
I'ma go to sleep
BigggMoustache
BigggMoustacheOP3y ago
❤️ okay. I appreciate the help bud.
Pobiega
Pobiega3y ago
its only really a problem when the lifetimes of the subscriber and the publisher differ greatly
BigggMoustache
BigggMoustacheOP3y ago
have a good night. why would sub need to keep reference of event around if source is gone? to use the data in the arg?
Pobiega
Pobiega3y ago
no its the publisher that knows about the subscribers
BigggMoustache
BigggMoustacheOP3y ago
oh okay
ero
ero3y ago
Yeah the order of execution here isn't really sensical
Pobiega
Pobiega3y ago
once the source dies, the handler will not be invoked
ero
ero3y ago
A tree shouldn't have a harvester
Pobiega
Pobiega3y ago
because the source is dead
BigggMoustache
BigggMoustacheOP3y ago
yeah i get that
ero
ero3y ago
A harvester should be assigned the task of harvesting a number of trees
BigggMoustache
BigggMoustacheOP3y ago
NICE. getting pinned I'm looking at what Ero shared I've only used generics a couple times. This is super cool. What is being extended here though? Or were you just saying extensionmethods are based af? lol
ero
ero3y ago
Tree in TreeFarm
Pobiega
Pobiega3y ago
extension methods are just static methods that "pretend" to be part of something else so intead of doing TreeFarm.AddNotifier(tree, ...) you can call tree.AddNotifier(...) its the exact same thing
BigggMoustache
BigggMoustacheOP3y ago
Oh yeah okay
Pobiega
Pobiega3y ago
they are very common in modern C#
BigggMoustache
BigggMoustacheOP3y ago
yeah everything in there is new to me so it's more than a bit confusing xD
ero
ero3y ago
I guess they don't need to be extensions
BigggMoustache
BigggMoustacheOP3y ago
Generics, Events, and ExtensionMethods are things I've only done a couple times
ero
ero3y ago
Could just be part of tree
BigggMoustache
BigggMoustacheOP3y ago
I'm gonna copy it and step through it
Pobiega
Pobiega3y ago
well you don't need to worry about this right now
BigggMoustache
BigggMoustacheOP3y ago
oh well okay hahahaha
Pobiega
Pobiega3y ago
your example is a bit contrived, and very simple
BigggMoustache
BigggMoustacheOP3y ago
ya
ero
ero3y ago
You probably can't copy it anyway
BigggMoustache
BigggMoustacheOP3y ago
Oh 😭 lol
ero
ero3y ago
Unless you have a preview version of .net 7 downloaded and bs 2022 preview installed vs
BigggMoustache
BigggMoustacheOP3y ago
That's a no lol
ero
ero3y ago
Then you'll have to wait until November lol
BigggMoustache
BigggMoustacheOP3y ago
So what you folks are telling me is I did an okay job with this exercise and to move onto the next one? xD
Pobiega
Pobiega3y ago
I refrain from commenting 😛
BigggMoustache
BigggMoustacheOP3y ago
👀
ero
ero3y ago
Eh, it works, right?
BigggMoustache
BigggMoustacheOP3y ago
lollllllllll
ero
ero3y ago
It's not something you would ever design in a real app
Pobiega
Pobiega3y ago
Like, we don't know the exercise, only your code and as said, its a contrived example that is too simple to be realistic
ero
ero3y ago
I don't think there was a task
BigggMoustache
BigggMoustacheOP3y ago
Objectives:
• Make a new project that includes the above code.
• Add a Ripened event to the CharberryTree class that is raised when the tree ripens.
• Make a Notifier class that knows about the tree (Hint: perhaps pass it in as a constructor
parameter) and subscribes to its Ripened event. Attach a handler that displays something like “A
charberry fruit has ripened!” to the console window.
• Make a Harvester class that knows about the tree (Hint: like the notifier, this could be passed as
a constructor parameter) and subscribes to its Ripened event. Attach a handler that sets the tree’s
Ripe property back to false.
• Update your main method to create a tree, notifier, and harvester, and get them to work together
to grow, notify, and harvest forever
Objectives:
• Make a new project that includes the above code.
• Add a Ripened event to the CharberryTree class that is raised when the tree ripens.
• Make a Notifier class that knows about the tree (Hint: perhaps pass it in as a constructor
parameter) and subscribes to its Ripened event. Attach a handler that displays something like “A
charberry fruit has ripened!” to the console window.
• Make a Harvester class that knows about the tree (Hint: like the notifier, this could be passed as
a constructor parameter) and subscribes to its Ripened event. Attach a handler that sets the tree’s
Ripe property back to false.
• Update your main method to create a tree, notifier, and harvester, and get them to work together
to grow, notify, and harvest forever
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();
public class CharberryTree
{
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
}
}
}
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();
public class CharberryTree
{
private Random _random = new Random();
public bool Ripe { get; set; }
public void MaybeGrow()
{
// Only a tiny chance of ripening each time, but we try a lot!
if (_random.NextDouble() < 0.00000001 && !Ripe)
{
Ripe = true;
}
}
}
Pobiega
Pobiega3y ago
alright given that description, yeah you did alright
BigggMoustache
BigggMoustacheOP3y ago
NICE.
ero
ero3y ago
Yeah that's fine
Pobiega
Pobiega3y ago
I'd remove the Tree prop from the harvester/notifier but other than that, 👍
BigggMoustache
BigggMoustacheOP3y ago
heck yes
ero
ero3y ago
If anything make it a private field, but if it's not used, remove it completely
BigggMoustache
BigggMoustacheOP3y ago
ya it's not used now I had confusion about sender before which is why I had it probably still have confusion about sender but that's okay lol ❤️ thanks again Ero and Pobiega. Maybbe someday your discord handles will be references in my resume. xD
Pobiega
Pobiega3y ago
wtf :d don't
ero
ero3y ago
And isn't that truly why we're here
Pobiega
Pobiega3y ago
thats not what a reference is, lol
ero
ero3y ago
*referenced
BigggMoustache
BigggMoustacheOP3y ago
"Give three references of people you know who can attest the quality of your work" Uhhh.. @Pobiega#2671, uhhh...
Pobiega
Pobiega3y ago
yeah, nah
BigggMoustache
BigggMoustacheOP3y ago
lmao I know I know I kid.
Pobiega
Pobiega3y ago
references on CVs need to be actual people you've worked with.
BigggMoustache
BigggMoustacheOP3y ago
I'm just saying you folks are a large part of what's going to get me through this. ❤️
ero
ero3y ago
Hey I'm as real as any other person thank you
BigggMoustache
BigggMoustacheOP3y ago
LOL
Pobiega
Pobiega3y ago
I'm just a shared figment of both your imaginations
BigggMoustache
BigggMoustacheOP3y ago
DUDE POBIEGA, I HAVE AN INTERESTING TWIST ON THAT THAT HAS IMPLICATIONS ABOUT THE NATURE OF BEING sorry, i dig philosophy though To Hegel, 'being' is the product of a social function, that is 'consciousness reflecting on consciousness' the object, that is subject, become subject of the subject.
Pobiega
Pobiega3y ago
/close?
BigggMoustache
BigggMoustacheOP3y ago
LOL okay fine.
Pobiega
Pobiega3y ago
:d
ero
ero3y ago
I was about to send that lmfao
BigggMoustache
BigggMoustacheOP3y ago
haha ty again.
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server