WingMan20-10
WingMan20-10

Reputation: 3734

Declaring parameter that i don't want to pass

 public Jquery Extra(this HtmlHelper htmlhelper, 
                     string message, 
                     IDictionary<string, object> htmlAttributes)

if i declare the this Htmlhelper htmlhelper when i declare my method, but i don't want to pass that parameter in when i call the method??

am i making sense

Upvotes: 1

Views: 297

Answers (3)

apoorv020
apoorv020

Reputation: 5650

Who is the author of this function? If it's you then do not include the first parameter. public Jquery Extra(string message, IDictionary<string, object> htmlAttributes).
If it's code you have not written yourself, then the HtmlHelper variable is probably necessary, and you shoud not attempt to remove it from the function prototype.
One of your comments said that you cannot initialize a HtmlHelper, that is not technically true. See the [msdn reference].1

Upvotes: 0

SLaks
SLaks

Reputation: 887225

EDIT: It sounds like you want to be able to call this method without any HtmlHelper object at all.

If the method needs an HtmlHelper, you will not be able to call it without one.
You should rewrite the method so that it doesn't need an HtmlHelper.


You can make an overload with fewer parameters:

public static Jquery Extra(this HtmlHelper htmlhelper, string message) {
    return htmlHelper.Extra(message, null);
}

In C# 4, you can also use an optional parameter:

public Jquery Extra(this HtmlHelper htmlhelper, string message, IDictionary<string, object> htmlAttributes = null) {

I highly recommend that you also add an overload that takes an anonymous type:

public static Jquery Extra(this HtmlHelper htmlhelper, string message, object htmlAttributes) {
    return htmlHelper.Extra(message, null, new RouteValueDictionary(htmlAttributes));
}

Upvotes: 1

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

I believe you are trying to write an Extension Method. You define it like so

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static Jquery Extra(this HtmlHelper htmlhelper, string message, IDictionary htmlAttributes)
        {
            //do work
            return Jquery;
        }
    }   
}

And then use it like this:

HtmlHelper helper = new HtmlHelper();
Jquery jq = helper.Extra(message, htmlAttributes);

Upvotes: 6

Related Questions