Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implements statement problem again

Status
Not open for further replies.

aspvbnetnerd

Programmer
May 16, 2006
278
SE
I have a problem using interface. The problem is that I have to open the database connection 2 times

I have asked a simular question before.
If I create one class DataAdapter that implements IDataInsert and IDataUpdate

The IDataInsert handles the insert to the database and IDataUpdate handles the update to the database.

Orginise you say :)
Class IDataInsert
Code:
Public Interface IDataInsert
    Sub init(ByRef connString As String)
End Interface


Class IDataUpdate
Code:
Public Interface IDataUpdate
    Sub init(ByRef connString As String)
End Interface

Class DataUpdapter that Implements IDataUpdate and IDataInsert

And the Init sub is this code
Code:
Public Class DataUpdater
    Implements IDataUpdater

    Private Sub init(ByRef connString As String) Implements IDataUpdater.init
        mDbConn = New SqlConnection(connString)
        mDbConn.Open()
    End Sub
End Class

If I the want to insert and after that update the database I would have do It like this.
Code:
Private mDataUpdate As IDataUpdater 'Handles database update
Private mDataInsert As IDataInsert 'Handles database insert

mDataInsert = New DataAdapter
mDataInsert.init(prvConnString)
mDataUpdate = New DataAdapter
mDataUpdate.init(prvConnString)

I dont want open the database connection twice

I hope you understand my problem and could help and solve this. :-(

George
 
Code:
Private mDataAdapter As DataAdapter 'Handles database update

mDataAdapter = New DataAdapter
mDataAdapter.init(prvConnString)

Why not this?

Christiaan Baes
Belgium

"My old site" - Me
 
chrissie1, thanks you very much for the response.

At the top I have used this declaration. Declared the Interfaces.
Code:
Private mDataUpdate As IDataUpdater 'Handles database update
Private mDataInsert As IDataInsert 'Handles database insert

With your examples i going through the Interfaces.
chrissie1 said:
Code:
Private mDataAdapter As DataAdapter 'Handles database update

mDataAdapter = New DataAdapter
mDataAdapter.init(prvConnString)

Why not this?

Then i am not using the Interface?
Then there is no use of the Interface programming, right?

George
 
The interfaces are used as a contract and if and when you need them. You don't really need them in this case so don't.

The right tools for the job kind of thing.

Now if parts of your program only wanted to use the insert part you could use only the IInsert interface but if it needs both then use the dataadapter. The interfaces just make sure that your object has the methods you need.

Christiaan Baes
Belgium

"My old site" - Me
 
Thanks for the help chrissie1. I have removed the implenting statement and now I am only connecting to the DataAdatper.

George
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top