对非共享成员的引用需要对象引用

时间:2011-05-03 18:54:45

标签: vb.net visual-studio-2008

当我运行一个用VB.NET编写的ASPX页面时,我现在收到了上述错误。所以我尝试了以下解决方案: http://msdn.microsoft.com/en-us/library/zwwhc0d0(v=vs.80).aspx

以上链接似乎很有希望,因为它似乎完全描述了我的问题。但是,我从这个解决方案中得到以下错误:

  

编译器错误消息:BC30456:   'GlobalF2'不是其成员   'GlobalFunctions'第88行:
  DSProductData =   GlobalFunctions.GlobalF2.ComplaintTrendingDrillDown3p(FirstMonthDate,   LastMonthDate,TheLevel,ProductGroup,   TheCategory,ListNumber)

这是我修改后的源代码导致此错误,但基于Mike Smith的解决方案:

Namespace GlobalFunctions
    Public Class GlobalF
        Public Function ComplaintTrendingDrillDown3p(ByVal FirstMonth As DateTime, ByVal LastMonth As DateTime, ByVal rowLevel As Integer, ByVal productGroup As String, ByVal category As String, ByVal ListNumber As String) As DataSet
            Dim DSPageData As New System.Data.DataSet
            Dim param(5) As SqlClient.SqlParameter

            param(0) = New SqlParameter("@FirstMonthDate", SqlDbType.DateTime)
            param(0).Value = FirstMonth
            param(1) = New SqlParameter("@LastMonthDate", SqlDbType.DateTime)
            param(1).Value = LastMonth
            param(2) = New SqlParameter("@TheLevel", SqlDbType.Int)
            param(2).Value = rowLevel
            param(3) = New SqlParameter("@ProductGroup", SqlDbType.Varchar)
            param(3).Value = productGroup
            param(4) = New SqlParameter("@TheCategory", SqlDbType.Varchar)
            param(4).Value = category
            param(5) = New SqlParameter("@ListNumber", SqlDbType.Varchar)
            param(5).Value = ListNumber

            ''# A Using block will ensure the .Dispose() method is called for these variables, even if an exception is thrown 
            ''# This is IMPORTANT - not disposing your connections properly can result in an unrespsonsive database 
            Using conn As New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString")), _
           cmd As New SQLCommand("ComplaintTrendingDrillDown3p", conn), _
            da As New SQLDataAdapter(cmd)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddRange(param)

                da.Fill(DSPageData)
            End Using

            Return DSPageData
        End Function
    End Class

    Public Class CallingClass
        Dim GlobalF2 As New GlobalF
        Public Function ComplaintTrendingDrillDown3p(ByVal FirstMonth As DateTime, ByVal LastMonth As DateTime, ByVal rowLevel As Integer, ByVal productGroup As String, ByVal category As String, ByVal ListNumber As String) As DataSet
            Dim DSPageData As New System.Data.DataSet
            Dim param(5) As SqlClient.SqlParameter

            param(0) = New SqlParameter("@FirstMonthDate", SqlDbType.DateTime)
            param(0).Value = FirstMonth
            param(1) = New SqlParameter("@LastMonthDate", SqlDbType.DateTime)
            param(1).Value = LastMonth
            param(2) = New SqlParameter("@TheLevel", SqlDbType.Int)
            param(2).Value = rowLevel
            param(3) = New SqlParameter("@ProductGroup", SqlDbType.Varchar)
            param(3).Value = productGroup
            param(4) = New SqlParameter("@TheCategory", SqlDbType.Varchar)
            param(4).Value = category
            param(5) = New SqlParameter("@ListNumber", SqlDbType.Varchar)
            param(5).Value = ListNumber

            ''# A Using block will ensure the .Dispose() method is called for these variables, even if an exception is thrown 
            ''# This is IMPORTANT - not disposing your connections properly can result in an unrespsonsive database 
            Using conn As New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString")), _
           cmd As New SQLCommand("ComplaintTrendingDrillDown3p", conn), _
            da As New SQLDataAdapter(cmd)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddRange(param)

                da.Fill(DSPageData)
            End Using

            Return DSPageData
        End Function
    End Class

End Namespace

我认为Mike Smith对于不使用Shared是正确的,因为我认为这导致了这个问题。但是,我是VB.NET的新手,我不知道如何将实例声明为对象变量,然后通过变量名称引用此实例。你能帮忙吗?

好的,你的解决方案对我来说非常好。我想确保我正确实现它。你能比较我的吗?

现在我得到了我最初的错误......也许它覆盖了表格中的数据?

            Dim gf As New GlobalFunctions.CallingClass
            DSProductData = gf.GlobalF2.ComplaintTrendingDrillDown3p(FirstMonthDate, LastMonthDate, TheLevel, ProductGroup, TheCategory, ListNumber)
...
    Public Class CallingClass
        Public GlobalF2 As New GlobalF
        'Public Function CallingClass(ByVal FirstMonth As DateTime, ByVal LastMonth As DateTime, ByVal rowLevel As Integer, ByVal productGroup As String, ByVal category As String, ByVal ListNumber As String)
        '    Dim cc_new As New CallingClass()
        'End Function

        Public Function ComplaintTrendingDrillDown3p(ByVal FirstMonth As DateTime, ByVal LastMonth As DateTime, ByVal rowLevel As Integer, ByVal productGroup As String, ByVal category As String, ByVal ListNumber As String) As DataSet
            Dim DSPageData As New System.Data.DataSet
            Dim param(5) As SqlClient.SqlParameter

            param(0) = New SqlParameter("@FirstMonthDate", SqlDbType.DateTime)
            param(0).Value = FirstMonth
            param(1) = New SqlParameter("@LastMonthDate", SqlDbType.DateTime)
            param(1).Value = LastMonth
            param(2) = New SqlParameter("@TheLevel", SqlDbType.Int)
            param(2).Value = rowLevel
            param(3) = New SqlParameter("@ProductGroup", SqlDbType.Varchar)
            param(3).Value = productGroup
            param(4) = New SqlParameter("@TheCategory", SqlDbType.Varchar)
            param(4).Value = category
            param(5) = New SqlParameter("@ListNumber", SqlDbType.Varchar)
            param(5).Value = ListNumber

            ''# A Using block will ensure the .Dispose() method is called for these variables, even if an exception is thrown 
            ''# This is IMPORTANT - not disposing your connections properly can result in an unrespsonsive database 
            Using conn As New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString")), _
           cmd As New SQLCommand("ComplaintTrendingDrillDown3p", conn), _
            da As New SQLDataAdapter(cmd)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddRange(param)

                da.Fill(DSPageData)
            End Using

            Return DSPageData
        End Function
    End Class

错误:

  

System.ArgumentException:Column   'QXP_SHORT_DESC'不属于   表格表。

违规行:

  

如果pException(“QXP_SHORT_DESC”)=   TheCategory然后

1 个答案:

答案 0 :(得分:1)

您不能在可以使用的方法之外调暗类的实例

public GlobalF2 As New GlobalF

修改


我不确定你究竟在做什么,只是把所有无关的代码都拉出来。

班级档案

Namespace GlobalFunctions

    Public Class GlobalF
        Public Sub DoSomthing()
            Console.WriteLine("hi")
        End Sub

    End Class

    Public Class CallingClass
        Public GlobalF2 As New GlobalF
        Public x As Int16 = 3
    End Class
End Namespace

主文件

Imports System.IO
Module Module1


    Public Sub Main()
        Dim gf As New GlobalFunctions.CallingClass
        gf.GlobalF2.DoSomthing()
    End Sub

End Module