Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中跨多个函数使用多个变量#_C#_Variables - Fatal编程技术网

C# 在C语言中跨多个函数使用多个变量#

C# 在C语言中跨多个函数使用多个变量#,c#,variables,C#,Variables,我正试图编写一个程序来与I/O卡接口。硬件提供的文档是C语言的,我以前从未使用过C语言。所以,如果这个问题看起来很基本,请原谅 使用制造商的函数要求每个I/O引脚至少有3个变量,共有55个引脚。这意味着有很多定义的变量。当我编写代码时,我发现它非常丑陋。我在这里和那里重用代码片段,这些代码片段很容易成为函数。但是把它们变成一个函数,我就失去了访问大量变量的能力。有什么简单的方法可以做到这一点吗 这是我目前正在使用的一个简短版本。丢失的碎片看起来是一样的。我正在使用VS2012。提前谢谢 usin

我正试图编写一个程序来与I/O卡接口。硬件提供的文档是C语言的,我以前从未使用过C语言。所以,如果这个问题看起来很基本,请原谅

使用制造商的函数要求每个I/O引脚至少有3个变量,共有55个引脚。这意味着有很多定义的变量。当我编写代码时,我发现它非常丑陋。我在这里和那里重用代码片段,这些代码片段很容易成为函数。但是把它们变成一个函数,我就失去了访问大量变量的能力。有什么简单的方法可以做到这一点吗

这是我目前正在使用的一个简短版本。丢失的碎片看起来是一样的。我正在使用VS2012。提前谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

/* Tool Changer Controller Program
 * In theory, this program will run the tool changer through a full cycle.
 * This includes:     removing the current tool, 
 *                    placing the current tool in its appropriate location on the carousel,
 *                    moving the carousel to the correct location of the new tool,
 *                    placing the new tool in the spindle so it is ready for use.
 */
namespace Tool_Changer
{

class Program
{
    static void Main(string[] args)
    {
        // Create PoKeys55 device object
        PoKeysDevice_DLL.PoKeysDevice device = new PoKeysDevice_DLL.PoKeysDevice();

        // Define all pins and correlate to correct pin
        // Pin: Pin #, Status: pass/fail returned, State: On/Off status
        // Swivel
        byte SwivelCCWA_Pin = 0; //Set*
        bool SwivelCCWA_Status = false;
        bool SwivelCCWA_State = false;

        byte SwivelCWA_Pin = 2; //Set*
        bool SwivelCWA_Status = false;
        bool SwivelCWA_State = false;

        // Telescope
        byte TeleExtA_Pin = 4; //Set*
        bool TeleExtA_Status = false;
        bool TeleExtA_State = false;

        byte TeleRetA_Pin = 6; //Set*
        bool TeleRetA_Status = false;
        bool TeleRetA_State = false;
        // Grabber
        byte GrabberOpen_Pin = 12; //Set*
        bool GrabberOpen_Status = false;
        bool GrabberOpen_State = false;

        byte ToolPresent_Pin = 13; //Get
        bool ToolPresent_Status = false;
        bool ToolPresent_State = false;

        // Begin Tool Change procedure.
        SwivelCWA_State = false;
        SwivelCCWA_State = true;
        SwivelCWA_Status = device.SetOutput(SwivelCWA_Pin, SwivelCWA_State); // Turn off CW
        SwivelCCWA_Status = device.SetOutput(SwivelCCWA_Pin, SwivelCCWA_State); // Turn on CCW
        Thread.Sleep(1000);

        GrabberOpen_State = true;
        GrabberOpen_Status = device.SetOutput(GrabberOpen_Pin, GrabberOpen_State); // Open Grabber

        TeleRetA_State = false;
        TeleExtA_State = true;
        TeleRetA_Status = device.SetOutput(TeleRetA_Pin, TeleRetA_State); // Turn off retract
        TeleExtA_Status = device.SetOutput(TeleExtA_Pin, TeleExtA_State); // Turn on extend
        Thread.Sleep(1000);

如果使用结构来保存公共变量,代码将变得更漂亮、更紧凑。例如:

public struct Item
{
    public Item(byte pin, bool status, bool state)
    {
        Pin = pin;
        Status = status;
        State = state;
    }

    public byte Pin;
    public bool Status;
    public bool State;
}
然后,不要像这样单独设置每个变量:

byte SwivelCCWA_Pin = 0; //Set*
bool SwivelCCWA_Status = false;
bool SwivelCCWA_State = false;
Item swivelCCWA = new Item(0, false, false);
您将只能使用以下一行代码:

byte SwivelCCWA_Pin = 0; //Set*
bool SwivelCCWA_Status = false;
bool SwivelCCWA_State = false;
Item swivelCCWA = new Item(0, false, false);

创建一个字典,其中包含每个管脚的所有输出设置:

 Dictionary<int, bool> pinStates = new Dictionary<int, bool> 
 {
     {0, false},
     {2, true},
     {12, false},
     {13, false}
 };

将变量设置为类变量,然后在类中使用这些变量的函数(这里的类是program)。为什么不使用专用类呢?您也可以将一些属性设置为“属性包”,为不同的管脚设置与同一事物相关的变量集合。@GrantThomas-在这种情况下,结构可能会更好。@Ramhound我考虑过这一点,但我想留下简短的评论,这将是这个想法的下一个扩展——虽然可能不会是结构类型的50多个属性,但它们可能是集合中项目的类型。当我调用例如device.SetOutput时,我将如何使用它?SwivelCCWA.Status=device.SetOutput(SwivelCCWA.Pin,SwivelCCWA.State)?是的,完全正确,甚至更好-您可以向接受设备的结构添加一个方法,并使用Pin和状态更新状态。然后调用将如下所示:
swivelCCWA.UpdateStatus(设备)