0

Noob here in C#, so I'm having an issue trying to call a function in C# with system event args. I know the issue isn't with system event args, but what is the correct way I should call this type of function?

Error1: An object reference is required for the non-static field, method, or property 'ConsoleApplication3.Program.Reject_call(object, System.EventArgs)'

Error2: An object reference is required for the non-static field, method, or property 'ConsoleApplication3.Program.Accept_call(object, System.EventArgs)'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Lync.Model;
using Microsoft.Lync.Model.Conversation;
using Microsoft.Lync.Model.Conversation.AudioVideo;

namespace ConsoleApplication3
{
    class Program
    {
    //holds the Lync client instance
    private static LyncClient _client;
    //holds the reference to the conversation associated with this window
    private Conversation conversation;
    //self participant's AvModality
    private AVModality avModality;

    private static void Main(string[] args)
    {
        //Obtains the lync client instance
        _client = LyncClient.GetClient();
        consoleInputReadRoutine();
        _client.ConversationManager.ConversationAdded += ConversationManager_ConversationAdded;
        _client.ConversationManager.ConversationRemoved += ConversationManager_ConversationRemoved;

        Console.ReadLine();
    }

    static void ConversationManager_ConversationAdded(object sender, ConversationManagerEventArgs e)
    {
        e.Conversation.Modalities[ModalityTypes.AudioVideo].ModalityStateChanged += Program_ModalityStateChanged;
    }

    static void Program_ModalityStateChanged(object sender, ModalityStateChangedEventArgs e)
    {
        Console.WriteLine("Modality state changed " + String.Format("{0} => {1}", e.OldState, e.NewState));
    }

    static void consoleInputReadRoutine()
    {

        bool done = false;
        while(!done){

            string input = Console.ReadLine(); // Get string from user
            Console.WriteLine("user input {0}\n", input);

            // Set exit condition.
            if(String.Compare(input, "exit") == 0){
                done = true;
            }
            else if (String.Compare(input, "reject") == 0)
            {
                //Do a reject call.
                Reject_call(null, new EventArgs());
            }
            else if(String.Compare(input, "answer") == 0)
            {
                //Do a answer call.
                Accept_call(null, new EventArgs());
            }
        }
    }

    // Accepts an incoming call: AvModality.Accept()
    private void Accept_call(object sender, EventArgs e)
    {
        //accepts an incoming invite (syncronous operation)
        avModality.Accept();
    }

    // Rejects an incoming call: AvModality.Reject()
    private void Reject_call(object sender, EventArgs e)
    {
        //rejects an incoming invite (which will disconnect the call)
        //the ModalityDisconnectReason may be used to specify a reason to the caller side
        //the reason may be shown on the Lync client conversation window
        avModality.Reject(ModalityDisconnectReason.Decline);
    }

    static void ConversationManager_ConversationRemoved(object sender, ConversationManagerEventArgs e)
    {
        //Exit out of program if you wish after conversation is removed.
    }
    }
}
Alex K.
  • 171,639
  • 30
  • 264
  • 288
deusmaximus
  • 73
  • 1
  • 8
  • If you want to call/access a function/field/property without an instance of the class, then the function must be static. – mason Oct 28 '14 at 17:01

2 Answers2

3

You are calling a instance method (private void Accept_call) from a static one (static void consoleInputReadRoutine).

Given that this is a console app, you likely just want to make Accept_call static. In general, if this were a normal object, you would want to make everything non-static.

private static void Accept_call(object sender, EventArgs e)

private static void Reject_call(object sender, EventArgs e)

Class members used by these functions would also need to be static. avModality in particular.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
1

You need to either create an instance of your Program class via the new operator or mark Reject_call and Accept_call as static.

Kapol
  • 6,383
  • 3
  • 21
  • 46