2017-12-28 4 views
-1

私はクラス名がDriverで、オブジェクトは20個あります。オブジェクトクラスの変数の1つが変数値。私はクラスのオブジェクトを変更するだけで、ウィンドウコードのような20のコードを持っています、そして、私はそれを簡単にするためにこれをしたいと思います。クラスの1つのオブジェクトに変数のX値があることを検出するには

public class Driver 
{ 
    public float Position; 
    public float DriverID; 
    public float TeamID; 
    public float TyreCompound; 
    public float BestLap; 
    public static Driver van = new Driver(0, 0, 0, 0, 0); 
    public static Driver ric = new Driver(0, 0, 0, 0, 0); 
    public static Driver vet = new Driver(0, 0, 0, 0, 0); 
    public static Driver rai = new Driver(0, 0, 0, 0, 0); 
    public static Driver gro = new Driver(0, 0, 0, 0, 0); 
    public static Driver eri = new Driver(0, 0, 0, 0, 0); 
    public static Driver per = new Driver(0, 0, 0, 0, 0); 
    public static Driver alo = new Driver(0, 0, 0, 0, 0); 
    public static Driver str = new Driver(0, 0, 0, 0, 0); 
    public static Driver mas = new Driver(0, 0, 0, 0, 0); 
    public static Driver mag = new Driver(0, 0, 0, 0, 0); 
    public static Driver kvy = new Driver(0, 0, 0, 0, 0); 
    public static Driver hul = new Driver(0, 0, 0, 0, 0); 
    public static Driver pal = new Driver(0, 0, 0, 0, 0); 
    public static Driver oco = new Driver(0, 0, 0, 0, 0); 
    public static Driver ves = new Driver(0, 0, 0, 0, 0); 
    public static Driver ham = new Driver(0, 0, 0, 0, 0); 
    public static Driver sai = new Driver(0, 0, 0, 0, 0); 
    public static Driver bot = new Driver(0, 0, 0, 0, 0); 
    public static Driver weh = new Driver(0, 0, 0, 0, 0); 

    public Driver(float position, float driverid, float teamid, float tyre, float bestlap) 
    { 
     Position = position; 
     DriverID = driverid; 
     TeamID = teamid; 
     TyreCompound = tyre; 
     BestLap = bestlap; 
    } 

    public void SetData(float position, float driverid, float teamid, float tyre, float bestlap) 
    { 
     Position = position; 
     DriverID = driverid; 
     TeamID = teamid; 
     TyreCompound = tyre; 
     BestLap = bestlap; 
    } 
} 
ウィンドウで

if (App.Driver.van.Position == 2) 
{ 
    TowerSecondName.Source = new BitmapImage(new Uri(@"/Images/Drivers/driver_" + (App.Driver.van.DriverID) + ".png", UriKind.Relative)); 
    TowerSecond.Source = new BitmapImage(new Uri(@"/Images/TeamColours/" + (App.Driver.van.TeamID) + "Opa" + ".png", UriKind.Relative)); 
    TowerSecondTyre.Source = new BitmapImage(new Uri(@"/Images/TyreIcons/" + (App.Driver.van.TyreCompound) + ".png", UriKind.Relative)); 
} 
+4

あなたの質問は明確ではありません...あなたが望むものを指定してください... – Arvindsinc2

+0

'Driver'タイプのすべての静的フィールドを反復処理し、' Driver'タイプのものを検索する方法をお探しですか?特定の値?もしそうなら、あなたは[リフレクションを使ってすべての静的プロパティとクラスの値を取得する方法](https://stackoverflow.com/q/12474908/3744182)から始めることができます。 – dbc

+1

"クラスのオブジェクト変数の1つ"が意味をなさないとき。あなたが念頭に置いている事例の具体例を挙げてください。コンクリート、特定、明確。あなたがしようとしていることを伝えることができない場合、誰もあなたを助けません。 –

答えて

2

多くの個別変数を宣言する代わりに、コレクションを使用してください(例:今

public static Driver[] Drivers = new Driver[] { 
    new Driver(0, 0, 0, 0, 0), 
    new Driver(0, 0, 0, 0, 0), 
    new Driver(0, 0, 0, 0, 0), 
    ... 
    new Driver(0, 0, 0, 0, 0), 
    new Driver(0, 0, 0, 0, 0) 
}; 

あなたがインデックスによってドライバにアクセスしたり、それらを列挙することができる配列を宣言

foreach (Driver driver in App.Driver.Drivers) { 
    if (driver.Position == 2) { 
     TowerSecondName.Source = new BitmapImage(new Uri(@"/Images/Drivers/driver_" + (driver.DriverID) + ".png", UriKind.Relative)); 
     TowerSecond.Source = new BitmapImage(new Uri(@"/Images/TeamColours/" + (driver.TeamID) + "Opa" + ".png", UriKind.Relative)); 
     TowerSecondTyre.Source = new BitmapImage(new Uri(@"/Images/TyreIcons/" + (driver.TyreCompound) + ".png", UriKind.Relative)); 
     break; 
    } 
} 

あなたはLINQを知っている場合、あなたはこのようなドライバを検索することができます代わりに

Driver driver = App.Driver.Drivers 
    .Where(d => d.Position == 2) 
    .FirstOrDefault(); 
if (driver != null) { 
    TowerSecondName.Source = new BitmapImage(new Uri(@"/Images/Drivers/driver_" + (driver.DriverID) + ".png", UriKind.Relative)); 
    TowerSecond.Source = new BitmapImage(new Uri(@"/Images/TeamColours/" + (driver.TeamID) + "Opa" + ".png", UriKind.Relative)); 
    TowerSecondTyre.Source = new BitmapImage(new Uri(@"/Images/TyreIcons/" + (driver.TyreCompound) + ".png", UriKind.Relative)); 
} 

配列は、使用できる唯一のコレクション型ではありません。 List<Driver>を使用することもできます。動的に成長できるという利点があります。 Dictionary<string, Driver>は、名前(またはキーとして使用される他の固有のプロパティ)によってドライバを非常に高速に検索できるという利点があります。また、変数名の代わりにDriverクラスのフィールドまたはプロパティとしてドライバの名前を追加することを検討してください。

2

代わりのすべての変数に別の名前を与えて、辞書に格納します。そのように、あなたがすべてのオブジェクトを保持している変数を1つだけ持っていて、このように、それらを反復処理することができます:辞書に

void Main() 
{ 
    var drivers = new Dictionary<string, Driver>(); 
    drivers.Add("van", new Driver()); 
    drivers.Add("ric", new Driver()); 
    // ... etc. 

    foreach(var driver in drivers) 
    { 
     if(driver.Value.Position == 2) 
     { 
      // Do something 
     } 

     if(driver.Key == "van") 
     { 
      // Do something else 
     } 
    } 
} 

アイテムがKeyValuePairsに格納され、あなたがキーにアクセスするかどうかを定義する必要がありますかその値は.Value.Keyです。

関連する問題