如何从不同的类访问方法 [英] How to access method from a different class

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

问题描述

public class CaseStageTransitionControlModel : ControlModel // *
{
    CaseStageTransitionControlModel x = new CaseStageTransitionControlModel();
}



在上面,我正在创建该类*的实例.它甚至让我做到了.现在,我正在尝试访问该类的方法.因此,当我输入x时,我看不到任何智能感知给我任何选择的机会.

为什么会这样?
如何使用该方法?

大多数时候,我发现在C#中实例化另一个类的对象时为什么会出现问题?



In the above I''m creating an instance of that class *. It even let me did that. Now I''m trying to access a method of that class. So when i type x, I don''t see any intellisense giving me any option to choose.

Why is that?
How can i access that method?

Most of the time i find problem with instantiating an object of a different class in c# why is that too?

推荐答案

检查方法的访问修饰符.应该可以公开访问以在智能中看到.

公开您的方法,例如

check the access modifiers of your method.. the method should be publicly accessible to be seen in intelicense.

make your methods public, like

public string myMethod(string param1)
{
    //do something...
    return "Hello";
}



请注意方法签名开头的 public 关键字.

如果解决您的问题,则标记为答案



notice the public keyword in the start of the method signature.

mark as answer if solves your problem


示例:

an example:

class Program
{

    static void Main()
    {
        CaseStageTransitionControlModel x = new CaseStageTransitionControlModel();
        x.Method();
        

    }
}
public class CaseStageTransitionControlModel : ControlModel // *
{
    public void Method()
    {
        Console.WriteLine("yes sir!");
    }
}


执行此操作的一种方法是创建类的新对象(SomeClass名称= new SomeClass("Construters"));然后调用这样的方法:name.YourMethodsnamehere(如果需要,此处为变量);但请记住,只有将类和方法设置为public才能执行此操作,否则您将无法从其自己的类之外访问它们.
one way you can do this is to make a new object of your class (SomeClass name = new SomeClass("Construters"); and then call the method like this: name.YourMethodsnamehere(variables here if you need any); BUT REMEMBER you can ONLY do this if you have set the class and method to public otherwise you cant access them from outside their own class.


这篇关于如何从不同的类访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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