0

フォームの上部に次のものがあります。

public static int hoursInt;
public static int minutesInt;
public static int secondsInt;
public static int CompletedIn24;

numericUpDown次に、新しいランナーを選択するときにボックスをゼロにリセットするために、次のようにします。

private void lstRunners_SelectedIndexChanged(object sender, EventArgs e)
{

Runner selectedRunner = (Runner)lstRunners.SelectedItem;

numericUpDown1.Value = 0;
numericUpDown2.Value = 0;
numericUpDown3.Value = 0;
}

次に、Finishボタンクリックイベントで次のことを行います。

 hoursInt = Convert.ToInt32(numericUpDown1.Value);
 minutesInt = Convert.ToInt32(numericUpDown2.Value);
 secondsInt = Convert.ToInt32(numericUpDown3.Value);

if (lstRunners.SelectedIndex > -1 && hoursInt + minutesInt + secondsInt != 0)
        {
            // Obtain selected climber
            Runner selectedRunner = (Runner)lstRunners.SelectedItem;
            selectedRunner.Hours = hoursInt;
            selectedRunner.Minutes = minutesInt;
            selectedRunner.Seconds = secondsInt;

            var expertRunner = selectedRunner as Expert;
            if (expertRunner != null)
            {
                expertRunner.UponFinish();
            }

これが私のオーバーライドされたメソッドですExpert : Runner

public override void UponFinish()
        {
            base.UponFinish();

            // The integer must increment by one if the time is 24:00:00 or less i.e. 23:59:59 would increment the integer as well
            if (Hours < 24 || (Hours == 24 && Minutes == 0 && Seconds == 0))
            {
                CompletedIn24++;
            }
        }

現在、何かが必要かどうかわからないので、中括弧の中にUponFinish()メソッドにRunnerは何もありませんか?

CompletedIn24ボタンクリック時に動作するかどうか整数値を文字列に出力してみましたが、エキスパートランナーを選択して24:00:00以下の時間でも値がゼロのままでした。整数が増加しません。何が問題の原因なのかわかりません。

どんな助けでも大歓迎です。

4

7 に答える 7

1

as次のようにキーワードを使用するだけです。

var runner = selectedRunner as Expert;
if(runner != null) runner.UponFinish();

クラスでRunnerというメソッドがすでに定義されているUponFinish場合は、このメソッドを として定義し、次virtualのように派生クラスでそのメソッドをオーバーライドする必要があります。

public class Runner {
  public virtual void UponFinish(){
     //...
  }
}
public class Expert : Runner {
  public override void UponFinish(){
    //You talked about the time, I asked for clarification on this
    //but it's still very unclear. I suppose when you mean the time is 24:00:00
    //that means the hours is 24, the minutes is 0 and the seconds is 0
    if(Hours < 24 || (Minutes == 0 && Seconds == 0)) Completedin24++;
  }
}

もちろん、キャストは必要ありません。呼び出すだけUponFinishで、オーバーライドされたコード (存在する場合) が正しく呼び出されます。

selectedRunner.UponFinish(); 
于 2013-11-07T14:12:20.673 に答える
0

これを行うにはいくつかの方法がありますis。たとえば 、オペレーターが存在します。オペレーター IS

于 2013-11-07T14:11:57.720 に答える
0

is キーワードを使用して、ランナーが ExpertRunner であるかどうかを確認できます。

if(selectedRunner is ExpertRunner)

ただし、OOP に関しては、これを行う必要はありません。オーバーライドされた動作 (関数またはプロパティ) ではなく、このケースを個別に処理する必要がある理由を階層またはロジックで確認することをお勧めします。

于 2013-11-07T14:12:00.133 に答える
0

これを試して :

if(lstRunners.SelectedItem is Expert)
{
    Expert selectedRunner = lstRunners.SelectedItem as Expert;
    selectedRunner.UponFinish();
}
于 2013-11-07T14:12:04.707 に答える
0
if (lstRunners.SelectedItem is Expert)
{
    ((Expert)lstRunners.SelectedItem).UponFinish();
}
于 2013-11-07T14:12:15.707 に答える
0

次のようにタイプを確認できます。

if (selectedRunner.GetType() == typeof(Expert))
{
    Expert expert = (Expert)selectedRunner;
}
于 2013-11-07T14:11:07.507 に答える
0

できるよ

if(selectedRunner is Expert)
{
    UponFinish((Expert)selectedRunner);
    //or ((Expert)selectedRunner).UponFinish(); if that was the intention
}

または代わりに

Expert selectedExpert = selectedRunner as Expert;
if(selectedExpert != null)
    UponFinish(selectedExpert);

編集:

関数UponFinishがすでに と の両方の一部であるRunner(Expertつまり でオーバーライドされているExpert) 場合、それを呼び出す前に selectedRunner をキャストする必要はありません。

于 2013-11-07T14:11:17.593 に答える