Zum Inhalt springen

[c# .net] Struct + List Problem


Empfohlene Beiträge

Geschrieben

Hi!

Ich habe folgendes Problem:

Ich erstelle ein Struct:


    public struct CommandStruct

    {

        public string aCategory; // Eine Category kann mehrere Commands haben.

        public string aCommand;  // Ein Command kann nur eine Category haben. (Jedes Command ist eingigartig)

        public string aShortcut; // Ein Shortcut kann nur zu einem Command gehören.

        public string aToolTip;  // Mehrere Commands können einen gleichen ToolTip haben.

        public image aImage;     // Mehrere Commands können sich ein Image teilen.

    }

Nun mache ich mir eine Liste daraus:

List<CommandStruct> m_CommandStructList = new List<CommandStruct>();

Ich möchte nun zum Beispiel folgendes wissen:

Ich habe eine Category und das dazugehörige Command. Nun möchte ich den ShortCut dazu wissen.

Anderes Beispiel:

Ich habe eine Category und möchte die jeweiligen Commands dazu wissen.

Kann ich das mit der Liste machen?

Wenn nicht: Gibt es einen anderen (besseren) Weg?

Habe es auch mal mit mehreren Dictionarys probiert. Das hat aber den Nachteil das man immer nur einen Key und ein Value hat.

Sorry falls es ein wenig kompliziert geschrieben ist. :hells:

Gruß Nightfall

Geschrieben
class Program
{
public struct CommandStruct
{
public string aCategory; // Eine Category kann mehrere Commands haben.
public string aCommand; // Ein Command kann nur eine Category haben. (Jedes Command ist eingigartig)
public string aShortcut; // Ein Shortcut kann nur zu einem Command gehören.
public string aToolTip; // Mehrere Commands können einen gleichen ToolTip haben.
public string aImage; // Mehrere Commands können sich ein Image teilen.
}


static void Main(string[] args)
{
List<CommandStruct> m_CommandStructList = new List<CommandStruct>();
m_CommandStructList.Add(new CommandStruct() { aCategory = "Cat1", aCommand = "Com1", aShortcut = "Strg+A", aImage = "Image1", aToolTip = "fdbgio" });
m_CommandStructList.Add(new CommandStruct() { aCategory = "Cat1", aCommand = "Com2", aShortcut = "Strg+B", aImage = "Image2", aToolTip = "fdbgfhrjio" });
m_CommandStructList.Add(new CommandStruct() { aCategory = "Cat2", aCommand = "Com3", aShortcut = "Strg+C", aImage = "Image1", aToolTip = "trh" });
m_CommandStructList.Add(new CommandStruct() { aCategory = "Cat2", aCommand = "Com4", aShortcut = "Strg+D", aImage = "Image2", aToolTip = "fdbgio" });
m_CommandStructList.Add(new CommandStruct() { aCategory = "Cat2", aCommand = "Com5", aShortcut = "Strg+E", aImage = "Image1", aToolTip = "fdbfjjgio" });

var shortcut = from com in m_CommandStructList
where com.aCategory == "Cat1" && com.aCommand == "Com2"
select com.aShortcut;
Console.WriteLine(shortcut.First());


var commands = from com in m_CommandStructList
where com.aCategory == "Cat2"
select com;

foreach (CommandStruct cs in commands)
{
Console.WriteLine(cs.aCommand);
}
}
}[/PHP]

Geschrieben

Habe es auch mal mit mehreren Dictionarys probiert. Das hat aber den Nachteil das man immer nur einen Key und ein Value hat.

List<CommandStruct> als Value?! :floet:

Aber ich denke das Linq-Beispiel vom Piraten ist da performanter, einfacherer usw.

Erstelle ein Benutzerkonto oder melde Dich an, um zu kommentieren

Du musst ein Benutzerkonto haben, um einen Kommentar verfassen zu können

Benutzerkonto erstellen

Neues Benutzerkonto für unsere Community erstellen. Es ist einfach!

Neues Benutzerkonto erstellen

Anmelden

Du hast bereits ein Benutzerkonto? Melde Dich hier an.

Jetzt anmelden

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...