如何在C#中的另一个.cs类中调用一个.cs类的方法 [英] How to call a method of one .cs class in another .cs class in C#

查看:873
本文介绍了如何在C#中的另一个.cs类中调用一个.cs类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void TrustedForm()
{
    char doubleQuote = '"';
    if (ViewState["OfferID"].ToString() == "992")
    {
        trustedFormScript = "<script src=" + doubleQuote + "http://api.trustedform.com/trustedform.js?field=trustedformfield"
        + doubleQuote + "  type=" + doubleQuote + "text/javascript" + doubleQuote + "></script>";
    }
}

public partial class Offer : BaseForm
{
    public partial class Admin_EditCompaign : System.Web.UI.Page
    {
        protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton oButton = (ImageButton)sender;

            if (oButton.ID == "btnUpdatePostingSetUp")
            {
                if (ddlMethod.SelectedValue != "File")
                {
                    if (txtClientPostURL.Text.Trim().Length == 0)
                    {
                        //ClientScript.RegisterClientScriptBlock(GetType(), "PostURL", "alert('Client Post URL required')", true);
                        //return;
                    }
                }
            }

            if (IsValid)
            {
                SaveData();
                SetTabVisibility(true);

            }

            SelectTab(oButton.ID);

            //Trusted form if checked
            if (chkTrustedForm.Checked)
            {
                
            }
        }
    }
}





我尝试了什么:



我宣布了方法



What I have tried:

I have declared the method

public static void TrustedForm()
 above existed if condition in 

页面中的条件存在,则上面存在TrustedForm()

Offer.aspx.cs





我想在

page.

I want to call that method in

Admin/EditCampaign.aspx.cs

页面中调用该方法。



问题是我无法通过在EditCampaign页面中调用

page.

Problem is I dont get intellisense to call

Offer.TrustedForm()

进行智能感知并获取错误:

in EditCampaign page and get error:

A namespace cannot directly contain members like fields or methods

等成员



尝试使用静态和无静态声明实例仍然没有用。请帮助我。



上面显示的代码:报价页面中的trustedform方法,报价页面的顶部,Admin / editCampaign页面的顶部和我想要的地方调用方法

in Offer page

Tried using static and without static declaring the instance still no use. Please assist me on this.

Code above shown: trustedform method in offer page , top part of offer page, top part of Admin/editCampaign page and place where I want to call the method

//Trusted form if checked
if (chkTrustedForm.Checked)
{
    
}

推荐答案

你需要基本上按照3个步骤进行操作。



1.如果你的课程在dll中,那么将dll的引用带到你的解决方案文件中。

2.使用using< namespace>;到你班级的标题。

3.使用别名调用方法或使用classname.methodName调用静态方法。



问候,

Avinash kumar
you need to follow basically 3 steps.

1. if your class is in dll then take reference of the dll to your solution file.
2. use "using <namespace>;" to the header of your class.
3. call the method using alias or with classname.methodName if static method.

Regards,
Avinash kumar


文件本身没有意义,因为该方法是Offer类的一部分,它必须在其类声明中,而不仅仅是同一个文件



The file itself has no significance, for the method to be a part of the Offer class it has to be inside its class declaration, not just in the same file

public partial class Offer : BaseForm
{
    public static void TrustedForm()
    {
        // your code here
    }

    // rest of Offer code here
}


这篇关于如何在C#中的另一个.cs类中调用一个.cs类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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