0

I am sorry for the weird title but I coulnt figure out how to express it :) I recently shifted to C# and currently I am working on structures. I am basically a C++ developer and in my c++ code I had done the folowing:

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;
}

I need to do this in my c# application. I tried it as follows but something is wrong:

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;
        }

Where Am I making a mistake??? :(

5
  • What are you trying to assign mVoltageStruct ? do you want to assign the first item of your array redhookChannels ? Commented Oct 12, 2012 at 6:02
  • I want to assign all values of redhookChannels to mVoltageStruct Commented Oct 12, 2012 at 6:07
  • @StonedJesus but what does that mean? For example: you have a variable that can hold a single "person", and a list of 6 "people"; how do you assign the values of all 6 "people" to the single "person" variable? It looks to me like m_voltageChannels is a pointer to the first item in the array, but that isn't copying anything. Commented Oct 12, 2012 at 6:08
  • well mVoltageStruct is a single item struct, and your redhookChannels is an array, in your C++ code you are assigning it to a pointer of the struct type, In C# you have define another array and make copy of it. Commented Oct 12, 2012 at 6:09
  • @Habib you don't have to do that at all; the correct next step depends a lot on what you want to do with it, and how you want to behave. Making a copy of the array will significantly change the behaviour, for example if we change values via the pointer, are the array contents updated? Commented Oct 12, 2012 at 6:11

2 Answers 2

1

mVoltageStruct is an individual value; redhookChannels is an array. You can't assign an array to a value. You can, however, peek inside the array:

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

Note, however, that unless you have specific reasons (P/Invoke etc) to use a struct, you might want to consider using a class there. Mutable structs usually cause a lot of confusion. struct in C# does not mean the same as struct in C/C++.

Note also that the line mVoltageStruct = redhookChannels[0]; is a copy operation; it does not mean that mVoltageStruct is a reference / pointer to the zeroth item. There are ways to do that in C#, but it would be a lot easier if you were using a class; then you would be only copying the reference-value.


Edit per comments:

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks :) I need to copy all items in mVoltageStruct. Hows that possible?
@StonedJesus why do you need to copy all the values? Is that really what the original code does? Where would you put them? (you haven't declared a second array, etc)
I need to copy all the channels to display in my view. I will be using channelname.available function later to make only true items visible in my view :)
@StonedJesus I disagree; since an array has fixed size, making an additional copy doesn't help you at all; it certainly won't help you include/exclude specific rows. And it is different to what the original code does/did.
Yes you are right. Then what's the ideal way of doing it? Can u post a sample Code to demonstrate using a class?
|
1

What you need is just to declare mVoltageStruct as Array, not an object:

  VoltageBoardChannel[] mVoltageStruct;

Then you can assign:

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

1 Comment

pedantic point; mVoltageStruct is at no point "an object"; it probably doesn't matter in regular discussion, though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.