Proper use of Interfaces?

R

RSH

I am trying really hard to grasp the concept of Interfaces and their real
world usage.

I constructed a simple project of a webpage that contains two controls. I
am enabling communication back and forth between the page as well as
controls. I used two interfaces to remove the hardcoded references.

Is what I'm doing correct? Any suggestions?

Thanks!
Ron

SamplePage.aspx.vb------------------------------------------------------------------
Public Class SamplePage

Inherits System.Web.UI.Page

Implements IResultContainer

Private RR As Results

Private RH As ResultHeader

Private _Title As String

Public ReadOnly Property Title() As String Implements IResultContainer.Title

Get

Return Page.ToString

End Get

End Property

Public ReadOnly Property Results() As IResult Implements
IResultContainer.IResults

Get

Return RR

End Get

End Property

Public ReadOnly Property Header() As ResultHeader Implements
IResultContainer.ResultHeader

Get

Return RH

End Get

End Property

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

RR = CType(Page.LoadControl("Results.ascx"), Results)

RH = CType(Page.LoadControl("ResultHeader.ascx"), ResultHeader)

End Sub

End Class



SamplePage.Aspx -----------------------------------------------------------------------

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="SamplePage.aspx.vb" Inherits="Communications.SamplePage"%>
<%@ Register TagPrefix="Result" TagName="Results" Src="Results.ascx" %>
<%@ Register TagPrefix="Result" TagName="Header" Src="ResultHeader.ascx" %>

<HTML>
<body>
<form id="Form1" method="post" runat="server">
<Result:Results id="rr" runat="server" />
<Result:Header id="rh" runat="server" />
</form>
</body>
</HTML>




Results.ascx--------------------------------------------------------------------------------------

Public Class Results

Inherits System.Web.UI.UserControl

Implements IResult

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private _Info As DataTable

Private _Title As String

Private _RowCount As String

Public Property RowCount() As String Implements IResult.RowCount

Get

_RowCount = GetRowCount()

Return _RowCount

End Get

Set(ByVal Value As String)

_RowCount = Value

End Set

End Property

Public Property Info() As DataTable Implements IResult.Info

Get

_Info = GetAllResults()

Return _Info

End Get

Set(ByVal Value As DataTable)

_Info = Value

End Set

End Property

Private Function GetAllResults() As DataTable

Dim dt As New DataTable

Dim dc As DataColumn

Dim dr As DataRow

dc = New DataColumn("Col1")

dc.DataType = System.Type.GetType("System.String")

dt.Columns.Add(dc)

dr = dt.NewRow

dr.Item("Col1") = "Row1 Col1"

dt.Rows.Add(dr)

dr = dt.NewRow

dr.Item("Col1") = "Row2 Col1"

dt.Rows.Add(dr)

Return dt

End Function

Private Function GetRowCount() As String

Return _Info.Rows.Count

End Function

End Class



Results.ascx-----------------------------------------------------------

Public Class ResultHeader

Inherits System.Web.UI.UserControl

Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

Protected _Info As DataTable

Protected _Title As String

Protected _RowCount As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If TypeOf (Page) Is IResultContainer Then

_Info = CType(Page, IResultContainer).IResults.Info

_Title = CType(Page, IResultContainer).Title

_RowCount = CType(Page, IResultContainer).IResults.RowCount

BindGrid()

TextBox1.Text = _Title & " Returned " & _RowCount & " rows"

End If

End Sub

Private Sub BindGrid()

DataGrid1.DataSource = (_Info)

DataGrid1.DataBind()

End Sub

End Class





Interfaces-----------------------------------------------------------------------------------

Public Interface IResultContainer

ReadOnly Property ResultHeader() As ResultHeader

ReadOnly Property Title() As String

ReadOnly Property IResults() As IResult

End Interface



Public Interface IResult

Property Info() As DataTable

Property RowCount() As String

End Interface
 
K

Karl Seguin

Looks good to me.

Looking @ the code, I assume your user control can't work if the page
doesn't implement IResultContainer. If so, I'd go the extra step and throw
an exception in that case.

I would also avoid the multiple ctypes and do it once...

if typeof(Page)... then
dim container as IResultContainer = ctype(Page, IResultContainer)
container...
container..
end if
 
R

RSH

Thanks Karl!

BTW you might recognize the exersize :)

I was trying to really "get it" I was concerned about how to make sure that
the Container page contains both of the controls. i understand the
implementation of the IResultHeader interface but I was unsure how to
implement the iResults interface in this context. I found that this worked
but if it is a proper implementation I need to dive in and really try to
grasp it.

thanks!
Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top