Visual Studio - VB - Windows 窗体项目 - 未声明“变量”。可能无法访问

Posted

技术标签:

【中文标题】Visual Studio - VB - Windows 窗体项目 - 未声明“变量”。可能无法访问【英文标题】:Visual Studio - VB - Windows forms project - 'variable' is not declared. It may be inaccessible 【发布时间】:2015-04-13 16:13:51 【问题描述】:

我正在尝试为 HERE 找到的简单 Lync“机器人”编译此代码。

我正在使用 Visual Studio 2013 并安装了 Lync 2010 SDK(和 Lync 2010 客户端)。我知道该 SDK 所需的 DLL 已正确导入,因为我已在 PowerShell 项目的其他地方成功使用它。

在尝试构建解决方案时,我遇到了与该网站上的第一个评论者遇到的相同问题,但没有得到解决方案。我在“扩展”下添加了对以“Microsoft.Lync”开头的任何内容的引用。 (其中有 4 个)。我的具体错误:

------ 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 ==========

还有我的具体代码:

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

非常感谢任何帮助。谢谢。

【问题讨论】:

这个错误非常简单。这些错误与您的 dll 无关。您尚未声明这些变量。 我不明白那个网站上的一些人(包括它的作者)是如何正确编译这段代码的,而我和另一个人却没有。 声明一个变量是这样完成的 - Dim aVariable As Object 所以如果你不声明一个对象,它将因为你的错误状态而无法访问。请获取一本初学者书籍来学习基础知识,不要仅仅复制和粘贴从网络上找到但不知道如何使用的代码。 你确定吗?对于LycConversation,如何声明?它被用作似乎特定于 Lync SDK 的函数的参数。 【参考方案1】:

这是原始代码中的错误。在this site 上,我发现有人出于类似目的使用Lyc 变量。我将Imports 有效负载复制到我的函数中,如下所示:

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

另一部分是错字:根据 Form1 类的第四条语句,LycConversation 应该是 _LycConversation

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

我无法测试代码是否按预期方式工作,但它现在可以编译并执行。

【讨论】:

以上是关于Visual Studio - VB - Windows 窗体项目 - 未声明“变量”。可能无法访问的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio 2019 缺少 SQL CLR VB

从 Visual Studio 2010 (VB.NET) 读取访问查询

Visual Studio VB 项目配置转换不显示

Visual Studio / VB.Net 2008 IntelliSense 奇怪行为

VB.NET - Visual Studio 2003 的“继续”的替代方案

从 Visual Studio 2012 调试 VB6 代码