简体   繁体   中英

Pass Second Parameter Into Function

I am new to c# and have inherited code that's not well commented and the original developer is no longer around.

I need to pass a second parameter in to the function below:

Code Ref=1

public class SnmpTrapListener : IDisposable
{
 ....
 private KeyValuePair<string, object> MapVariableValues(Variable variable)
        {
            ....
            return new KeyValuePair(...);
        }    
...
}

The only places the above function is called/referenced is in the same class and one other class.

The reference from the same class is...

Code Ref=2

var observable = trapV1MessageHandler.ToObservableGenericTrapMessage(_Logger, MapVariableValues)
            .Merge(trapV2MessageHandler.ToObservableGenericTrapMessage(_Logger, MapVariableValues));

The call from the other class is...

Code Ref=3

public static IObservable<GenericTrapMessage> ToObservableGenericTrapMessage(this TrapV1MessageHandler handler, Logger Logger, Func<Variable, KeyValuePair<string, object>> variableMapping)
    {
        return Observable.FromEventPattern<TrapV1MessageReceivedEventArgs>(
          h => handler.MessageReceived += h,
          h => handler.MessageReceived -= h)
          .Select(e => 
          {
            return new GenericTrapMessage
            {
                Timestamp = e.EventArgs.TrapV1Message.TimeStamp,
                Sender = e.EventArgs.Sender.ToString() + ":" + System.Environment.MachineName.ToString(),
                Type = e.EventArgs.TrapV1Message.Enterprise.ToString(),
                Variables = e.EventArgs.TrapV1Message.Variables().Select(variableMapping).ToList()
            }
         });
     ...
    }

I want to add a second string 'OID' parameter as shown below... Code Ref=4

private KeyValuePair<string, object> MapVariableValues(Variable variable, String OID)
            {
                ....
                return new KeyValuePair(...);
            }    
    ...
    }

The part I need help on is how to change the code shown in "Code Ref=2" and "Code Ref=3" above to handle the second string parameter?

My thoughts are I just need to change one line under "Code Ref=3" passing in the new String parameter but I don't know how to do this - the line I think I need to change:

Variables = e.EventArgs.TrapV1Message.Variables().Select(variableMapping).ToList()

You can just add a second parameter to your function as usual:

private KeyValuePair<string, object> MapVariableValues(Variable variable, String OID)
{
}

Then, you need to change the parameter of the function in code sample 3 in order to accept functions with 2 input parameters, like this:

public static IObservable<GenericTrapMessage> ToObservableGenericTrapMessage(this TrapV1MessageHandler handler, Logger Logger, Func<Variable, String, KeyValuePair<string, object>> variableMapping)
{
}

For the selection you can use a slightly different syntax, so that you can provide a additional parameter:

Variables = e.EventArgs.TrapV1Message.Variables().Select(v => variableMapping(v, "yourStringParameter").ToList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM