无法在共享上下文中访问非共享功能 [英] Cannot access non-shared function in shared context

查看:69
本文介绍了无法在共享上下文中访问非共享功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从另一个类调用的公共函数。因此,需要共享该功能以便调用它。但是,当我将其共享时,连接字符串将抛出异常无法访问共享上下文中的非共享函数GetConnection()。该功能是...



I have a public function that gets called from another class. As such the function needs to be shared in order for it to be called. However, when I make it shared, the connection string throws the exception Cannot access non-shared function GetConnection() in shared context. The function is ...

Public Shared Function GetUsernames() As Object
     Dim cmdUsernames As SqlCommand = New SqlCommand("crc_GetUsernameForAccountNoChangesMadeReport", CType(GetConnection(), SqlConnection))
     Dim user As String

     cmdUsernames.CommandType = CommandType.StoredProcedure
     With cmdUsernames
         .Connection.Open()
         Dim results As New List(Of Usernames)()
         Using reader As SqlDataReader = .ExecuteReader()
             While reader.Read()
                 user = reader.GetString(0)
                 Dim item As New Usernames() With {.Name = user}
                 results.Add(item)
             End While
             reader.Close()
             .Connection.Close()
             Return results
         End Using
     End With
 End Function







此函数从表单调用以下代码...




This function gets called from a form with the following code...

Private Sub frmAcctNoChgsMadeFilters_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     Dim Users As List(Of Usernames) = GetUsernames()
     cboUsername.DataSource = Users
     cboUsername.ValueMember = "Name"
     cboUsername.DisplayMember = "Name"
 End Sub





如何解决此错误



How can I resolve this error

推荐答案

这是因为您的GetConnection()方法未共享,并且需要包含它的类的实例首先创建。
It's saying that because your GetConnection() method is not shared and requires an instance of the class containing it to be created first.


更改为使用Report Viewer访问返回值的控件。不再需要共享方法。
Changed to using controls on Report Viewer accessing return values. Method no longer needed to be shared.


这篇关于无法在共享上下文中访问非共享功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