无法从以前的 Windows 窗体访问类方法 - C#

发布于 2024-08-25 22:09:17 字数 2514 浏览 5 评论 0原文

我仍在编写一个应用程序,我需要在 30 分钟内每分钟测试一些设备。使用设置为每 60 秒启动一次的计时器并执行事件处理程序中所需的操作是有意义的。

但是,我需要应用程序等待 30 分钟,直到我完成计时器,因为以下代码会更改我尝试监控的设备的状态。

我显然不想使用任何形式的循环来做到这一点。我考虑使用另一个窗口窗体,因为我还显示进度,这将简单地启动计时器并等待其完成。

我遇到的问题是我使用设备类,并且似乎无法从第二个(实际上是第三个 - 见下文)窗口窗体访问设备类中的方法。

我有一个初始窗口窗体,我从用户那里获取输入,然后调用第二个窗口窗体,它计算出需要完成哪些测试以及需要使用哪些设备类,然后我想调用第三个窗口窗体处理计时器。

我将拥有最多 6-7 个设备类,因此只想在实际需要它们时从第二种形式实例化它们。

我是否应该将此逻辑放入第一个窗口窗体(程序类??)?

我是否仍然会遇到无法从那里访问设备类方法的问题?

无论如何,也许有人知道更好的方法来每分钟进行检查,而无需执行其余代码(并更改设备的状态),或者我应该如何访问应用程序中的方法?

您好,

以下是“调用”形式 -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace TestCall

{
    public partial class Form1 : Form
    {
        NDTClass NDT = new NDTClass();

        public Form1()
        {
            InitializeComponent();

            NDT.NDTOpen();
            NDT.NDT1_CMD1();
            (new Form2()).ShowDialog();
            NDT.NDTClose();

        }

        public class NDTClass
        {
            public static double SWVolt = 0.5;
            public static string Rxstring = "";
            public SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);

            public string NDTOpen()
            {
                port.Open();
                port.Write("CURRENT ?\r\n");
                return Rxstring;
            }

            public void NDTClose()
            {
                port.Close();
            }

            public void NDT1_CMD1()
            {
                port.Write("DUAL MODE\r\n");
            }
        }
    }
}

以下是被调用形式 -

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

namespace TestCall
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Form1.NDTClass.SWVolt = 99;
            NDT.NDT1_CMD1();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

被调用形式中的 NDT.NDT1_CMD1() 在构建中给出了以下错误 -

Error   1   The name 'NDT' does not exist in the current context    

在 NDTClass 中使用变量 SWVolt 工作正常,但无法在类中使用该方法。

我做错了什么??

谢谢,乔治。 (我希望格式也正确)。

I am writing an app, still, where I need to test some devices every minute for 30 minutes. It made sense to use a timer set to kick off every 60 secs and do whats required in the event handler.

However, I need the app to wait for the 30 mins until I have finished with the timer since the following code alters the state of the devices I am trying to monitor.

I obviously don't want to use any form of loop to do this. I thought of using another windows form, since I also display the progress, which will simply kick off the timer and wait until its complete.

The problem I am having with this is that I use a device Class and cant seem to get access to the methods in the device class from the 2nd (3rd actually - see below) windows form.

I have an initial windows form where I get input from the user, then call the 2nd windows form where it work out which tests need to be done and which device classes need to be used, and then I want to call the 3rd windows form to handle the timer.

I will have up to 6-7 device classes and so wanted to only instantiate them when actually requiring them, from the 2nd form.

Should I have put this logic into the 1st windows form (program class ??) ?

Would I not still have the problem of not being able to access device class methods from there too ?

Anyway, perhaps someone knows of a better way to do the checks every minute without the rest of the code executing (and changing the status of the devices) or how I should be accessing the methods in the app ??

Hi,

The following is the "calling" form -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace TestCall

{
    public partial class Form1 : Form
    {
        NDTClass NDT = new NDTClass();

        public Form1()
        {
            InitializeComponent();

            NDT.NDTOpen();
            NDT.NDT1_CMD1();
            (new Form2()).ShowDialog();
            NDT.NDTClose();

        }

        public class NDTClass
        {
            public static double SWVolt = 0.5;
            public static string Rxstring = "";
            public SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);

            public string NDTOpen()
            {
                port.Open();
                port.Write("CURRENT ?\r\n");
                return Rxstring;
            }

            public void NDTClose()
            {
                port.Close();
            }

            public void NDT1_CMD1()
            {
                port.Write("DUAL MODE\r\n");
            }
        }
    }
}

The following is the called form -

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

namespace TestCall
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Form1.NDTClass.SWVolt = 99;
            NDT.NDT1_CMD1();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

The NDT.NDT1_CMD1() in the called form gives me the following error in the build -

Error   1   The name 'NDT' does not exist in the current context    

The use of the variable SWVolt within the NDTClass works fine but the use of the method in the class is not possible.

What have I done wrong ??

Thanks, George. (I hope the formatting comes out corrrectly too).

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

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

发布评论

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

评论(1

旧伤慢歌 2024-09-01 22:09:17

Form1.NDTClass.SWVolt = 99; 之所以有效,是因为您引用了 Form1.NDTClass 类中的公共静态字段。它不依赖于任何实例。但是对于“NDT.NDT1_CMD1();”,这不起作用,因为:

  1. NDT1_CMD1() 是一个实例方法,您需要该类的实例来调用它。
  2. Form2 中没有名为 NDT 的类的实例。 Form1 包含一个名为 NDT 的私有字段,但无法从 Form2 访问该字段。您需要在 Form2 中声明 Form1.NDTClass NDT = new Form1.NDTClass()

我认为您需要确保您理解 变量范围

让我尝试另一种解释:
SWVolt 作为公共静态字段,具有全局范围。可以从应用程序中的任何位置访问它。 NDT 作为私有实例字段,其范围为 Form1 的单个实例。也就是说,只能从 Form1 实例内部访问它。任何不属于Form1的代码都看不到它,当然也包括Form2中的代码。

Form1.NDTClass.SWVolt = 99; works because you are referencing a public static field in the Form1.NDTClass class. It is not tied to any instance. However for `NDT.NDT1_CMD1();, that doesn't work because:

  1. NDT1_CMD1() is an instance method and you need an instance of that class to call it.
  2. There is no instance of your class named NDT in Form2. Form1 contains a private field named NDT, but that is not accessible from Form2. You need to declare Form1.NDTClass NDT = new Form1.NDTClass() in Form2.

I think you need to make sure you understand variable scope.

Let me attempt an alternate explanation:
SWVolt as a public static field, has global scope. It is accessible from anywhere in the application. NDT as a private instance field has the scope of a single instance of Form1. That is it is only accessible from inside an instance of Form1. Any code that is not part of Form1 cannot see it, which of course includes the code in Form2.

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