Emanuel
Emanuel

Reputation: 6972

How to use Delegate in C# for Dictionary<int, List<string>>

The code:

[1]

private delegate void ThreadStatusCallback(ReceiveMessageAction action, 
      Dictionary<int, List<string>> message);

[2]

Dictionary<int, List<string>> messagesForNotification = 
      new Dictionary<int, List<string>>();

[3]

Invoke(new ThreadStatusCallback(ReceivesMessagesStatus), 
     ReceiveMessageAction.Notification , messagesForNotification );

[4]

private void ReceivesMessagesStatus(ReceiveMessageAction action, object value)
{
    ...
}

How can I send the two variable of type ReceiveMessageAction respectively Dictionary<int, List<string>> to the ReceivesMessagesStatus method.

Thanks.


I have a function that is executed in a thread. Here I create an object of type Dictionary <int, List <string>> and using a delegation I whant to use this object in my form.

A part of the code is above: [1] Decraration of the delegated [2] The object [3] The Call Of the delegate [3] the function Where the object Need To Be send

Upvotes: 0

Views: 828

Answers (3)

Hans Passant
Hans Passant

Reputation: 941635

You can do it type-safe without casting by using a lambda:

  Invoke(new MethodInvoker(
    () => ReceivesMessagesStatus(ReceiveMessageAction.Notification, messagesForNotification)
  ));

Upvotes: 2

Emanuel
Emanuel

Reputation: 6972

Solution:

[1]

Invoke(new ThreadStatusCallback(ReceivesMessagesStatus), new object[] { ReceiveMessageAction.Notification, messagesForNotification } );

[2]

private void ReceivesMessagesStatus(ReceiveMessageAction action, Dictionary<int, List<string>> value)
{
    ...
}

Upvotes: 1

James
James

Reputation: 82096

Have you tried casting value as a Dictionary? e.g.

private void ReceiveMessagesStatus(ReceieveMessageAction action, object value)
{
    var myDictionary = (Dictionary<int, List<string>>)value;
    ...
}

Upvotes: 1

Related Questions