简体   繁体   中英

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.

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. 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.

I'm sure I'm just doing something daft because I've not doing C# in quite a while.

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#. 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()
            {
            }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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