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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Imlements statement 2

Status
Not open for further replies.

aspvbnetnerd

Programmer
May 16, 2006
278
SE
I have question that I was hoping that someone could help me with.

I would like to have one Class that handles all insert to the database (IDataInsert) and another Class that handles all the Update to the database (IDataUpdate)
Then I want to have one Class that handles those two classes (IDataAdapter)

I notice a problem is that if I have the same sub name in both IDataUpdate and IDataInsert then one of the sub is called Init and the other called Init1

I want to be able to have the sub name in my DataAdapter class how should I fix this?

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
Code:
Public Class DataAdapter
    Implements IDataInsert, IDataUpdate
    Public Sub init(ByRef connString As String) Implements IDataInsert.init

    End Sub
    Public Sub init1(ByRef connString As String) Implements IDataUpdate.init

    End Sub
End Class

Regards
George
 
That is one of the problems of multiple inheritance. You can't do what you do. You have to make them different or you create a third interface.

Is dataadapter the only class that will implement this interfaces?

Christiaan Baes
Belgium

"My old site" - Me
 
Thanks for the quick response.

A third interface? How could this solve this?

I will explain my problem more detailed.
If I create one class DataInsert that implements IDataInsert
This Class handles the insert to the database.

Code:
Public Class DataInsert
    Implements IDataInsert

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

And then I have another class that implements IDataUpdate.
This class handles the Update to the database.
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 then want to Insert and then update something to the database I have to do it like this.

Code:
Private mDataUpdate As IDataUpdater 'Handles database update
Private mDataInsert As IDataInsert 'Handles database insert

mDataInsert = New DataInsert
mDataInsert.init(prvConnString)
mDataUpdate = New DataUpdater
mDataUpdate.init(prvConnString)

I have to open the same database connection base twice to perform an insert and then an update.
When it is the same server same database same table etc...

Hope you understand my problem better now.

George



 
aspvbnetnerd,

If you are sure that you want to have same implementation for both methods then you can use the following code block. It will solve your problem.

Code:
Public Interface IDataInsert
	Sub Init()
End Interface

Public Interface IDataUpdate
	Sub Init()
End Interface


Public Class DataAdapter
	Implements IDataInsert, IDataUpdate

	[b][COLOR=red]Public Overridable Sub Init() Implements IDataInsert.Init, IDataUpdate.Init[/color][/b]
		' Implementation goes here.
	End Sub

End Class

The Implements keyword, used to mark a method as an interface method, supports multiple arguments, so you can have methods from multiple interfaces that map to the same procedure.

A variant of this technique enables you to map multiple methods from one interface to the same procedure in the class. Mapping multiple methods to the same procedure works only if all the methods have the same argument signature and a return value of the same type.

Hope it helps you.

 
computerjin, thanks for the answer.

I have implemented your overridable in my code.

You deserve 10 stars, not only one.

Again, many thanks from aspvbnetnerd. :)


George
 
aspvbnetnerd,

Thank you for the appraisal. Nice to know that my solution helped you.

ComputerJin.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top