Question on interfaces and inheritance

M

Marc

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
 
H

Hans Kesting

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
 
C

Clive Dixon

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

Marc

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;
}
}
 
M

Marc

Peter Duniho said:
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/library/system.web.ui.datasourcecontrol.getview(VS.85).aspx
 
H

Hans Kesting

Marc brought next idea :
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/library/system.web.ui.datasourcecontrol.getview(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
 
M

Marc

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.
 

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

Similar Threads

Overriding interface members 10
calling the super class 3
Inheritance and hiding 4
lists and interfaces 4
Generics and inheritance 8
Inheritance and Interfaces 8
Equals() and inheritance 7
Inheritance Problem 9

Top