Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static types cannot be used as parameters

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.

like image 935
109221793 Avatar asked Jan 04 '11 21:01

109221793


People also ask

Can we pass static class as a parameter?

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...

Can we pass static class as parameter in C#?

C# Static types cannot be used as parameters.

Can not declare a variable of static type?

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.

What is a static class C#?

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.


1 Answers

Just rename your static HtmlHelper class to HtmlHelperExtensions.

like image 89
Victor Haydin Avatar answered Sep 21 '22 12:09

Victor Haydin