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
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.
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.
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.
[ + ] Deleted
[ - ] deleted 1 point 3.0 yearsMay 10, 2022 16:37:50 ago (+1/-0)
[ + ] AugustineOfHippo2
[ - ] AugustineOfHippo2 [op] 0 points 3.0 yearsMay 10, 2022 17:01:16 ago (+0/-0)
[ + ] diggernicks
[ - ] diggernicks 0 points 3.0 yearsMay 10, 2022 20:01:08 ago (+0/-0)
Off yourself
[ + ] chrimony
[ - ] chrimony 0 points 3.0 yearsMay 10, 2022 17:33:01 ago (+0/-0)
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
[ - ] AugustineOfHippo2 [op] 1 point 3.0 yearsMay 10, 2022 20:54:13 ago (+1/-0)
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
[ - ] WillisJaxson 0 points 3.0 yearsMay 10, 2022 18:20:57 ago (+0/-0)