tags:

views:

67

answers:

2

Hello, I have two methods in the same class and would like to find out how to use the first method in the second one.

// first method

public static void RefreshGridView(GridView GridView1)
{
    GridView1.DataBind();
}

// second method

public static void AssignDefaultUserNameLetter(Literal categoryID, ObjectDataSource ObjectDataSource1)
{
    // declare variable for filter query string
    string userFirstLetter = HttpContext.Current.Request.QueryString["az"];

    // check for category ID
    if (String.IsNullOrEmpty(userFirstLetter))
    {
        // display default category
        userFirstLetter = "%";
    }

    // display requested category
    categoryID.Text = string.Format(" ... ({0})", userFirstLetter);

    // specify filter for db search
    ObjectDataSource1.SelectParameters["UserName"].DefaultValue = userFirstLetter + "%";

    // HERE IS WHAT I DON"T KNOW HOW!
    // GET SQUIGLY LINE
    RefreshGridView(GridView1);
}

Please note the capital letters above. That is where I am trying to call the first method but getting the red underline. Can some one help please? Thank you.

+6  A: 

The method is marked as static but GridView1 looks like it is an instance variable.

You need to change the method so that AssignDefaultUserNameLetter is not static or the GridView is fetched some other way such as passed in as a parameter.

Andrew Kennan
Andrew thank you! I added the parameters (Gridview GridView1) in addition to the others and it works. Is this a correct way to do this? Thanks.
John Walsh
I think that if your AssignDefaultUserNameLetter method is only called from within the same class it should probably be "private" rather than "public static" but without knowing much about what you're doing I can't say for certain.
Andrew Kennan
A: 

You probably don't want either of those methods to be static as they both appear to operate on instance variables of your class (which appears to be a form). Is there any particular reason you made them static?

Donnie
@Donnie, the reason was because I am clueless. I could not figure out how to instantiate it in my code behind. Visual Studio said: An object reference is required for the non-static field. So I made it static. I know it was not a smart move but I'm just learning and its painful.
John Walsh