C
C#17mo ago
Dawnbomb

❔ ✅ Help with searching a class of classes for something?

So, i have a StartingClass, it has a list of classes, and that list has a list of classes, etc. Eventually it gets to EntryClass. How do i search StartingClass to find the EntryClass, that THAT entryclass's string name is "Dog" and then put THAT entryclass's string something into a textbox. so like, DebugTextbox.Text = TargetEntry.Something; but yeah, how do i search StartingClass to find the correct EntryClass and set that EntryClass as the TargetClass?
3 Replies
Not Mark
Not Mark17mo ago
Assuming you have this sort of structure:
public EntryClass
{
public string Name { get; set; }
public string SomethingSpecial { get; set; }
}

public MiddleClass
{
public List<EntryClass> EntryClasses { get; set; }
}

public StartingClass
{
public List<StartingClass> MiddleClasses { get; set; }
}
public EntryClass
{
public string Name { get; set; }
public string SomethingSpecial { get; set; }
}

public MiddleClass
{
public List<EntryClass> EntryClasses { get; set; }
}

public StartingClass
{
public List<StartingClass> MiddleClasses { get; set; }
}
Then you would need to iterate over over every MiddleClass inside of StartingClass's list, and inside each one of those MiddleClass object, iterate over that list until you find your target. So something like:
public EntryClass FindTarget(StartingClass startingClass, string nameTarget)
{
foreach (var middleClass in startingClass.MiddleClasses)
{
foreach (var entryClass in middleClass.EntryClasses)
{
if (entryClass.Name == nameTarget)
return entryClass;
}
}

// we have iterated over every possible EntryClass and none had the "nameTarget"
return null;
}
public EntryClass FindTarget(StartingClass startingClass, string nameTarget)
{
foreach (var middleClass in startingClass.MiddleClasses)
{
foreach (var entryClass in middleClass.EntryClasses)
{
if (entryClass.Name == nameTarget)
return entryClass;
}
}

// we have iterated over every possible EntryClass and none had the "nameTarget"
return null;
}
If the nesting of your classes is more "deep" you would just add more for loops.
Dawnbomb
Dawnbomb17mo ago
Thanks!
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.