views:

110

answers:

3
 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

+1  A: 

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));
}
SLaks
But i need to use the html helper in my method, and i cannot create new instance of it
WingMan20-10
Huh? What do you mean?
SLaks
I need to call my Extra method without passing the first parameter. But I don't want to pass the htmlhelper not the htmlattribuites.So if i create an overload i wont be able to use the htmlhelper variable, i want to assign a value to that.
WingMan20-10
I have no idea what you're saying
SLaks
you are making the last parameter an optional parameter, I want to know how to make "this HtmlHelper helper" an optional paramater,sorry for the confusion
WingMan20-10
It's an extension method. The first parameter -- htmlHelper -- will ALWAYS be required.
andymeadows
+7  A: 

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);
P.Brian.Mackey
I think you've figured it out.
SLaks
this wont work HtmlHelper helper = new HtmlHelper();because the HtmlHelper has no constructor.
WingMan20-10
Brian's code was just an example to demonstrate that you need an HtmlHelper variable on which to call your Extra extension method. THe point is that the syntax has to be something like: `<your HtmlHelper instance>.Extra(message, htmlAttributes);` You're probably going to call this method within a view, in which case you already have access to an HtmlHelper instance (the Html property of your view) and you don't need to create a new instance of it. You should be able to do something like `<%= Html.Extra(message, htmlAttributes) %>` or `<% JQuery jq = Html.Extra(message, htmlAttributes); %>`
Dr. Wily's Apprentice
Ok, I got it thanks
WingMan20-10
A: 

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

apoorv020
this is my code.... I need to include the html helper i need to use it in my method..Well i meant u can not create a instance the way he wrote it.
WingMan20-10