I'm following the MVC Music Store tutorial, but I've just gotten a bit stuck with the Html Helper in part 5: Part 5.
I seem to have followed it correctly so far (please correct me if I'm wrong :) )...however I am getting the following error:
'musicStoreMVC.Helpers.HtmlHelper': static types cannot be used as parameters
Here is the code from my application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace musicStoreMVC.Helpers
{
public static class HtmlHelper
{
public static string Truncate(this HtmlHelper helper, string input, int length)
{
if (input.Length <= length)
{
return input;
}
else
{
return input.Substring(0, length) + "...";
}
}
}
}
If anyone can see what I'm doing wrong, or if more info is needed, I'd be grateful for the pointers!! Thanks.
As MSDN says A static constructor is called automatically to initialize the class before the first instance is created, therefore you can't send it any parameters. We can only pass parameter to a non static constructor(parametrized constructor ) but we can not pass any parameter to a static constructor...
C# Static types cannot be used as parameters.
We cannot declare a variable or instance of static class which means we cannot do it for this static type of class , but despite this we call the static class by name to acccess its members. So this is it for today.
A static class in C# is a class that cannot be instantiated. A static class can only contain static data members including static methods, static constructors, and static properties. In C#, a static class is a class that cannot be instantiated.
Just rename your static HtmlHelper
class to HtmlHelperExtensions
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With