简体   繁体   English

为什么没有看到我的新对象? C#

[英]Why isn't my new object being seen? C#

I am getting ready to take a class at a college on C#. 我准备在C#的大学里上课。 I have been reading up on it a lot and decided to start a fun project. 我已经阅读了很多,决定开始一个有趣的项目。

Here is what my project consists of: 这是我的项目组成的:

  • Main Control Form 主控制表
  • Configuration Form 配置表
  • Arduino Arduino的

Program.cs calls Configuration.cs at start. Program.cs在开始时调用Configuration.cs This is where pin modes for the Arduino are set and where a timer is set. 在此处设置Arduino的引脚模式并设置计时器。 When I set these values, they get sent to MainControl.cs . 当我设置这些值时,它们将被发送到MainControl.cs When I hit the "Save" button in Configuration.cs, a MainControl.cs object is created [[I am correct in that?]] 当我在Configuration.cs中单击“保存”按钮时,将创建一个MainControl.cs对象[[我说的对吗?]

All of those values that were sent by Configuration.cs had corresponding setters that set private static variables in MainControl.cs [[ I don't really know if that is the preferred way, I am most definetly open to any suggestion anyone has]] Configuration.cs发送的所有这些值都有相应的设置程序,这些设置程序在MainControl.cs中设置了私有静态变量[[我真的不知道这是否是首选方法,我绝对可以接受任何人提出的任何建议]]

MainControl.cs uses its default constructor, and this constructor calls a method that creates an arduino object from one of the private variables (serialPort) [[ Using this Arduino class Firmata.NET ]] MainControl.cs使用其默认构造函数,该构造函数调用一个方法,该方法从一个私有变量(serialPort)中创建一个arduino对象[[使用此Arduino类Firmata.NET ]]

When the arduino object is created, I know (I guess I do) because the form takes a few seconds to come up (As opposed to not using serial port) My problem is this: 创建arduino对象时,我知道(我想是的),因为表单需要几秒钟的时间(而不是不使用串行端口),我的问题是:

I do not understand why nothing can see the object 我不明白为什么什么都看不到物体

I have been very wordy, I apologize if I wasn't concise. 我一直很罗word,如果我不简洁,我深表歉意。 Here is the code: 这是代码:

public partial class CMainControl : Form
{
       private static string serialPort;
        public CMainControl()
    {
        InitializeComponent();
        createArduino();
        updateConfig();  // Change label values to values set in configuration
    }

    private void createArduino()
    {
        Arduino arduino = new Arduino(serialPort);
    }

In Configuration.cs , when I set the serial port through a combobox, the value is sent to MainControl.cs just fine. Configuration.cs中 ,当我通过组合框设置串行端口时,该值发送到MainControl.cs就好了。

Here is the error I get: 这是我得到的错误:

Error 1 The name 'arduino' does not exist in the current context C:\\Programming\\Visual Studio\\Workhead Demo\\Workhead Demo\\CMainControl.cs 94 13 Workhead Demo 错误1名称'arduino'在当前上下文中不存在C:\\ Programming \\ Visual Studio \\ Workhead Demo \\ Workhead Demo \\ CMainControl.cs 94 13 Workhead演示

Please let me know if anyone can help and/or offer pointers, and please let me know if I didn't post or format anything correctly. 请让我知道是否有人可以帮助和/或提供指示,并且如果我没有正确发布或格式化任何内容,请让我知道。

Thank you very much :) 非常感谢你 :)

Try defining the Arduino variable as field in the CMainControl class. 尝试将Arduino变量定义为CMainControl类中的字段。

public partial class CMainControl : Form
{
   private Arduino arduino;
   private static string serialPort;

    private void createArduino()
    {
        arduino = new Arduino(serialPort);
    }

我正在猜测,因为不确定我是否会完全遵循,但是我猜测是因为您的arduino对象在create方法中本地声明,并且该方法之外的任何人都无法访问。

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

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