0

New to c# and object oriented I am creating predefined data held in structs (that will not change). and I want to add a reference to those structs in either an array or a list The thought process is to then loop through an array of these structs and if the filter criteria is met, to then add an element of the struct "Displayname" to a listbox Example I am using here is for an array

    public struct ListboxData
    {
        public string Category;
        public string Displayname;
        public string ID;
        public List<string> IDTags;
        public string FaultTxt;
        public string Suggestion;
        public string Comments;
        public List<string> MoreQuestions;
    }

    public ListboxData COOLING = new ListboxData
    {
        Category = "CATEGORY",
        Displayname = "COOLING",
        ID = "CAT_COOL",
        IDTags = { ""},
        FaultTxt = "",
        Suggestion = "",
        Comments = "",
        MoreQuestions = { ""}
    };

Having now created multiple instances of the ListboxData structs I then want to add a selection of some of these structs to an array. In this case the Struct reference COOLING

Example of what I am trying to achieve

public ListboxData[] ARRAYofCATEGORIES =
{
    COOLING
};

How can I achieve this ?

Terox
  • 31
  • 5
  • Are you looking for "array initializer" syntax (something like https://stackoverflow.com/questions/17322250/c-sharp-syntax-to-initialize-custom-class-objects-through-constructor-params-in) or more https://www.bing.com/search?q=c%23+create+array+initialize? Note that using `struct` for data you have is strange (https://stackoverflow.com/questions/521298/when-to-use-struct) and likely cause you a lot of pain... – Alexei Levenkov Oct 22 '17 at 17:46
  • It is not clear at all what you actually want to accomplish here. Besides the fact that your question is simply vague and overly broad, it also doesn't make sense to talk about a _"reference to those structs"_ -- you can only have a reference to reference types, i.e. classes, and not structs -- and you haven't offered any example of what you've actually _tried_ with any sort of explanation of what _specifically_ you are having trouble accomplishing. – Peter Duniho Oct 22 '17 at 19:16

2 Answers2

0

I re-wrote your example, working. You can find a live test here

Below is the code. I used a List() since you mentioned in your question that it was acceptable, and I am more used to it.

I don't know what you were doing wrong since you didn't show us a full example. You can read the code bellow, tell me if there is something you don't understand/want and i'll update my answer to fit your needs :-)

Here is the code

public struct ListboxData { private readonly string _category;

    private readonly string _displayname;
    private readonly string _id;
    private readonly List<string> _idTags;
    private readonly string _faultTxt;
    private readonly string _suggestion;
    private readonly string _comments;
    private readonly List<string> _moreQuestions;

    public ListboxData(string category, string displayname, string id, List<string> idTags, string faultTxt, string suggestion, string comments, List<string> moreQuestions)
    {
        _category = category;
        _displayname = displayname;
        _id = id;
        _idTags = idTags;
        _faultTxt = faultTxt;
        _suggestion = suggestion;
        _comments = comments;
        _moreQuestions = moreQuestions;
    }

    public string Category
    {
        get { return _category; }
    }

    public string Displayname
    {
        get { return _displayname; }
    }


    public string Id
    {
        get { return _id; }
    }


    public List<string> IdTags
    {
        get { return _idTags; }
    }


    public string FaultTxt
    {
        get { return _faultTxt; }
    }


    public string Suggestion
    {
        get { return _suggestion; }
    }


    public string Comments
    {
        get { return _comments; }
    }


    public List<string> MoreQuestions
    {
        get { return _moreQuestions; }
    }
}

public class Test
{
    public List<ListboxData> Categories = new List<ListboxData>();
    public CoolingListTest()
    {            
        Categories.Add(new ListboxData(
            category: "CATEGORY",
            displayname: "COOLING",
            id: "CAT_COOL",
            idTags: new List<String> { "" },
            faultTxt: "",
            suggestion: "",
            comments: "",
            moreQuestions: new List<String> { "" }
         ));
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var test = new Test(); 

        Console.WriteLine(test.Categories.First().Displayname);
    }
}
Mathieu VIALES
  • 4,526
  • 3
  • 31
  • 48
  • 1
    Mutable structs are evil. – John Alexiou Oct 22 '17 at 18:47
  • Thank you for teaching about the evilness of mutable stucts. I learned, and updated my answer. It *should* be a bit less evil now. If you don't mind taking an other look to check, i'd be very grateful :-) – Mathieu VIALES Oct 22 '17 at 19:01
  • Now I understand that a bit better, thanks for the rework. I was trying to write it in such a way that the individual structs and there is going to be a lot of them were a lot more condensed to read than the example you just posted. My hope was to add the construct references to a list, like for example – Terox Oct 22 '17 at 19:19
  • Maybe you should use a database to store this data ? Or maybe a file ? Storing it in the code directly is a bit o an odd thing to do :-) If your question is answered you can click the little green "tick" so other people know that is their question is similar, an answer can be found here. This will also give you and then answerer some reputation in case you care about that :-) – Mathieu VIALES Oct 22 '17 at 19:21
  • Now I understand that a bit better, thanks for the rework. Thank you very much for your time – Terox Oct 22 '17 at 19:26
0

1st rule to go by is that you have to design immutability into struct. Especially if the fields are reference types (string, array, user classes).

I will demonstrate below a simplified example of populating a struct with various values, then creating a collection (array or list) of the struct and then finally slicing one of the values as an array.

See live code here this code:

public struct ListboxData
{        
    public ListboxData(
        int id,
        string category,
        string name,
        params string[] tags) : this()
    {
        this.ID = id;
        this.Category = category;
        this.Name = name;
        this.Tags = new List<string>();
        this.Tags.AddRange(tags);
    }      
    public int ID { get; private set; }
    public string Category { get;  private set;}
    public string Name { get;  private set;}
    public List<string> Tags { get;  private set;}

    public string[] ToStringArray() 
    {
        return new[] {
            ID.ToString(),
            Category,
            Name,
            string.Join(";", Tags)
        };
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        //Your code goes here
        Console.WriteLine("Hello, world!");

        var list = new List<ListboxData>();
        list.Add(new ListboxData(1001, "COOLING", "Cooling Intro"));
        list.Add(new ListboxData(1002, "COOLING", "Cooling Adanced"));
        list.Add(new ListboxData(1003, "COOLING", "Cooling Tests"));
        list.Add(new ListboxData(1004, "HEATING", "Heating Intro", "tag1", "tag2"));

        // Get all cooling ID's
        int[] ids = list.Where((item)=> item.Category=="COOLING").Select( (item)=> item.ID).ToArray();
        // { 1001, 1002, 1003 }

        foreach(var item in ids)
        {
            Console.WriteLine(item);
        }

        // Get all items that contain "Intro" in the name
        ListboxData[] intro = list.Where((item)=>item.Name.Contains("Intro")).ToArray();
        // { list[0], list[3] }
        foreach(var item in intro)
        {
            Console.WriteLine(item.Name);
        }
    }
}

Newer versions of have simplifications for the above code. You can omit the private set; statements in the properties.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133