0

我很抱歉这个奇怪的标题,但我无法弄清楚如何表达它:) 我最近转向 C#,目前我正在研究结构。我基本上是一名 C++ 开发人员,在我的 C++ 代码中,我完成了以下操作:

typedef struct 
{
String ChannelName;
bool available;
} Voltage_Channel;

Voltage_Channel *m_voltageChannels;

Voltage_Channel redhookChannels[6] = {
{"", false},
{"VDD_IO_AUD",  true},
{"VDD_CODEC_AUD",true},
{"VDD_DAL_AUD", true},
{"VDD_DPD_AUD",  true},
{"VDD_PLL_AUD", true}   
};

if(m_boardName->compareIgnoreCase("S1010012") == 0) //m_BoardName is string
{   
    m_voltageChannels = redhookChannels;
}

我需要在我的 c# 应用程序中执行此操作。我尝试如下但有问题:

struct VoltageBoardChannel
    {
        public string ChannelName;
        public bool available;            
    };

VoltageBoardChannel[] mVoltageStruct; 

VoltageBoardChannel[] redhookChannels = new VoltageBoardChannel[6]
    {
        new VoltageBoardChannel() { ChannelName = "", available = false},
        new VoltageBoardChannel() { ChannelName = "VDD_IO_AUD", available = true},
        new VoltageBoardChannel() { ChannelName = "VDD_CODEC_AUD", available = true},
        new VoltageBoardChannel() { ChannelName = "VDD_DAL_AUD", available = true},
        new VoltageBoardChannel() { ChannelName = "VDD_DPD_AUD", available = true},
        new VoltageBoardChannel() { ChannelName = "VDD_PLL_AUD", available = true}            
    };

string redhookboardname = "S1010012";
string redhookboardnameCase = "s1010012";

if (redhookboardnameCase.Equals(redhookboardname, stringComparison.InvariantCultureIgnoreCase))
        {
            mVoltageStruct = redhookChannels;
        }

我在哪里犯错???:(

4

2 回答 2

1

mVoltageStruct是个人价值;redhookChannels是一个数组。您不能将数组分配给值。但是,您可以查看数组内部:

mVoltageStruct = redhookChannels[0]; // copy the item with index 0, zero-based

但是请注意,除非您有特定的原因(P/Invoke 等)使用 a struct,否则您可能需要考虑在那里使用一个类。可变结构通常会引起很多混乱。structC# 中的含义与structC/C++ 中的含义不同。

另请注意,该行mVoltageStruct = redhookChannels[0]; 是复制操作;这并不意味着它mVoltageStruct是指向第零项的引用/指针。在 C# 中有一些class方法可以做到这一点,但如果你使用;会容易得多。那么你只会复制参考值。


根据评论编辑:

using System;
using System.Collections.Generic;
class VoltageBoardChannel
{
    public string ChannelName { get; set; }
    public bool IsAvailable { get; set; }
}
static class Program {
    static void Main()
    {
        List<VoltageBoardChannel> selectedChannels = null;

        List <VoltageBoardChannel> redhookChannels = new List<VoltageBoardChannel>
        {
            new VoltageBoardChannel { ChannelName = "", IsAvailable = false},
            new VoltageBoardChannel { ChannelName = "VDD_IO_AUD", IsAvailable = true},
            new VoltageBoardChannel { ChannelName = "VDD_CODEC_AUD", IsAvailable = true},
            new VoltageBoardChannel { ChannelName = "VDD_DAL_AUD", IsAvailable = true},
            new VoltageBoardChannel { ChannelName = "VDD_DPD_AUD", IsAvailable = true},
            new VoltageBoardChannel { ChannelName = "VDD_PLL_AUD", IsAvailable = true}            
        };

        string redhookboardname = "S1010012";
        string redhookboardnameCase = "s1010012";

        // string.Equals(a,b,...) rather than a.Equals(b, ...) avoids
        // potential issues when "a" is null
        if (string.Equals(redhookboardnameCase, redhookboardname,
            StringComparison.InvariantCultureIgnoreCase))
        {
            // set selectedChannels to the **same** list:
            selectedChannels = redhookChannels;

            // or if we wanted a filtered list (same VoltageBoardChannel
            // objects, but a different list instance)
            selectedChannels = redhookChannels.FindAll(x => x.IsAvailable);
        }
    }
}
于 2012-10-12T06:04:07.257 回答
1

您需要的只是声明mVoltageStructArray,而不是对象:

  VoltageBoardChannel[] mVoltageStruct;

然后你可以分配:

 if (redhookboardnameCase.Equals(redhookboardname, StringComparison.InvariantCultureIgnoreCase))
 {
       mVoltageStruct = redhookChannels;
 }
于 2012-10-12T06:07:17.767 回答