Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以调用位于另一个类中的公共void方法吗?_C#_Asp.net - Fatal编程技术网

C# 我可以调用位于另一个类中的公共void方法吗?

C# 我可以调用位于另一个类中的公共void方法吗?,c#,asp.net,C#,Asp.net,我想知道如何调用VOID方法;位于我的默认代码后面,来自web用户控件 该控件包含一条语句,该语句需要使用我在默认页面代码隐藏中声明的方法。我只想在控件的代码隐藏中声明该方法,但该方法将被多次使用,因此理想情况下,我希望将其保留在默认代码隐藏中,甚至将其放在类中 问题是,我尝试将它保留在默认的公共部分类中,并且尝试创建一个单独的类,但我不知道在需要使用它时如何调用它 Default.aspx.cs中的类: // This method adds a string to the status ou

我想知道如何调用VOID方法;位于我的默认代码后面,来自web用户控件

该控件包含一条语句,该语句需要使用我在默认页面代码隐藏中声明的方法。我只想在控件的代码隐藏中声明该方法,但该方法将被多次使用,因此理想情况下,我希望将其保留在默认代码隐藏中,甚至将其放在类中

问题是,我尝试将它保留在默认的
公共部分类中,并且尝试创建一个单独的类,但我不知道在需要使用它时如何调用它

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;
    }
}
// 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.");
}
Namespace.Default.AddStatusMessage("error", "You must confirm the provider before proceeding.");
用户控件中需要使用以下方法的语句:

// 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;
    }
}
// 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.");
}
Namespace.Default.AddStatusMessage("error", "You must confirm the provider before proceeding.");
我想从用户控件中调用这样的方法:

// 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;
    }
}
// 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.");
}
Namespace.Default.AddStatusMessage("error", "You must confirm the provider before proceeding.");
…但这似乎不起作用

更新3/24: 嗯,我已经厌倦了在静态方法中创建该类的实例,以允许我从用户控件调用它,但是我得到了默认的未知方法“AddStatusMessage()”。这就好像新实例无法识别默认页面类中的原始方法一样

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

如果您的方法
AddStatusMessage
是静态的,例如位于名为
A
的命名空间和名为
B
的类中,则正确的调用方法是:
A.B.AddStatusMessage(“一些文本”、“一些文本”)

但是,要执行此操作,您需要将您的方法(AddStatusMessage)声明为
static
。如果该方法不是静态的,则需要创建类B的实例(通过使用
A.B myB=new A.B();
然后在此实例上调用该方法
myB.AddStatusMessage(“一些文本”,“一些文本”);

如果类与调用类位于同一命名空间中,则不需要添加命名空间前缀

正如在一些注释中已经解释的那样,static与方法是否返回值无关。简单地说,关键字
static
意味着您可以通过实现该方法的类直接调用该方法,并且在此方法中您无权访问实例成员

如果我理解正确,你会想要这样的东西:

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;
        }
    }
}
public static void AddStatusMessage(字符串类型,字符串消息,System.Web.SessionState.HttpSessionState SessionState,列表成功消息列表,列表错误消息列表)
{
if(string.IsNullOrEmpty(消息)| | string.IsNullOrEmpty(类型))
{
返回;
}
如果(类型=“成功”)
{
if(成功消息传递者!=null)
{
添加(“成功:+消息”);
}
if(sessionState!=null)
{
sessionState[“SuccessMessageList”]=SuccessMessageList;
}
}
else if(类型==“错误”)
{
如果(errorMessageList!=null)
{
errorMessageList.Add(消息);
}
if(sessionState!=null)
{
会话状态[“ErrorMessageList”]=ErrorMessageList;
}
}
}

但是,您应该考虑一些事情:如果类型既不是“成功”也不是“错误”,会发生什么情况。在这种情况下,明智的做法是使用。这样就不可能得到“不需要的”值。下一点是:为什么有两个列表?为什么不能将成功和错误都添加到一个列表中?这个列表会不断地出现…

有许多类似的问题-看看周围…你要么需要调用静态方法,要么需要在其上创建/获取实例并调用实例方法。我意识到还有其他类型的方法可以在atic无法实现这一点,但我尝试使用的方法不会返回值。:/And?作为
static
或instance与返回值无关…@Alexei Levenkov我明白你的意思了。因为在我的情况下,static不是一个选项,我将尝试创建该类的实例并对其调用该方法。该类是
public partial
只意味着它可以被任何人/从任何地方看到,而partial意味着它可以在其他地方扩展。因此问题一定是在其他地方。如果您声明一个方法
static
您就没有访问实例成员的权限。因此,如果您要使
successessagelist
也成为static,您可以按下它。但是您将无法更改
会话
。我建议您更改代码。在调用
AddStatusMessage
的地方,只需将会话和
successessagelist
作为参数传递即可。例如
AddStatusMessage(字符串a、字符串b、会话状态s、列表l)
不用担心,我明白你的意思。事实上,我可能在其他地方看到过。我会在有机会的时候尝试一下。我会向你详细解释。我会给你回复,让你知道它是如何工作的。好的。我想这是我的问题。没有指定附加参数。明白了。我还没有机会检查它,但我会去的这看起来很有希望。我很感谢你的意见。太棒了。我在同一个页面上定义了两个列表,我打电话过来。这应该可以解决问题。我编译后会发现。不过可能需要几天时间。