Cannot create an instance of the abstract class

S

shapper

Hello,

I have the folllowing:
RssActionResult a = new RssActionResult();

And I get the error:
Cannot create an instance of the abstract class or interface
'App.Mvc.RssActionResult'

RssActionResult is as follows:

public abstract class RssActionResult : ActionResult {
public SyndicationFeed Feed { get; set; }

public override void ExecuteResult(ControllerContext context) {
context.HttpContext.Response.ContentType = "application/rss
+xml";
Rss20FeedFormatter formatter = new Rss20FeedFormatter(Feed);
using (XmlWriter writer = XmlWriter.Create
(context.HttpContext.Response.Output)) {
formatter.WriteTo(writer);
}
}
} // RssActionResult

And ActionResult is:

public abstract class ActionResult {
protected ActionResult();
public abstract void ExecuteResult(ControllerContext context);
} // ActionResult

Does anyone knows why I am getting this error?

Thanks,
Miguel
 
S

sloan

Basic OO concepts.

You cannot instantiate an abstract class.

You must create (at least one) subclass from the abstract class, and
instantiate that subclass.
 
J

Jeff Johnson

Does anyone knows why I am getting this error?

The answer IS the subject line.
 
S

shapper

The answer IS the subject line.

That was my mistake ... RssActionResult shouldn't be abstract. I
didn't notice.

One more question if you don't mind. I have:

public ActionResult Rss() {
...
return new RssActionResult() { Feed = feed };
}

I get the error:
Cannot implicitly convert type 'MyApp.Mvc.RssActionResult' to
'System.Web.Mvc.ActionResult'

RssActionResult inherits System.Web.ActionResult. Shouldn't this work?

I know how to solve it. I just make:
public MyApp.Mvc.RssActionResult Rss() { ...

I just wasn't sure I needed to ...

Thanks,
Miguel
 
P

Pavel Minaev

It also doesn't help that you are asking questions about an area in .NET  
that, as far as MSDN appears to be concerned, doesn't even exist.  I can't  
find any reference pages on MSDN that document the "System.Web.Mvc"  
namespace at all.  Where is this stuff documented?

Pete, this is one case where I'll have to point to your own words on
"JFGI" :) I would expect you to have heard of ASP.NET MVC, at least
(and I believe Miguel has actually mentioned it by name explicitly at
least once).

That stuff is in beta, so of course the stable MSDN version doesn't
know anything about it. I believe the central site at the moment is
http://www.asp.net/mvc/
 
S

shapper

I spent a good 15-20 minutes browsing around with Google before replying  
to Miguel's post, and while I found various links related to ASP.NET MVC  
stuff, I didn't find a single online documentation resource.

(Not that "JFGI" is an accurate characterization of "my own words" anyway,  
but if the docs aren't readily available even with Google, that point is  
moot).


Sure.  I found that before.  But I don't see any links to online  
documentation there.

In any case, having the code Miguel is trying to compile would be _much_  
more useful than documentation for the MVC stuff.  Both would be helpful,  
but only one is really critical for the purpose of answering the question..

Pete

Hi Pete and Pavel,

Indeed there is no documentation on ASP.NET MVC which is on RC2.
I have been following this project since the beginning ...
.... the reason why I posted is because I don't think my problem has to
do with ASP.NET MVC but with some wrong notion I might have from
classes.

In this case I am following a code example:
http://www.developerzen.com/2009/01/11/aspnet-mvc-rss-feed-action-result/

"So basically, to return a feed result all we need to do is define our
own ActionResult implementation by deriving from ActionResult:
public abstract class ActionResult {
protected ActionResult();
public abstract void ExecuteResult(ControllerContext context);
}"

As far as I understand I will have a new ActionResult.
MyApp.Mvc.ActionResult instead of the standard
System.Web.Mvc.ActionResult.

This will create a conflict on my code since I will always need to use
System.Web.Mvc. So I tried to rename ActionResult to ActionResultCore:

public abstract class ActionResultCore {
protected ActionResultCore();
public abstract void ExecuteResult(ControllerContext context);
} // ActionResult

And I got the errors:

* 'MyApp.Mvc.ActionResultCore.ActionResultCore()' must declare a
body because it is not marked abstract, extern, or partial

* 'MyApp.Mvc.ActionResultCore.ExecuteResult
(System.Web.Mvc.ControllerContext)' hides inherited abstract member
'System.Web.Mvc.ActionResult.ExecuteResult
(System.Web.Mvc.ControllerContext)'

Something I really don't understand about this code is:
protected ActionResult;

Shouldn't I use:
public abstract class ActionResult : System.Web.Mvc.ActionResult {
public abstract void ExecuteResult(ControllerContext context);
}

Well, I think my problem has more to do with OOP then to MVC
itself ...

I am a little bit lost on the direction to take because my tries end
up always on some error.

Thank You,
Miguel
 
P

Pavel Minaev

"So basically, to return a feed result all we need to do is define our
own ActionResult implementation by deriving from ActionResult:
public abstract class ActionResult {
  protected ActionResult();
  public abstract void ExecuteResult(ControllerContext context);

}"

This isn't the code that you're supposed to add to your program. It's
a shortened definition of the stock System.Web.Mvc.ActionResult class
that you're supposed to derive from, so that you know which members
you can and should override, for exposition only. The real meat is
RssActionResult in that example - just follow that one.
 
S

shapper

This isn't the code that you're supposed to add to your program. It's
a shortened definition of the stock System.Web.Mvc.ActionResult class
that you're supposed to derive from, so that you know which members
you can and should override, for exposition only. The real meat is
RssActionResult in that example - just follow that one.

Thank You Pavel. I miss understood it ... Thanks
 

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