使用另一个类的类方法

发布于 2024-09-25 04:14:04 字数 579 浏览 2 评论 0原文

我正在尝试使用另一个类中的类内方法。

namespace Crystal.Utilities
{
   public class Logging
   {
      public static void Log()
      {
          //dostuff
          Crystal.MainForm.general_log_add_item("Hello World");
      }
   }
}

namespace Crystal
{
   public partial class MainForm : Form
   { 
      public void general_log_add_item(string msg)
      {
         listBox1.Items.Add(msg);
      }
   }
} 

我希望能够从任何地方调用 Crystal.Utilities.Logging.Log() ,并且能够调用 Crystal.MainForm.general_log_add_item() 。但它不允许我,因为如果我将其设置为公共,那么我就看不到它,如果它是静态的,那么它就无法与我的列表框交互。

I am trying to use a method inside class, from another class.

namespace Crystal.Utilities
{
   public class Logging
   {
      public static void Log()
      {
          //dostuff
          Crystal.MainForm.general_log_add_item("Hello World");
      }
   }
}

namespace Crystal
{
   public partial class MainForm : Form
   { 
      public void general_log_add_item(string msg)
      {
         listBox1.Items.Add(msg);
      }
   }
} 

I want to be able to call Crystal.Utilities.Logging.Log() from anywhere, and that to be able to call Crystal.MainForm.general_log_add_item() . But It doesn't let me, because if I put it as public, then I can't see it, if it's static then It can't interact with my listbox.

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

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

发布评论

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

评论(4

尛丟丟 2024-10-02 04:14:04

这是错误的做法。您的类不应调用 UI,因为 UI 可能会发生变化。该类不应该知道也不关心 UI。相反,该类可以公开表单可以订阅的事件,并根据事件参数中包含的信息进行更新。

这是一个仓促拼凑的例子。

class Program
{
    static void Main()
    {
        Logger.OnLogging += Logger_OnLogging;
        Logger.Log();
        Logger.OnLogging -= Logger_OnLogging;
    }

    static void Logger_OnLogging(LoggingEventArgs e)
    {
        Trace.WriteLine(e.Message);
    }
}

public class Logger
{
    public delegate void LoggingEventHandler(LoggingEventArgs e);

    public static event LoggingEventHandler OnLogging;

    public static void Log()
    {
        // do stuff
        RaiseLoggingEvent("Data logged");
    }

    protected static void RaiseLoggingEvent(string message)
    {
        if (OnLogging != null)
            OnLogging(new LoggingEventArgs(message));
    }
}

public class LoggingEventArgs : EventArgs
{
    public LoggingEventArgs(string message)
    {
        this.Message = message;
    }

    public string Message { get; private set; }
}

This is a wrong approach. Your class should not call into the UI, as the UI could change. The class should not know nor care about the UI. Instead, the class could expose an event that the form could subscribe to, and update based upon the information contained within the event's arguments.

Here's a hastily thrown together example.

class Program
{
    static void Main()
    {
        Logger.OnLogging += Logger_OnLogging;
        Logger.Log();
        Logger.OnLogging -= Logger_OnLogging;
    }

    static void Logger_OnLogging(LoggingEventArgs e)
    {
        Trace.WriteLine(e.Message);
    }
}

public class Logger
{
    public delegate void LoggingEventHandler(LoggingEventArgs e);

    public static event LoggingEventHandler OnLogging;

    public static void Log()
    {
        // do stuff
        RaiseLoggingEvent("Data logged");
    }

    protected static void RaiseLoggingEvent(string message)
    {
        if (OnLogging != null)
            OnLogging(new LoggingEventArgs(message));
    }
}

public class LoggingEventArgs : EventArgs
{
    public LoggingEventArgs(string message)
    {
        this.Message = message;
    }

    public string Message { get; private set; }
}
各自安好 2024-10-02 04:14:04

不要将其实现为静态方法,而是尝试将其实现为单例。将实例设置为全局范围并限制为一个实例,而不将所有内容设为静态(因此无法用作实例)是一种常见的技巧。

Instead of implementing it as a static method, try implementing as a singleton. It's a common trick to make an instance global in scope, and restrict to one instance, without making everything static (and thus unable to be used as an instance).

烟凡古楼 2024-10-02 04:14:04

你必须明白,窗口不是静态的,他有一个实例,这就是为什么该方法不能是静态的,
你可以使用
Application.Windows 到达此实例并调用 add 方法。

或者您可以在另一个类的构造函数中注册该窗口,该类将协调日志记录和窗口。

如果您不明白请告诉我,我会尽力说得更清楚

You have to understand that the window is not static, there is one instance of him, thats why the method cant be static,
you can use
Application.Windows to reach this instance and call the add method.

or you can register the window in his constructor on another class that will mediate the Logging and the window.

If you don't understand tell me and I'll try to be more clear

丶视觉 2024-10-02 04:14:04

当您将一个方法声明为“静态”时,您是在说它不依赖于它所在类的特定实例。
例如,如果您有一个名为“chair”的类,并且想要计算有多少把椅子,则可以使用静态字段和静态方法来返回该字段的值。
所有椅子的数量与特定椅子无关。
在您的情况下,您想要添加一个静态方法来将项目添加到表单的特定实例。这是不可能的,也没有意义。
如果要向列表框添加项目,则必须通过公共方法。
所以基本上我想说的是 - 重新思考你想要做什么,对于为什么你没有成功做到这一点有一个很好的解释。

When you declare a method as "static" you're saying that it's not dependent upon a specific instance of the class it's in.
For example if you have a class named "chair" and you want to count how many chairs there are, you'll do that with a static field, and a static method to return that field's value.
The count of all chairs is not related to a specific chair.
In your case you want to add a static method to add an item to a specific instance of a Form. That's impossible and doesn't make sense.
If you want to add an item to a listBox, it must be through a public method.
So basically what I'm saying is - rethink what you're trying to do, there's a good explanation as to why you're not succeeding in doing that.

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