Accessing values from partial classes
Please help! I'm still learning so this might be completely wrong. I have built a multi-user web application for internal purposes. I have split my code up to make it more manageable and have run into some problems. I suspect that I have done something very silly!
I have a class in a separate file (quoteStatus.vb) in App_Code here it is:
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
I use this to fire the code from another file:
quoteStatusHelper.GetQuoteStatus(QuoteID)
Then grab the result with
Dim returnedStatus = quoteStatusHelper.QuoteStatus
This seems really wrong, as the value seems to be held in the partial class after the first run. So if I have two users accessing the class the public shared dim is changed for both users! There must be a better way to do this. I have looked everywhere and I'm more confused now than wh开发者_JAVA技巧en I started off...
How can I make QuoteStatus in the partial class unique to every user and access it from my code. I cant figure it out :( Please help
Many thanks in advance.
The problem is that your QuoteStatus
variable is shared/static. You don't need to do that here. Declaring it as Public will make it visible to other classes. Remove Shared
from the method and the QuoteStaus
variable, and all should be well.
Once you remove Shared
it will be an instance class, which means that you'll need to initialize it in your code:
Dim statusHelper As New quoteStatusHelper()
statusHelper.GetQuoteStatus("QuoteID")
EDIT
Taking a closer look at your code, it looks like you could make GetQuoteStatus
a function that returns the QuoteStatus
as a string, as @Claudio Redi suggested.
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
By making GetQuoteStatus
a function, you'll be able to get the value like this:
Dim statusHelper As New quoteStatusHelper()
Dim quoteStatus As String = statusHelper.GetQuoteStatus("QuoteID")
An option would be using a local variable on your method for quoteStatus
and using it as return value of GetQuoteStatus
. So you wouldn't use anymore the shared member QuoteStatus
EDIT
Added a function example. You should remove your shared member QuoteStatus
from your class. I don't use VB on my day to day work so I hope I didn't make any stupid syntax mistake
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
精彩评论