Question on interfaces and inheritance

This is a discussion on Question on interfaces and inheritance within the CSharp forums in Programming Languages category; Should a class implementing an interface define the methods in the interface? That seems logical to me but I have the following code: public class XmlDataSourceMarc : XmlDataSource { protected DataSourceView tst; public override DataSourceView GetView(string viewName) { return tst; } } This gives the error: 'XmlDataSourceMarc.GetView(string)': no suitable method found to override Now if I look at the base class, it's true GetView is not defined in the class public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource { public XmlDataSource(); public virtual int CacheDuration { get; set; } public virtual DataSourceCacheExpiry CacheExpirationPolicy { get; set; } public virtual string CacheKeyDependency ...

Go Back   Application Development Forum > Programming Languages > CSharp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 10:36 AM
Marc
Guest
 
Default Question on interfaces and inheritance

Should a class implementing an interface define the methods in the
interface?
That seems logical to me but I have the following code:

public class XmlDataSourceMarc : XmlDataSource
{
protected DataSourceView tst;
public override DataSourceView GetView(string viewName)
{
return tst;
}
}

This gives the error: 'XmlDataSourceMarc.GetView(string)': no suitable
method found to override


Now if I look at the base class, it's true GetView is not defined in the
class

public class XmlDataSource : HierarchicalDataSourceControl, IDataSource,
IListSource
{
public XmlDataSource();
public virtual int CacheDuration { get; set; }
public virtual DataSourceCacheExpiry CacheExpirationPolicy { get;
set; }
public virtual string CacheKeyDependency { get; set; }
public virtual string Data { get; set; }
public virtual string DataFile { get; set; }
public virtual bool EnableCaching { get; set; }
public virtual string Transform { get; set; }
public virtual XsltArgumentList TransformArgumentList { get; set; }
public virtual string TransformFile { get; set; }
public virtual string XPath { get; set; }
public event EventHandler Transforming;
protected override HierarchicalDataSourceView
GetHierarchicalView(string viewPath);
public XmlDocument GetXmlDocument();
protected virtual void OnTransforming(EventArgs e);
public void Save();
}

But the IDataSource interface it should implement has.

public interface IDataSource
{
event EventHandler DataSourceChanged;
DataSourceView GetView(string viewName);
ICollection GetViewNames();
}

So what am I missing? Why XmlDataSource does not have an implementation of
DataSourceView GetView(string viewName) while it implements the interface
IDataSource that defines it?

Marc Wentink


Reply With Quote
  #2  
Old 08-27-2008, 10:49 AM
Hans Kesting
Guest
 
Default Re: Question on interfaces and inheritance

Marc brought next idea :
> Should a class implementing an interface define the methods in the interface?
> That seems logical to me but I have the following code:
>
> public class XmlDataSourceMarc : XmlDataSource
> {
> protected DataSourceView tst;
> public override DataSourceView GetView(string viewName)
> {
> return tst;
> }
> }
>
> This gives the error: 'XmlDataSourceMarc.GetView(string)': no suitable
> method found to override
>
>
> Now if I look at the base class, it's true GetView is not defined in the
> class
>
> public class XmlDataSource : HierarchicalDataSourceControl, IDataSource,
> IListSource
> {
> public XmlDataSource();
> public virtual int CacheDuration { get; set; }
> public virtual DataSourceCacheExpiry CacheExpirationPolicy { get;
> set; }
> public virtual string CacheKeyDependency { get; set; }
> public virtual string Data { get; set; }
> public virtual string DataFile { get; set; }
> public virtual bool EnableCaching { get; set; }
> public virtual string Transform { get; set; }
> public virtual XsltArgumentList TransformArgumentList { get; set; }
> public virtual string TransformFile { get; set; }
> public virtual string XPath { get; set; }
> public event EventHandler Transforming;
> protected override HierarchicalDataSourceView
> GetHierarchicalView(string viewPath);
> public XmlDocument GetXmlDocument();
> protected virtual void OnTransforming(EventArgs e);
> public void Save();
> }
>
> But the IDataSource interface it should implement has.
>
> public interface IDataSource
> {
> event EventHandler DataSourceChanged;
> DataSourceView GetView(string viewName);
> ICollection GetViewNames();
> }
>
> So what am I missing? Why XmlDataSource does not have an implementation of
> DataSourceView GetView(string viewName) while it implements the interface
> IDataSource that defines it?
>
> Marc Wentink


