2014-02-15 70 views
0

此業務登錄Layer類:是否可以將類的對象作爲函數參數傳遞?

Public Class BankEntity 

     Private _bankCode As String 
     Private _bankName As String 
     Private _bankAddress As String 

     Public Property bankCode() As String 
      Get 
       Return _bankCode 
      End Get 
      Set(ByVal value As String) 
       _bankCode = value 
      End Set 
     End Property 

     Public Property bankName() As String 
      Get 
       Return _bankName 
      End Get 
      Set(ByVal value As String) 
       _bankName = value 
      End Set 
     End Property 

     Public Property bankAddress() As String 
      Get 
       Return _bankAddress 
      End Get 
      Set(ByVal value As String) 
       _bankAddress = value 
      End Set 
     End Property 

    End Class 

,這是數據訪問層類

Imports System 
    Imports System.Data 
    Imports System.Data.SqlClient 

Public Class SqlHelper 

    Private _connStr As String = "Data Source=NEO-PC\SQLEXPRESS;Initial Catalog=dbBank;Integrated Security=True" 
    Private _conn As SqlConnection 
    Private _cmd As SqlCommand 

    Public Function AddNewBank(ByVal BankCode As String, ByVal BankName As String, ByVal BankAddress As String) As Boolean 
     _conn = New SqlConnection(_connStr) 
     _cmd = New SqlCommand() 
     _cmd.Connection = _conn 
     _cmd.CommandType = CommandType.StoredProcedure 
     _cmd.CommandText = "AddNewBank" 
     _cmd.Parameters.AddWithValue("@bankCode", SqlDbType.NVarChar).Value = BankCode 
     _cmd.Parameters.AddWithValue("@bankName", SqlDbType.NVarChar).Value = BankName 
     _cmd.Parameters.AddWithValue("@bankAddress", SqlDbType.NVarChar).Value = BankAddress 
     _conn.Open() 
     _cmd.ExecuteNonQuery() 
     _conn.Close() 
     Return True 
    End Function 

End Class 

這個UI類

Dim obj As New sqlHelper 
    Dim entity As New bankEntity 

     entity.bankCode = txtBankCode.Text 
     entity.bankName = txtBankName.Text 
     entity.bankAddress = txtBankAddress.Text 

    obj.AddNewBank(entity.bankCode , entity.bankName , entity.bankAddress) 

我希望把唯一的對象(實體)類(BankEntity)作爲AddNewBank函數的參數。例如:

Dim obj As New sqlHelper 

Dim entity As New bankEntity 
entity.bankCode = txtBankCode.Text 
entity.bankName = txtBankName.Text 
entity.bankAddress = txtBankAddress.Text 

obj.AddNewBank(entity) ----please have a look here 

這可能嗎?

+0

如果該函數需要3個字符串參數,那麼你必須傳遞它3個字符串參數。 –

回答

1

您應該將AddNewBank更改爲:

AddNewBank(entity as bankEntity) 

然後你就可以很容易地通過你的實體對象的功能。

相關問題