Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can i use Table Driven methods with delegate and Dictionary?
    text
    copied!<p>Hi i have been exhausted too many if comparization. and switch case methods. i have been read below issue: </p> <p><a href="https://stackoverflow.com/questions/214017/best-table-enum-driven-method-calling-system">Best table / enum driven method calling system</a></p> <p>this usage is good. Look Vivek's responses. </p> <p>My usage Below but i can not this. </p> <pre><code> namespace DelegateKavrami { class Program { static void Main(string[] args) { new MyTest().Run(); Console.Read(); } } public class MyTest { public void Run() { MyMessageProcessor myProcessor = new MyMessageProcessor(); myProcessor.Register(0, Sms); myProcessor.Register(1, Mms); myProcessor.ProcessMessage(1, Message.Sms); } public void Sms(Message message) { Console.WriteLine("Sms olarak gönderilen mesaj"); } void Mms(Message message) { Console.WriteLine("Mms olarak gönderilen mesaj:"); } } public class MyMessageProcessor { public delegate void ProcessMessageDelegate(Message message); Dictionary methods; public void Register(int messageType, ProcessMessageDelegate processMessage) { methods[messageType] = processMessage; } public void ProcessMessage(int messageType, Message message) { if (methods.ContainsKey(messageType)) { methods[messageType](message); } } } public enum Message { Sms,Mms } } </code></pre> <p>But my Dream</p> <h3> myProcessor.ProcessMessage(1)</h3> <p>How can i use this? Error after above code runnig: Object reference not set to an instance of an object. ON methods[messageType] = processMessage;</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload