How do I Implements Parent class Interface in child class

O

owais

Hi,

I have a problem, I want to implements Parent class interface methods in child class. for e.g

-------------- Test1.vb ----------------
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Globalization

Public Class Test1
Inherits RadioButton
Implements IPostBackDataHandler

Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData

End Function

Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent

End Sub
End Class

-------------------------------------------------

when i compiling this class it gives me error

" Interface 'System.Web.UI.IPostBackDataHandler' is already implemented by base class 'System.Web.UI.WebControls.RadioButton'."

Please help me in this regard

Thanks
Owais
 
S

Steve

owais said:
Hi,

I have a problem, I want to implements Parent class interface methods in
child class. for e.g

" Interface 'System.Web.UI.IPostBackDataHandler' is already implemented by
base class 'System.Web.UI.WebControls.RadioButton'."

Just take the implements keyword out of your derived class, and override the
methods in the base class that you want your class to handle.
You do not need the implements keyword in your overriding methods either.

eg:

public Class MyBase
implements SomeInterface

protected overridable sub DoSomething implements
SomeInterface.DoSomething
'do whatever
end sub

end class

public class MyDerived
inherits MyBase

protected overrides sub DoSomething
'do something else instead
end sub

end class


hope this helps

Steve
 
O

owais

Thanks for the reply.

I made the class according to your instruction

-------------------------------------------------------
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Globalization

Public Class Test1
Inherits RadioButton

Public Overrides Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean

End Function

Public Overrides Sub RaisePostDataChangedEvent()

End Sub
End Class
-------------------------------------------------------------

but unfortunately it also gives me the error

"function 'LoadPostData' cannot be declared 'Overrides' because it does not override a function in a base class.
sub 'RaisePostDataChangedEvent' cannot be declared 'Overrides' because it does not override a sub in a base class."


Thanks
Owais
 
R

Robin Tucker

I think in this instance you should declare those methods Overridable and
then in a derived class (child class as you say), you "overrides" the
methods, so:

Public Class Test1
Inherits RadioButton

Public Overridable Function LoadPostData(ByVal postDataKey As
String, ByVal postCollection As
System.Collections.Specialized.NameValueCollection) As Boolean

End Function

Public Overridable des Sub RaisePostDataChangedEvent()

End Sub
End Class

Public Class Test2
Inherits Test1

Public Overrides Function LoadPostData(ByVal postDataKey As String,
ByVal postCollection As System.Collections.Specialized.NameValueCollection)
As Boolean

End Function

Public Overrides des Sub RaisePostDataChangedEvent()

End Sub

End Class
 
S

Steve

owais said:
but unfortunately it also gives me the error

"function 'LoadPostData' cannot be declared 'Overrides' because it does
not override a function in a base class.
sub 'RaisePostDataChangedEvent' cannot be declared 'Overrides' because it
does not override a sub in a base class."

Hi owais

I just had a quick look through the docs. The interface is implemented in
the web checkbox class, which is the parent of the radiobutton.
The function is private though so you cannot override it

From the docs....

This member supports the .NET Framework infrastructure and is not intended
to be used directly from your code.

[Visual Basic]
Private Function LoadPostData( _
ByVal postDataKey As String, _
ByVal postCollection As NameValueCollection _
) As Boolean Implements IPostBackDataHandler.LoadPostData


Steve
 
O

owais

Thanks for the reply. Its amazing ,this code works on C#. I don't know VB.NET and C# both are using same CLR. The code of C# belo

-----------------------------------------------------------

[ToolboxData("<{0}:GroupRadioButton runat=server></{0}:GroupRadioButton>")
public class Test1 : RadioButton, IPostBackDataHandle

public GroupRadioButton() : base(



#region Propertie

private string Valu

ge

string val = Attributes["value"]
if(val == null
val = UniqueID
els
val = UniqueID + "_" + val
return val



#endregio

#region Renderin

protected override void Render(HtmlTextWriter output

RenderInputTag(output)


private void RenderInputTag(HtmlTextWriter htw

htw.AddAttribute(HtmlTextWriterAttribute.Id, ClientID)
htw.AddAttribute(HtmlTextWriterAttribute.Type, "radio")
htw.AddAttribute(HtmlTextWriterAttribute.Name, GroupName)
htw.AddAttribute(HtmlTextWriterAttribute.Value, Value)
if(Checked
htw.AddAttribute(HtmlTextWriterAttribute.Checked, "checked")
if(!Enabled
htw.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled")

string onClick = Attributes["onclick"]
if(AutoPostBack

if(onClick != null
onClick = String.Empty
onClick += Page.GetPostBackClientEvent(this, String.Empty)
htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick)
htw.AddAttribute("language", "javascript")

els

if(onClick != null
htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick)


if(AccessKey.Length > 0
htw.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey)
if(TabIndex != 0
htw.AddAttribute(HtmlTextWriterAttribute.Tabindex,
TabIndex.ToString(NumberFormatInfo.InvariantInfo))
htw.RenderBeginTag(HtmlTextWriterTag.Input)
htw.RenderEndTag()


#endregio

#region IPostBackDataHandler Member

void IPostBackDataHandler.RaisePostDataChangedEvent(

OnCheckedChanged(EventArgs.Empty)


bool IPostBackDataHandler.LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection

bool result = false
string value = postCollection[GroupName]
if((value != null) && (value == Value)

if(!Checked

Checked = true
result = true


els

if(Checked
Checked = false

return result


#endregio
}
 

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

Top