These are chat archives for CoraleStudios/Colore

22nd
Jan 2016
Roxas Keyheart
@roxaskeyheart
Jan 22 2016 14:05
Hey sorry I'm still rather new to C#, can i ask another (probably) stupid question? lol
Brandon Scott
@brandonscott
Jan 22 2016 14:05
Hey.
Ask away, nothing's stupid!
Roxas Keyheart
@roxaskeyheart
Jan 22 2016 14:06
Thanks >.<

So I've got this 2D array (or i believe its called an array of objects with anonymous types) which I cycle through using:

var groupedItems = new[] {
new { Group = "General", Value = "Movement", Display = "Movement" },
new { Group = "General", Value = "Modifier Command", Display = "Modifier Command" },
new { Group = "General", Value = "Crafting/Gathering", Display = "Crafting/Gathering" },
new { Group = "General", Value = "Macro", Display = "Macro" },
new { Group = "General", Value = "Static", Display = "Static" },
new { Group = "General", Value = "Other", Display = "Other" },
};

That works fine, the problem is I need to add more elements to this using a for each loop

So i was thinking along the lines of:

foreach (KeyValuePair<string, string> a in actions)
{
int c = groupedItems.Count();
groupedItems[c + 1] = new { Group = "Actions", Value = a.Key, Display = a.Key };
}

however this doesn't seem to work at all, and I can't really work out how to get around this (avoiding lists and dictionaries if possible)
Is there anyway to loop inside the array deceleration?
Roxas Keyheart
@roxaskeyheart
Jan 22 2016 14:30
Okay I might have figured a solution out
static List<T> CreateListFromSingle<T>(T value)
{
var list = new List<T>();
list.Add(value);
return list;
}
var groupedItemsA = CreateListFromSingle(new { Group = "General", Value = "Movement", Display = "Movement" });
groupedItemsA.Add(new { Group = "General", Value = "Modifier Command", Display = "Modifier Command" });
then loop using Add() after..
Roxas Keyheart
@roxaskeyheart
Jan 22 2016 14:43
Alright I sorted it out using that, sorry for bothering you.. >.<;
Adrian
@WolfspiritM
Jan 22 2016 20:05
@roxaskeyheart: Even so it's solved: The Problem you were facing is: Arrays always have a fixed size. The array you defined above has the size of 6 and you can't add more values without using Array.Resize. Technically Resize internally is doing nothing else then creating a new array with the new size and copying everything over and that hits Performance. A list shouldn't be avoided in that case as it's actually made exactly for that. (But you already moved to using a list...just wanted to mention as you wanted to avoid them). Another maybe more easier way to avoid the extra method is: var test = new[] { new { Group = "Actions", Value = "", Display = "" } }.ToList(); and you'd get a List<AnonymouseType...> ;-)......BUT if you ask me: It'd be a much better approach to use a Class in your case. It's easier to read, can be used outside of your scope and can be compared easier (e.g. "if(something is WhateverThisIs)..."). And it is more the C# like way in my personal opinion. Especially for something you did it would be way easier to understand if you just use "new WhateverThisIs("General","Movement","Movement")".