简体   繁体   English

为什么我不能使用另一类的方法

[英]Why can't i use a method from another class

I am putting together an application but I'm getting a strange issue where i can't use any methods from a class i've created with a couple of methods, the methods don't do anything at the moment because I'm just getting the shell of the program in place. 我正在组合一个应用程序,但遇到一个奇怪的问题,即我无法使用通过几种方法创建的类中的任何方法,这些方法目前无法执行任何操作,因为我只是使程序的外壳到位。 I am trying to call from the Form1 class below, specifically from a button click checking a specific operation from radio buttons. 我试图从下面的Form1类中调用,特别是从按钮单击中检查单选按钮的特定操作。

If btnDeviceControlAccept_Click is clicked it checks which of the radio buttons and goes to a method in the DeviceControlMethods class such as Add, Change or Delete VLAN. 如果单击btnDeviceControlAccept_Click,它将检查哪些单选按钮并转到DeviceControlMethods类中的方法,例如“添加”,“更改”或“删除VLAN”。 When i use the object (dc, DeviceControlMethods dc = new DeviceControlMethods();)I created in the Form1 i'm unable to use the methods even if the class is public or if i set the methods to static and use DeviceControlMethods.AddVlan etc. 当我使用对象(dc,DeviceControlMethods dc = new DeviceControlMethods();)我在Form1中创建时,即使该类是公共的,或者我将方法设置为static并使用DeviceControlMethods.AddVlan等,我也无法使用这些方法。

I'm sure I'm just doing something daft because I've not doing C# in quite a while. 我确定我只是在做一些愚蠢的事情,因为我已经有一段时间没有使用C#了。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MFT___Configurator
{
    public partial class Form1 : Form
    {       
        public Form1()
        {
            InitializeComponent();
        }      
        private void label1_Click(object sender, EventArgs e)
        {
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void groupBox1_Enter(object sender, EventArgs e)
        {
        }
        private void btnDeviceControlAccept_Click(object sender, EventArgs e)
        {
            DeviceControlMethods dc = new DeviceControlMethods();
            if (rbAddDevice.Checked == true)
            {   
                dc.CreateVlan() // the method is not found
                 resutlBox.Clear();
            }
            else if (rbChange.Checked == true)
            {
                resutlBox.Clear();
            }
            else if (rbDelete.Checked == true)
            {
                resutlBox.Clear();
            }
            else
            {
                resutlBox.Clear();
                resutlBox.Text = "Select a valid operation; Add, Change or Delete.";
            }
        }

Class with the methods i want to call; 用我想调用的方法来类;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MFT___Configurator
{
    public class DeviceControlMethods
    {
         static DeviceControlMethods()
         {
            string CreateVlan()
            {
                Console.WriteLine("ggg");
                return "";
            }
            string ChangeVlan()
            {
                return "";
            }
            void DeleteVlan()
            {
            }
        }
    }
}

I see only private methods, you need to make them public explicitly, not only the class. 我只看到私有方法,您需要显式地公开它们,而不仅仅是类。 See the docs about access modifiers 请参阅有关访问修饰符的文档

public 上市
The type or member can be accessed by any other code in the same assembly or another assembly that references it. 该类型或成员可以由同一程序集或引用该程序集的另一个程序集中的任何其他代码访问。

private 私人的
The type or member can be accessed only by code in the same class or struct. 类型或成员只能由相同类或结构中的代码访问。

protected 保护
The type or member can be accessed only by code in the same class, or in a class that is derived from that class. 该类型或成员只能由同一类或从该类派生的类中的代码访问。

internal 内部
The type or member can be accessed by any code in the same assembly, but not from another assembly. 可以通过同一程序集中的任何代码访问类型或成员,但不能从另一个程序集中访问该类型或成员。

protected internal 受保护的内部
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. 可以通过声明了该类型或成员的程序集中的任何代码或从另一个程序集中的派生类中访问该类型或成员。

private protected 私人保护
The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class. 该类型或成员只能在其声明程序集中,通过同一类或从该类派生的类型的代码访问。

Edit And, as other comments state as well, methods defined in the static constructor won't be accessible either. 编辑而且,正如其他注释所指出的那样,静态构造函数中定义的方法也将无法访问。

You have scoping issues with your class. 您的课程存在范围界定问题。 Read through this article to learn more about scoping in C#. 通读本文以了解有关C#范围界定的更多信息。 https://msdn.microsoft.com/en-us/library/ms973875.aspx https://msdn.microsoft.com/en-us/library/ms973875.aspx

But to solve your issue, change your class to be as follows: 但是要解决您的问题,请将您的班级更改为以下内容:

    public class DeviceControlMethods
    {
         public string CreateVlan()
            {
                Console.WriteLine("ggg");
                return "";
            }

            public string ChangeVlan()
            {
                return "";
            }

            public void DeleteVlan()
            {
            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM