简体   繁体   中英

Will I Need a Public Shared Variable Here?

I have this sub in one form of my program to calculate and display a total profit from my database:

Public Sub Profit()
    Try
        Using connection As New OleDbConnection(connectionstring)
            connection.Open()

            Dim Command As New OleDbCommand("SELECT SUM(Price) FROM ItemsSold", connection)
            Dim command2 As New OleDbCommand("SELECT SUM(TotalCost) FROM Inventory", connection)
            Dim Turnover As Double = Convert.ToDouble(Command.ExecuteScalar())
            Dim Cost As Double = Convert.ToDouble(command2.ExecuteScalar())
            Dim TotalProfit As Double = Turnover - Cost

            lblProfit.Text = "Profit: £" & TotalProfit
            lblProfit2.Text = "Profit: £" & TotalProfit
            lblProfit3.Text = "Profit: £" & TotalProfit
            lblProfit4.Text = "Profit: £" & TotalProfit

            connection.Close()
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Also, in a different form of the program, I am updating a database table with a value provided by the user. What I want to do, is add this input value to this profit that is displayed in the label in the Profit() sub.

How can I go about doing this? Would I need to somehow make this profit variable a public shared variable to be able to access it in my other form? Is there another way to just increase this value and display the new increased variable?

How does shared variable supposed to help you?

you should to split functions which works with data and which works with form. In other words, when you need display data, you call GetProfit , and when you need to update profit you call SetProfit(ByVal profit As Double) . And then use them in your project.

Based on your code, I suppose it should looks something like that

Public Sub GetProfit()
    Dim profit As Double
    Try
        Using connection As New OleDbConnection(connectionstring)
            connection.Open()

            Dim Command As New OleDbCommand("SELECT SUM(Price) FROM ItemsSold", connection)
            Dim command2 As New OleDbCommand("SELECT SUM(TotalCost) FROM Inventory", connection)
            Dim Turnover As Double = Convert.ToDouble(Command.ExecuteScalar())
            Dim Cost As Double = Convert.ToDouble(command2.ExecuteScalar())
            Dim TotalProfit As Double = Turnover - Cost

            profit = TotalProfit 

            connection.Close()
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    ' Return the result
    return profit
End Sub

And for update

Public Sub SetProfit(ByVal newProfit As Double)
    Try
        Using connection As New OleDbConnection(connectionstring)
            connection.Open()

            ' Commands for updating data (just for example)
            Dim Command As New OleDbCommand("UPDATE ItemsSold SET Price = " & newProfit)

            connection.Close()
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

And then you when you dealing with your form you just do like this

Private Sub Form1_Load()
   Label1.Text = GetProfit()
   Label2.Text = GetProfit()
End Sub

When you need to update

Private Sub Button1_Click()
  SetProfile(GetProfit() + 5)
End

It is not exactly production code, just for an example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM