×
Login Register an account
Top Submissions Explore Upgoat Search Random Subverse Random Post Colorize! Site Rules Donate
2

starting new job soon, coming back up to speed in C#. thought I would run through a producer/consumer pattern using delegates to oil the rusty parts of my brain :-)

submitted by AugustineOfHippo2 to programming 3.0 yearsMay 10, 2022 16:25:33 ago (+3/-1)     (programming)

using System;

public delegate void del_call(int prntVal);//defining the delegate

public class ProduceRandom {
del_call prDel;//local instance of delegate
public ProduceRandom(del_call pp) {
prDel = pp;//constructor to populate the local delegate
}
public void makeRandom() {
Random rnd = new Random();
for(int i = 0; i<10; i++ ) {
prDel( rnd.Next(0,10) );//generate 10 random numbers (from 0 to 10)
}
}
}//end class ProduceRandom
public class ConsumeRandom {
public void printRandom(int rVal) {
Console.WriteLine("Random = " + rVal); //delegate target, just print to console
}
}//end class ConsumeRandom
public class xyz{
public static void Main(string[] args) {
ConsumeRandom cr1 = new ConsumeRandom();//caveat - must instantiate consumer prior to producer
ProduceRandom pr1 = new ProduceRandom(cr1.printRandom);
pr1.makeRandom();
}//end Main
}//end class


5 comments block


[ - ] deleted 1 point 3.0 yearsMay 10, 2022 16:37:50 ago (+1/-0)

deleted

[ - ] AugustineOfHippo2 [op] 0 points 3.0 yearsMay 10, 2022 17:01:16 ago (+0/-0)

eh, well, not really? I wasn't thinking of that, more of a loosely coupled system that wouldn't need direct object references. Just looking to refamiliarize myself with the language by rehashing an old programming problem.

[ - ] chrimony 0 points 3.0 yearsMay 10, 2022 17:33:01 ago (+0/-0)

Wow, do you really write shit comments like that?

public delegate void del_call(int prntVal);//defining the delegate

Yes, I figured that out by the PUBLIC DELEGATE. But thanks for telling me again, I might have forgotten by the time I reached the end of the line.

}//end class ConsumeRandom

Thanks again, I guess I'm too stupid to look up four lines and see that.

Anyways, congrats on the new job.

[ - ] AugustineOfHippo2 [op] 1 point 3.0 yearsMay 10, 2022 20:54:13 ago (+1/-0)

yes, maybe excessive on the //defining the delegate :-)
usually classes are more than 4 lines long and //end class really helps me when the top of the class spans beyond the visible screen.

[ - ] WillisJaxson 0 points 3.0 yearsMay 10, 2022 18:20:57 ago (+0/-0)

Meh. It just reminded me of professors insisting on an over-use of comments for assignments back in the day. Since he's trying to get back up to speed, I imagine the comments are more for his benefit than anything else. I wouldn't hold it against him.

[ - ] diggernicks 0 points 3.0 yearsMay 10, 2022 20:01:08 ago (+0/-0)

You surely live in a liberal hell hole

Off yourself