在C#中无法使用结构数组

时间:2012-10-12 05:59:30

标签: c# .net wpf arrays struct

我很抱歉这个奇怪的标题,但我不知道如何表达它:)我最近转移到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;
        }

我在哪里弄错了? :(

2 个答案:

答案 0 :(得分:1)

mVoltageStruct是个人值; redhookChannels是一个数组。您无法将数组分配给值。但是,您可以在数组中查看:

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

但请注意,除非您有特定原因(P / Invoke等)使用struct,否则可能会考虑在那里使用某个类。可变结构通常会导致很多的混乱。 C#中的struct与C / C ++中的struct并不相同。

另请注意,行mVoltageStruct = redhookChannels[0]; 是复制操作;它并不意味着mVoltageStruct是第0个项的引用/指针。有 方法可以在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);
        }
    }
}

答案 1 :(得分:1)

您需要的是将mVoltageStruct声明为数组,而不是对象:

  VoltageBoardChannel[] mVoltageStruct;

然后你可以分配:

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