从部分类中访问值

时间:2011-09-02 14:47:41

标签: asp.net vb.net class

请帮忙!我还在学习,所以这可能是完全错误的。我为内部目的构建了一个多用户Web应用程序。我已将我的代码拆分以使其更易于管理并遇到一些问题。我怀疑我做了一些非常愚蠢的事情!

我在App_Code的单独文件(quoteStatus.vb)中有一个类,它是:

Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data

Public Class quoteStatusHelper

 Public Shared QuoteStatus As String

    Public Shared Sub GetQuoteStatus(ByVal QuoteID As String)

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        Try
            con.ConnectionString = ConfigurationManager.AppSettings("quotingSystemConnectionString")
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "SELECT Status FROM Quote WHERE QuoteID =" & QuoteID & ";"
            Dim lrd As SqlDataReader = cmd.ExecuteReader()

            While lrd.Read()
                QuoteStatus = lrd("Status")
            End While

        Catch ex As Exception

        Finally
            con.Close()
        End Try


    End Sub
    End Class

我用它从另一个文件中激活代码:

quoteStatusHelper.GetQuoteStatus(QuoteID)

然后用

抓取结果
Dim returnedStatus = quoteStatusHelper.QuoteStatus

这似乎是错误的,因为值似乎在第一次运行后保留在分部类中。因此,如果我有两个用户访问该类,则两个用户都会更改公共共享dim!必须有更好的方法来做到这一点。我到处都是,我现在比起初时更加困惑......

如何让分部类中的QuoteStatus对每个用户都是唯一的,并从我的代码中访问它。我无法弄清楚:(请帮助

非常感谢提前。

2 个答案:

答案 0 :(得分:1)

一个选项是在quoteStatus的方法上使用局部变量,并将其用作GetQuoteStatus的返回值。因此,您不再使用共享成员QuoteStatus

修改

添加了一个功能示例。您应该从班级中删除您的共享成员QuoteStatus。我不会在日常工作中使用VB,所以我希望我没有犯任何愚蠢的语法错误

Public Shared Function GetQuoteStatus(ByVal QuoteID As String) as String

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim quoteStatus As String

    Try
        con.ConnectionString = ConfigurationManager.AppSettings("quotingSystemConnectionString")
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "SELECT Status FROM Quote WHERE QuoteID =" & QuoteID & ";"
        Dim lrd As SqlDataReader = cmd.ExecuteReader()

        While lrd.Read()
            quoteStatus = lrd("Status")
        End While

    Catch ex As Exception

    Finally
        con.Close()
    End Try

    return quoteStatus

End Function

答案 1 :(得分:1)

问题是您的QuoteStatus变量是共享/静态的。你不需要在这里做。将其声明为Public将使其对其他类可见。从方法和Shared变量中删除QuoteStaus,一切都应该正常。

删除Shared后,它将成为实例类,这意味着您需要在代码中对其进行初始化:

Dim statusHelper As New quoteStatusHelper()
statusHelper.GetQuoteStatus("QuoteID")

修改

仔细查看您的代码,看起来您可以使GetQuoteStatus函数将QuoteStatus作为字符串返回,就像@Claudio Redi建议的那样。

Public Function GetQuoteStatus(ByVal QuoteID As String) As String 

    Dim quoteStatus As String

    Dim con As New SqlConnection  
    Dim cmd As New SqlCommand  

    Try  
        con.ConnectionString = ConfigurationManager.AppSettings("quotingSystemConnectionString")  
        con.Open()  
        cmd.Connection = con  
        cmd.CommandText = "SELECT Status FROM Quote WHERE QuoteID =" & QuoteID & ";"  
        Dim lrd As SqlDataReader = cmd.ExecuteReader()  

        While lrd.Read()  
            quoteStatus = lrd("Status")  
        End While  

    Catch ex As Exception  

    Finally  
        con.Close()  
    End Try  

    Return quoteStatus

End Function

通过使GetQuoteStatus成为一个函数,您将能够获得如下值:

Dim statusHelper As New quoteStatusHelper()
Dim quoteStatus As String = statusHelper.GetQuoteStatus("QuoteID")