armani
armani

Reputation: 123

Visual Studio - VB - Windows forms project - 'variable' is not declared. It may be inaccessible

I'm trying to compile this code for a simple Lync "bot" as found HERE.

I'm using Visual Studio 2013 and have the Lync 2010 SDK (and Lync 2010 client) installed. I know the required DLL from that SDK is imported correctly as I have used it elsewhere in a PowerShell project successfully.

When trying to build the solution, I get the same problem that the first commenter on that site got but didn't get a resolution to. I have added references to anything under "Extensions" that started with "Microsoft.Lync." (there are 4 of them). My specific errors:

------ Build started: Project: WindowsApplication1, Configuration: Debug Any CPU ------
C:\Users\MyUser\WindowsApplication1\WindowsApplication1\Form1.vb(31) : error BC30451: '_InitializeFlag' is not declared. It may be inaccessible due to its protection level.
C:\Users\MyUser\WindowsApplication1\WindowsApplication1\Form1.vb(46) : error BC30451: 'LycConversation' is not declared. It may be inaccessible due to its protection level.
C:\Users\MyUser\WindowsApplication1\WindowsApplication1\Form1.vb(46) : error BC30451: 'Lyc' is not declared. It may be inaccessible due to its protection level.
C:\Users\MyUser\WindowsApplication1\WindowsApplication1\Form1.vb(47) : error BC30581: 'AddressOf' expression cannot be converted to 'Object' because 'Object' is not a delegate type.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

And my specific code:

Imports Microsoft.Lync.Model
Imports Microsoft.Lync.Model.Conversation
Public Class Form1
    Public WithEvents _Client As LyncClient
    Public WithEvents _ConversationMgr As Microsoft.Lync.Model.Conversation.ConversationManager
    Private WithEvents _LocalIMModality As InstantMessageModality
    Public _LycConversation As Microsoft.Lync.Model.Conversation.Conversation

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            _Client = LyncClient.GetClient()
            _ConversationMgr = _Client.ConversationManager
            Select Case _Client.State
                Case ClientState.Uninitialized
                    _Client.BeginInitialize(AddressOf InitializeCallback, Nothing)
                Case ClientState.SignedIn

                Case ClientState.SignedOut
                    _Client.EndSignIn(_Client.BeginSignIn(Nothing, Nothing, Nothing, Nothing, Nothing))
            End Select
        Catch ex As AlreadyInitializedException
            MessageBox.Show("Another process has initialized Lync")
        Catch ex As Exception

        End Try

    End Sub

    Private Sub InitializeCallback(ByVal ar As IAsyncResult)
        _Client.EndInitialize(ar)
        _InitializeFlag = True
        _Client.EndSignIn(_Client.BeginSignIn(Nothing, Nothing, Nothing, Nothing, Nothing))
    End Sub

    Private Sub _ConversationMgr_ConversationAdded(ByVal sender As Object, ByVal e As Microsoft.Lync.Model.Conversation.ConversationManagerEventArgs) Handles _ConversationMgr.ConversationAdded
        _LocalIMModality = TryCast(e.conversation.Participants(1).Modalities(ModalityTypes.InstantMessage), InstantMessageModality)

    End Sub

    Private Sub _LocalIMModality_InstantMessageReceived(ByVal sender As Object, ByVal e As Microsoft.Lync.Model.Conversation.MessageSentEventArgs) Handles _LocalIMModality.InstantMessageReceived
        Dim strRec As String
        strRec = e.Text.Replace(vbCr, "").Replace(vbLf, "").Replace("'", "''")
    End Sub

    Public Sub SendIM(ByVal strMessage As String)
        Dim modal = DirectCast(LycConversation.Modalities(Lyc.ModalityTypes.InstantMessage), InstantMessageModality)
        modal.BeginSendMessage(strMessage, AddressOf SendMessageCallback, Nothing)
    End Sub

    Private Sub SendMessageCallback(ByVal r As IAsyncResult)

    End Sub

End Class

Any help is greatly appreciated. Thanks.

Upvotes: 0

Views: 1693

Answers (1)

armani
armani

Reputation: 123

This was an error in the original code. On this site I found someone using the Lyc variable for a similar purpose. I copied the Imports payload into my function like so:

Public Sub SendIM(ByVal strMessage As String)
        Dim modal = DirectCast(_LycConversation.Modalities(Microsoft.Lync.Model.Conversation.ModalityTypes.InstantMessage), InstantMessageModality)
        modal.BeginSendMessage(strMessage, AddressOf SendMessageCallback, Nothing)
    End Sub

The other part was a typo: LycConversation should have been _LycConversation per the fourth statement of the Form1 Class:

Public _LycConversation As Microsoft.Lync.Model.Conversation.Conversation

I am unable to test if the code works the way it is supposed to, but it compiles and executes now.

Upvotes: 2

Related Questions