Cody Hicks
Cody Hicks

Reputation: 420

Can I call a public void method that is located in another class?

I was wondering how to go about calling a VOID method; located in my Default code behind, from a web user control?

The control contains a statement that needs to use the method that I have declared in my Default page code behind. I would just declare the method in the control's code behind but the method will be used multiple times so ideally, I would like to keep it in either the Default code behind or even put it in a class.

The problem is that I've tried keeping it in the Default's public partial class and I've attempted to create a separate class but I cant figure out how to call it when I need to use it.

The class from within Default.aspx.cs:

// This method adds a string to the status output area
public void AddStatusMessage(string type, string message)
{
    if (message == string.Empty || type == string.Empty) { return; }

    if (type == "success")
    {
        successMessageList.Add("SUCCESS: " + message);
        Session["SuccessMessageList"] = successMessageList;
    }
    else if (type == "error")
    {
        errorMessageList.Add(message);
        Session["ErrorMessageList"] = errorMessageList;
    }
}

The statement within a user control that needs to use the method:

// did the user enter a provider code but not check the confirmation box?
if (!ConfirmProviderCheckbox.Checked)
{
    failed = true;
    AddStatusMessage("error", "You must confirm the provider before proceeding.");
}

I would think calling the method like this from within the user control:

Namespace.Default.AddStatusMessage("error", "You must confirm the provider before proceeding.");

..but this does not seem to work.

Update 3/24: Well, I've tired to create an instance of that class within the static method to allow me to call it from my user control but I get Unknown method 'AddStatusMessage()' of Default. It's as if the new instance does not recognize my original method within the Default page class.

public static void PublicStatusMessage()
{
    Default Status = new Default().AddStatusMessage();
    Status.AddStatusMessage();
}

Upvotes: 1

Views: 4752

Answers (1)

Markus Safar
Markus Safar

Reputation: 6580

If your method AddStatusMessage is static and located e.g. in your namespace called A and in the class called B, the correct way for calling it is: A.B.AddStatusMessage("some text", "some text");.

However to do so you need to declare your method (AddStatusMessage) as static. If the method is not static you need to create an instance of the class B (which is done by using A.B myB = new A.B(); and afterwards calling the method on this instance myB.AddStatusMessage("some text", "some text");.

If the class is in the same namespace like the calling class, you do not need to add the namespace prefix.

As already explained in some comments, static has nothing to do whether the method returns a value or not. Simply spoken the keyword static means that you can call this method directly by accessing via the class it is implemented in AND that within this method you have no access to instance members.

If I understood you correctly you want something like this:

public static void AddStatusMessage(string type, string message, System.Web.SessionState.HttpSessionState sessionState, List<string> successMessageList, List<string> errorMessageList)
{
    if (string.IsNullOrEmpty(message) || string.IsNullOrEmpty(type)) 
    {
        return; 
    }

    if (type == "success")
    {
        if (successMessageList != null)
        {
            successMessageList.Add("SUCCESS: " + message);
        }

        if (sessionState != null)
        {
            sessionState["SuccessMessageList"] = successMessageList;
        }
    }
    else if (type == "error")
    {
        if (errorMessageList != null)
        {
            errorMessageList.Add(message);
        }

        if (sessionState != null)
        {
            sessionState["ErrorMessageList"] = errorMessageList;
        }
    }
}

However there are some things you should think about: What happens if type is neither "success" nor "error". In cases like this it would be wise to use an enum. In this way it's no possible to get "unwanted" values. The next point is: Why are there two lists? Why can't you add both, success and error into one list? The list goes on and on...

Upvotes: 1

Related Questions