C# 类不公开

发布于 2024-09-10 23:14:13 字数 482 浏览 8 评论 0原文

我正在尝试创建一个类,因此当我在文件中执行以下操作时:

Functions LoginFunctions = new Functions();
LoginFunctions.loadFunctions();

它将创建我需要的对象,并将其公开,以便每个调用该类的表单都能够使用它。类文件如下。

namespace App
{
    public class Functions
    {
        public void loadFunctions()
        {
            TaskbarItemInfo taskbarItemInfo = new TaskbarItemInfo();

        }
    }
}

它似乎没有将 taskbarItemInfo 对象公开,并且不允许我在类内部以外的其他任何地方使用它。如何将其公开,以便调用该类的每个文件都可以使用该对象?

I am trying to make a class so when I do the following inside a file:

Functions LoginFunctions = new Functions();
LoginFunctions.loadFunctions();

It will create my object which I need, and make it public so every form which calls the class will be able to use it. The class file is below.

namespace App
{
    public class Functions
    {
        public void loadFunctions()
        {
            TaskbarItemInfo taskbarItemInfo = new TaskbarItemInfo();

        }
    }
}

It doesn't seem to be making the taskbarItemInfo object public, and it is not letting me use it anywhere else other then inside the class. How do I make it public so every file that calls the class can use the object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

高速公鹿 2024-09-17 23:14:13

正如其他人提到的,将其设为属性,例如如下所示:

public class Functions
{
    public TaskbarItemInfo TaskbarItemInfo { get; private set; }

    public void loadFunctions()
    {
        this.TaskbarItemInfo = new TaskbarItemInfo();
    }
}

As the others have mentioned, make it a property, for example like so:

public class Functions
{
    public TaskbarItemInfo TaskbarItemInfo { get; private set; }

    public void loadFunctions()
    {
        this.TaskbarItemInfo = new TaskbarItemInfo();
    }
}
指尖上的星空 2024-09-17 23:14:13

您的 taskbaritem 类在该方法的范围内,因此您将无法在该类之外访问它。

创建一个公共属性或在方法中返回它。

    namespace App
    {
        public class Functions
        {
            private TaskbarItemInfo _taskbarItemInfo;

            public TaskbarItemInfo taskbarItemInfo
           {
               get
              {
                   return _taskbarItemInfo;
              }
           }

            public void loadFunctions()
            {
                _taskbarItemInfo = new TaskbarItemInfo();

            }
        }
    }

我还将去将 loadFunctions 方法更改为构造函数,该构造函数创建您需要的所有对象。

public Functions()
{
    _taskbarItemInfo = new TaskbarItemInfo();
}

Your taskbaritem class is in the scope of the method and therefore you wont be able to access it outsite of the class.

Create a public property or return it in the method.

    namespace App
    {
        public class Functions
        {
            private TaskbarItemInfo _taskbarItemInfo;

            public TaskbarItemInfo taskbarItemInfo
           {
               get
              {
                   return _taskbarItemInfo;
              }
           }

            public void loadFunctions()
            {
                _taskbarItemInfo = new TaskbarItemInfo();

            }
        }
    }

I would also go and change the loadFunctions method to a constructor which creates all the objects you need.

public Functions()
{
    _taskbarItemInfo = new TaskbarItemInfo();
}
疯狂的代价 2024-09-17 23:14:13

在您提供的示例中,taskbarItemInfo 是在 loadFunctions() 方法的本地范围内声明的。如果您希望它对某个类公开,则必须先使其成为类成员,然后才能将其公开。

In the example you provide, taskbarItemInfo is declared within the local scope of the loadFunctions() method. If you want it to be public for some class, you must make it a class member before you can make it public.

我恋#小黄人 2024-09-17 23:14:13

您需要将变量公开。

namespace App
{
    public class Functions
    {
        public TaskbarItemInfo TaskbarItemInfo { get; private set; }

        public void loadFunctions()
        {
            TaskbarItemInfo = new TaskbarItemInfo();
        }
    }
}

编辑:您还可以在构造函数中对项目进行初始化。

namespace App
{
    public class Functions
    {
        public TaskbarItemInfo TaskbarItemInfo { get; private set; }

        public Functions() 
        {
            loadFunctions();
        }

        private void loadFunctions()
        {
            TaskbarItemInfo = new TaskbarItemInfo();
        }
    }
}

那么在初始化 LoginFunctions 对象后,您就不需要 LoginFunctions.loadFunctions(); 行代码。

You need to make the variable public.

namespace App
{
    public class Functions
    {
        public TaskbarItemInfo TaskbarItemInfo { get; private set; }

        public void loadFunctions()
        {
            TaskbarItemInfo = new TaskbarItemInfo();
        }
    }
}

EDIT: You could also do the initialization of the items in the constructor.

namespace App
{
    public class Functions
    {
        public TaskbarItemInfo TaskbarItemInfo { get; private set; }

        public Functions() 
        {
            loadFunctions();
        }

        private void loadFunctions()
        {
            TaskbarItemInfo = new TaskbarItemInfo();
        }
    }
}

Then you don't need the LoginFunctions.loadFunctions(); line of code after you initialize your LoginFunctions object.

星星的轨迹 2024-09-17 23:14:13

您可能希望将其作为属性来访问,以便在需要时生成私有静态成员。

namespace App
{
    public class Functions
    {
        private static TaskbarItemInfo _taskbarItemInfo;

        public static TaskbarItemInfo TaskBarItemInfoProperty
        {
            get{
               if (_taskbarItemInfo == null) 
               {
                  _taskbarItemInfo = new TaskbarItemInfo();
               }
               return _taskbarItemInfo;
            }
        }
    }

    public class Test
    {
        public void testFunction()
        {
           Functions.TaskBarItemInfoProperty.doSomething();
        }
    }
}

You probably want to access it as a property which generates a private static member when needed.

namespace App
{
    public class Functions
    {
        private static TaskbarItemInfo _taskbarItemInfo;

        public static TaskbarItemInfo TaskBarItemInfoProperty
        {
            get{
               if (_taskbarItemInfo == null) 
               {
                  _taskbarItemInfo = new TaskbarItemInfo();
               }
               return _taskbarItemInfo;
            }
        }
    }

    public class Test
    {
        public void testFunction()
        {
           Functions.TaskBarItemInfoProperty.doSomething();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文