The method does exist but is defined as "explicit interface
implementation". If you cast the XmlDataSource to IDataSource, then you
can use the GetView method.

Hans Kesting


Reply With Quote
  #3  
Old 08-27-2008, 10:55 AM
Clive Dixon
Guest
 
Default Re: Question on interfaces and inheritance

If I look at XmlDataSource with Reflector, it does have an implementation of
GetView.

"Marc" <m.wentink5@removethischello.nl> wrote in message
news:%23tweqIFCJHA.4884@TK2MSFTNGP02.phx.gbl...
> Should a class implementing an interface define the methods in the
> interface?
> That seems logical to me but I have the following code:
>
> public class XmlDataSourceMarc : XmlDataSource
> {
> protected DataSourceView tst;
> public override DataSourceView GetView(string viewName)
> {
> return tst;
> }
> }
>
> This gives the error: 'XmlDataSourceMarc.GetView(string)': no suitable
> method found to override
>
>
> Now if I look at the base class, it's true GetView is not defined in the
> class
>
> public class XmlDataSource : HierarchicalDataSourceControl,
> IDataSource, IListSource
> {
> public XmlDataSource();
> public virtual int CacheDuration { get; set; }
> public virtual DataSourceCacheExpiry CacheExpirationPolicy { get;
> set; }
> public virtual string CacheKeyDependency { get; set; }
> public virtual string Data { get; set; }
> public virtual string DataFile { get; set; }
> public virtual bool EnableCaching { get; set; }
> public virtual string Transform { get; set; }
> public virtual XsltArgumentList TransformArgumentList { get; set; }
> public virtual string TransformFile { get; set; }
> public virtual string XPath { get; set; }
> public event EventHandler Transforming;
> protected override HierarchicalDataSourceView
> GetHierarchicalView(string viewPath);
> public XmlDocument GetXmlDocument();
> protected virtual void OnTransforming(EventArgs e);
> public void Save();
> }
>
> But the IDataSource interface it should implement has.
>
> public interface IDataSource
> {
> event EventHandler DataSourceChanged;
> DataSourceView GetView(string viewName);
> ICollection GetViewNames();
> }
>
> So what am I missing? Why XmlDataSource does not have an implementation of
> DataSourceView GetView(string viewName) while it implements the interface
> IDataSource that defines it?
>
> Marc Wentink
>
>



Reply With Quote
  #4  
Old 08-27-2008, 11:05 AM
Marc
Guest
 
Default Re: Question on interfaces and inheritance


"Hans Kesting" <news.hansdk@spamgourmet.com> wrote

> The method does exist but is defined as "explicit interface
> implementation".


Aha. I will do some reading on that! Thanks.


Reply With Quote
  #5  
Old 08-27-2008, 12:18 PM
Marc
Guest
 
Default Re: Question on interfaces and inheritance


"Hans Kesting" <news.hansdk@spamgourmet.com> wrote


> The method does exist but is defined as "explicit interface
> implementation". If you cast the XmlDataSource to IDataSource, then you
> can use the GetView method.


But let us say I do not want to use the GetView method, as a method of a
instantated variable of type XmlDataSource , but I want to define a new
subclass of XmlDataSource that overrides the GetView method? How do I do
that?

public class XmlDataSourceMarc : XmlDataSource /* fails */
/*
HierarchicalDataSourceControl fails */
/* DataSourceControl
works */
{
DataSourceView myView;
protected override DataSourceView GetView(string viewName)
{
return myView;
}
}


Reply With Quote
  #6  
Old 08-27-2008, 01:08 PM
Peter Duniho
Guest
 
Default Re: Question on interfaces and inheritance

On Wed, 27 Aug 2008 09:18:39 -0700, Marc <m.wentink5@removethischello.nl>
wrote:

