从调用静态函数非静态函数 [英] Call non-static function from static function

查看:127
本文介绍了从调用静态函数非静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能够从一个静态函数调用的非静态函数(或)我想从静态函数调用javascript函数。

  [的WebMethod]
公共静态无效add_items(字符串itemslist)
{
    get_price(itemslist); //错误所需的非静态的对象引用    //(要么)
    ScriptManager.RegisterStartupScript(这一点,
        this.GetType(),
        脚本,
        < SCRIPT LANGUAGE =JavaScript的'。
           +类型=文/ JavaScript的'>消息();< / SCRIPT>中,
        假); //错误,在此,这
}保护无效get_price(字符串ITEM_ID)
{}


解决方案

由于静态方法可访问时,无论你是否不实例化类,从静态方法访问类的成员可能意味着你在引用该没有按什么'T还存在于堆栈或堆,从而在运行时创建一个例外。

由于这个原因,你不能引用静态方法里面的非静态成员不首先实例化的非静态成员,这就是为什么你会得到一个错误。希望这有助于!

I am not able to call a non-static function from a static function (or) i want to call a javascript function from static function.

[WebMethod]
public static void add_items(string itemslist)
{
    get_price(itemslist); // Error An object reference is required for non-static

    //(or)
    ScriptManager.RegisterStartupScript(this, 
        this.GetType(), 
        "script", 
        "<Script language='javascript' "
           + "type='text/javascript'>message();</script>", 
        false); //Error in this, this
}

protected void get_price(string item_id)
{

}

解决方案

Since static methods are accessible regardless whether or not you instantiated that class, accessing a class member from a static method could potentially mean that you're referencing something that doesn't yet exist in the stack or the heap, thus creating an exception at runtime.

For this reason, you can't reference non-static members inside static methods without instantiating the non-static member first, and that's why you're getting an error. Hope this helps!

这篇关于从调用静态函数非静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