473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Proper use of Interfaces?

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
Jan 5 '07 #1
2 1269
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


--
http://www.openmymind.net/
http://www.fuelindustries.com/
"RSH" <wa*************@yahoo.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>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

Jan 5 '07 #2
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
"Karl Seguin" <ka********@removeopenmymindremovemetoo.andmenetwr ote in
message news:uT**************@TK2MSFTNGP02.phx.gbl...
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


--
http://www.openmymind.net/
http://www.fuelindustries.com/
"RSH" <wa*************@yahoo.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>>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()Privat e 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


Jan 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
4
by: DKode | last post by:
This should be an easy question. With all of my dataadapters, when I am done with them i do the following: DataAdapter da = new DataAdapter(); // do something with da da.Dispose(); Is...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
8
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
18
by: rdemyan via AccessMonster.com | last post by:
Here's my plan for creating nightly backups of the production back-end file (the IT staff in their infinite wisdom have prevented use of Windows Scheduler and users do not have administrative...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
8
by: =?Utf-8?B?QmVu?= | last post by:
Hi, I have a couple of questions about the proper design of classes. I'll use a simple Customer class for my question. 1) Lets say that I have this Customer class like I said, and I want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.