> "Hans Kesting" <news.hansdk@spamgourmet.com> wrote
>
>
>> The method does exist but is defined as "explicit interface
>> implementation". If you cast the XmlDataSource to IDataSource, then you
>> can use the GetView method.

>
> But let us say I do not want to use the GetView method, as a method of a
> instantated variable of type XmlDataSource , but I want to define a new
> subclass of XmlDataSource that overrides the GetView method? How do I do
> that?


You need to declare your sub-class as also implementing the interface, and
then explicitly implement your override:

public class XmlDataSourceMarc : XmlDataSource, IDataSource
{
protected DataSourceView tst;
public DataSourceView IDataSource.GetView(string viewName)
{
return tst;
}
}

Pete
Reply With Quote
  #7  
Old 08-28-2008, 03:56 AM
Marc
Guest
 
Default Re: Question on interfaces and inheritance


"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote

> You need to declare your sub-class as also implementing the interface, and
> then explicitly implement your override:
>
> public class XmlDataSourceMarc : XmlDataSource, IDataSource
> {
> protected DataSourceView tst;
> public DataSourceView IDataSource.GetView(string viewName)
> {
> return tst;
> }
> }



Ok, now I am reallly starting to feel stupid but I got this and it still
gives an error.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public class XmlDataSourceMarc : XmlDataSource, IDataSource
{
DataSourceView myView;
public override DataSourceView GetView(string viewName)
{
return myView;
}
}

Error 1 'XmlDataSourceMarc.GetView(string)': no suitable method found to
override E:\user\m.wentink\My Documents\Visual Studio
2008\H4SPXML1\App_Code\XmlDataSourceMarc.cs 16 36 E:\...\H4SPXML1\

I tried to change public in protected but that's not it either.

And here some link with information about GetView

http://msdn.microsoft.com/en-us/libr...ew(VS.85).aspx






Reply With Quote
  #8  
Old 08-28-2008, 04:15 AM
Hans Kesting
Guest
 
Default Re: Question on interfaces and inheritance

Marc brought next idea :
> "Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote
>
>> You need to declare your sub-class as also implementing the interface, and
>> then explicitly implement your override:
>>
>> public class XmlDataSourceMarc : XmlDataSource, IDataSource
>> {
>> protected DataSourceView tst;
>> public DataSourceView IDataSource.GetView(string viewName)
>> {
>> return tst;
>> }
>> }

>
>
> Ok, now I am reallly starting to feel stupid but I got this and it still
> gives an error.
>
> using System;
> using System.Data;
> using System.Configuration;
> using System.Linq;
> using System.Web;
> using System.Web.Security;
> using System.Web.UI;
> using System.Web.UI.WebControls;
>
> public class XmlDataSourceMarc : XmlDataSource, IDataSource
> {
> DataSourceView myView;
> public override DataSourceView GetView(string viewName)
> {
> return myView;
> }
> }
>
> Error 1 'XmlDataSourceMarc.GetView(string)': no suitable method found to
> override E:\user\m.wentink\My Documents\Visual Studio
> 2008\H4SPXML1\App_Code\XmlDataSourceMarc.cs 16 36 E:\...\H4SPXML1\
>
> I tried to change public in protected but that's not it either.
>
> And here some link with information about GetView
>
> http://msdn.microsoft.com/en-us/libr...ew(VS.85).aspx


You missed something on the methodname: it's "IDataSource.GetView"
(note the extra "IDataSource" that signals this is an explicit
interface implementation.

Hans Kesting


Reply With Quote
  #9  
Old 08-28-2008, 05:14 AM
Marc
Guest
 
Default Re: Question on interfaces and inheritance


"Hans Kesting" <news.hansdk@spamgourmet.com> wrote


> You missed something on the methodname: it's "IDataSource.GetView" (note
> the extra "IDataSource" that signals this is an explicit interface
> implementation.


public class XmlDataSourceMarc : XmlDataSource, IDataSource
{
DataSourceView myView;
DataSourceView IDataSource.GetView(string viewName)
{
return myView;
}
}

Ok this works and I will do some study on interface semantics, and write
some C# code on it. But thanks for the help on my probably newbee questions.


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:24 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.