访问在C#中从另一个类的方法 [英] Accessing methods from another class in C#

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

问题描述

我公司拥有一批在一个类文件中的类,我希望他们都能够访问相同的,全球性的方法来保存复制的代码。问题是,我似乎无法从另一个类访问的方法在我的文件 - 任何想法



所以,我的Class1.cs布局与此类似:

 公共类岗位1 
{
公共作业1()
{

}
}

公共类方法
{
公共静态无效方法1()
{
//想从这里访问方法作业1
}
}


解决方案

你需要指定他们在课堂上是这样的:

 公共作业1()
{
Methods.Method1()
}

如果该类作业1 是不同的命名空间从方法那么你就需要以添加一个使用条款,或调用方法时指定的命名空间。 Name.Space.Methods.Method1()


I have a number of classes in a Classes file, and I want them all to be able to access the same, global method to save duplicating code. Problem is, I can't seem to access a method from another class in my file - any ideas?

So my class1.cs layout is similar to this:

public class Job1
{
    public Job1()
    {

    }
}

public class Methods
{
    public static void Method1()
    {
        //Want to access method here from Job1 
    }
}

解决方案

You'll need to specify the class they are in. Like this:

public Job1()
{
  Methods.Method1()
}

If the class Job1 is in a different namespace from Methods then you'll need to either add a using clause, or specify the the namespace when calling the method. Name.Space.Methods.Method1()

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

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