1

I'm communicating with an instrument asynchronously via tcp (TcpClient). There is a large but finite number of responses (which are converted to strings) that can be returned from the instrument and I would prefer not to have a large section of if-then-else statements. How does one go about making a SortedList where the value points to a member function?

I've tried the following

    private SortedList<string, object> functionList = new SortedList<string, object>();

    private void InitializeList() 
    {
        functionList.Add("Time", ProcessTime(string));  // Invalid expression term 'string'
        functionList.Add("Date", ProcessTime()); // There is no argument given that corresponds 
                                                //  to the required formal parameter 'reply' of  
                                                //  'ProcessTime(string)'
    }

    private void ProcessTime(string reply) 
    {
        // More complex processing here, but you should get the idea.
        Console.WriteLine(reply);
    }

I'm sure this has been asked before but I can't find where.

Part Time User
  • 313
  • 1
  • 2
  • 6
  • If you can have a common signature like : `void ProcessX(string imput))` You can use `Action` as the parameter. The question is why you want to use a sorted list instead of a dictionary. – Eldar May 15 '23 at 18:56

1 Answers1

3

I think Delegates may be what you have in mind: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/

pawooten
  • 140
  • 1
  • 9
  • That was what I was looking for. I finally got it to work with the help of https://stackoverflow.com/questions/3813261/how-to-store-delegates-in-a-list – Part Time User May 15 '23 at 19:39