Modifier une variable existante en tant que variable dynamique
Bonjour, le titre est peu clair, mais voici ce que je désire faire.
Je possède des ComboBox (10), j'ai eu envie de faire en 4 lignes ce que je pourrais faire en 40.
Example de nom de ComboBox :
AlteracValleyProfileType, WarsongGulchProfileType...
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| Battlegrounder.Profiletype.ComboboxItem item = new Battlegrounder.Profiletype.ComboboxItem();
for (int x = 0; x <= ProfileTypeFile.Battlegrounds.Count - 1; x++)
{
for (int y = 0; y <= ProfileTypeFile.Battlegrounds[x].ProfileTypes.Count - 1; y++)
{
item = new Battlegrounder.Profiletype.ComboboxItem();
item.Text = ProfileTypeFile.Battlegrounds[x].ProfileTypes[y].ProfileTypeName;
item.Value = ProfileTypeFile.Battlegrounds[x].ProfileTypes[y].ProfileTypeId;
this[ProfileTypeFile.Battlegrounds[x].ProfileTypes[y].ProfileTypeId + "ProfileType"].Items.Add(item);
// Reviendrait à faire: AlteracValleyProfileType.Items.Add(item);
}
} |
Ma structure XML (+la classe ComboBoxItem utilisée pour item)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| using System;
using System.Collections.Generic;
namespace Battlegrounder.Profiletype
{
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
[Serializable]
public class BattlegrounderProfileType
{
public List<Battleground> Battlegrounds = new List<Battleground>();
}
[Serializable]
public class Battleground
{
public string BattlegroundName = "";
public string BattlegroundId;
public List<ProfileType> ProfileTypes = new List<ProfileType>();
}
[Serializable]
public class ProfileType
{
public string ProfileTypeName = "";
public string ProfileTypeId = "";
}
} |
Ce que je pourrais faire:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
for (int x = 0; x <= ProfileTypeFile.Battlegrounds.Count - 1; x++)
{
if (ProfileTypeFile.Battlegrounds[x].BattlegroundId == AlteracValley.ToString())
{
for (int y = 0; y <= ProfileTypeFile.Battlegrounds[x].ProfileTypes.Count - 1; y++)
{
item = new Battlegrounder.Profiletype.ComboboxItem();
item.Text = ProfileTypeFile.Battlegrounds[x].ProfileTypes[y].ProfileTypeName;
item.Value = ProfileTypeFile.Battlegrounds[x].ProfileTypes[y].ProfileTypeId;
AlteracValleyProfileType.Items.Add(item);
}
}
if (ProfileTypeFile.Battlegrounds[x].BattlegroundId == WarsongGulch.ToString())
{
for (int y = 0; y <= ProfileTypeFile.Battlegrounds[x].ProfileTypes.Count - 1; y++)
{
item = new Battlegrounder.Profiletype.ComboboxItem();
item.Text = ProfileTypeFile.Battlegrounds[x].ProfileTypes[y].ProfileTypeName;
item.Value = ProfileTypeFile.Battlegrounds[x].ProfileTypes[y].ProfileTypeId;
WarsongGulchProfileType.Items.Add(item);
}
}
} |