【问题标题】:not able to access functions of a different class in different namespace无法访问不同命名空间中不同类的函数
【发布时间】:2014-03-24 11:38:22
【问题描述】:

我有两个不同的命名空间

命名空间 1

namespace Project.Constants
{
   public class EducationalInstitutes
    {
       public string test() {

           return "Value";
       }
    }
}

命名空间 2

using Project.Constants;  //namespace 1 

namespace Project.DAL
{
   public class EducationalInstitute
    {

       EducationalInstitutes obj = new EducationalInstitutes();
          obj.


    }
}

问题是我在 EducationalInstitutes 类中定义的函数在我创建对象的地方不可用

【问题讨论】:

    标签: c# asp.net oop


    【解决方案1】:

    你需要把你的代码放在一个方法内的底部sn-p中。

    您不能从班级中的该位置调用 obj 上的方法。

    public class EducationalInstitute
    {
       EducationalInstitutes obj = new EducationalInstitutes();
    
       public void DoSomething()
       {
           obj.test();
       }
    }
    

    【讨论】:

      【解决方案2】:

      在方法内部而不是在类内部调用方法。

      【讨论】:

        【解决方案3】:

        创建一个方法并在该方法内为 EducationalInstitutes 类创建对象。然后您可以访问其中的方法。您不能直接访问类中的方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-21
          • 1970-01-01
          • 1970-01-01
          • 2018-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多