mardi 3 décembre 2019

How to call the result of a function from a class in Vb.Net?

*Hello,

I would like to know how to call the result of a function that is in another class, in Visual Basic .Net, to then use that result for comparison in an if?

I want to call the result of the SubtractBalance function, specifically, if there is in it, I would like to have it in the class from where I want to call it(FRM_CashWithdrawal2).

I am simulating an ATM. Form FRM_CashWithdrawal2 calls two forms. One is for the impression of the receipt after the desired amount to be withdrawn is entered and the other is for the same procedure but without receipt in order to withdraw the money at the end. Otherwise, the program launches the Message: "Insufficient Funds." When I call the function in this way: oConnection.SubtractBalance (FRM_InsertCardID.TXB_CardID.Text), the SubtractBalance function is evaluated, when the fund is insufficient, it throws the message but also calls the previously mentioned forms. I wish that when you send the message: "Insufficient Funds", do not call those forms.

Thanks in advance.*

Imports System.Data.SqlClient

Public Class FRM_CashWithdrawal2

    Dim oConnection As New clsDBConnection


    Private Sub BTN_Ok_Click(sender As Object, e As EventArgs) Handles BTN_Ok.Click


        If TXB_Amount_to_Withdraw.Text.Length = 0 Then
            MsgBox("You must fill the field.")
            Return
        End If
        If TXB_Amount_to_Withdraw.Text = "0" Then
            MsgBox("Enter the amount to withdraw.")
            Return
        End If
        Dim intAmount As Integer
        If Not Integer.TryParse(TXB_Amount_to_Withdraw.Text, intAmount) Then
            MessageBox.Show("Please enter a value in the field.")
            Return
        End If
        Select Case intAmount
            Case > 10000
                MsgBox("It is only possible to withdraw up to RD $ 10000.00 at ATMs.")
                Return

        End Select

        Dim NumMultiple1 = intAmount Mod 100
        Dim NumMultiple2 = intAmount Mod 200
        Dim NumMultiple3 = intAmount Mod 500
        Dim NumMultiple4 = intAmount Mod 1000
        Dim NumMultiple5 = intAmount Mod 2000


        oConnection.SubtractBalance(FRM_InsertCardID.TXB_CardID.Text)




        If NumMultiple1 = 0 OrElse NumMultiple2 = 0 OrElse NumMultiple3 = 0 OrElse NumMultiple4 = 0 OrElse NumMultiple5 = 0 Then
                Dim Answer = MessageBox.Show("Do you want to print a receipt?", "Print Receipt", MessageBoxButtons.YesNo)
                If Answer = DialogResult.Yes Then

                    FRM_RemoveYourCard.Show()
                    Me.Hide()

                Else

                    FRM_RemoveYourCard1.Show()
                    Me.Hide()

                End If

            Else
                MsgBox("You can only make withdrawals on multiple tickets of RD $ 100.00, RD $ 200.00, RD $ 500.00, RD $ 1000.00 and RD $ 2000.00 pesos.")
            End If


    End Sub
End Class


Public Class clsDBConnection

    Private _strUserDB As String
    Private _strPassDB As String
    Private _strNameDB As String
    Private _strSRVName As String
    Private _strConnection As String

    Private adAdaptator As SqlDataAdapter
    Private tbTable As DataTable
    Private drRegister As SqlDataReader

    Public strComando As String

    Public _Error As Boolean
    Public _Menssage As String


    Public Sub New()

        With ATMApp3.My.Settings
            _strSRVName = .strNameSRV.ToString
            _strNameDB = .strNameDB.ToString
            _strUserDB = .strUserDB
            _strPassDB = .strPassUserDB
        End With

        _strConnection = "Data Source=JOVALLES-PC\SQLSERVEREX;Initial Catalog=" & _strNameDB & ";User ID=" & _strUserDB & ";Password=" & _strPassDB

        Try
            Dim dbConnection As New System.Data.SqlClient.SqlConnection(_strConnection)
            dbConnection.Open()
            MsgBox("CONNECTED")
        Catch ex As Exception
            MsgBox("Error to connect due to: " + ex.ToString)
        End Try

    End Sub


Public Function Modify(ByVal pCard As String, ByVal pBalance As Integer) As String

        Dim Output As String = "It was modified correctly."
        Dim dbConnection As New System.Data.SqlClient.SqlConnection(_strConnection)
        Dim cmd As New SqlClient.SqlCommand("up_modify_balance", dbConnection)
        cmd.CommandType = CommandType.StoredProcedure

        Try

            If dbConnection.State = ConnectionState.Closed Then
                dbConnection.Open()
            End If

            With cmd.Parameters

                .AddWithValue("@Num_Card", pCard)
                .AddWithValue("@Balance", pBalance)

            End With

            cmd.ExecuteNonQuery()

        Catch ex As Exception

            Output = "It was not modified due to:" + ex.ToString
            dbConnection.Close()
        End Try

        Return Output


    End Function


 Public Function SubtractBalance(ByVal pCard As String) As SqlDataReader

        Dim dbConnection As New System.Data.SqlClient.SqlConnection(_strConnection)
        Dim cmd As New SqlClient.SqlCommand("up_consult_balance", dbConnection)
        cmd.CommandType = CommandType.StoredProcedure

        With cmd.Parameters

            .AddWithValue("@Num_Card", pCard)

        End With


        Try

            If dbConnection.State = ConnectionState.Closed Then
                dbConnection.Open()
            End If

            drRegister = cmd.ExecuteReader

            If drRegister.Read Then

                Dim Subtract As Double
                Dim CurrentBalance As Double = CStr(drRegistros("BALANCE_AVAILABLE"))
                Subtract = (BalanceActual - FRM_CashWithdrawal2.TXB_Amount_To_Withdraw.Text)


                If CurrentBalance < FRM_CashWithdrawal2.TXB_Amount_To_Withdraw.Text Then
                      MsgBox("Insufficient funds.")

                Else

                    Modify(FRM_InsertCardID.TXB_CardID.Text, Subtract)

                End If


            Else

                _Error = True
                _Message = "There is not data"
                dbConnection.Close()
                Return Nothing

            End If

        Catch ex As Exception

            MsgBox("It was not modified due to:" + ex.ToString)
            dbConnection.Close()
        End Try

    End Function
End Class

Aucun commentaire:

Enregistrer un commentaire