コースの一部としてストップウォッチとして機能する小さなプログラムを作成しています。 私が抱えている問題は、私がコンパイルしようとすると私のProgram.cs
クラスの私のDuration()
メソッドにCannot convert from void to bool
を得ているということです。 メソッドが返されますTimeSpan
C#ボイドからブールに変換できません - CS1503
void
に設定されている場所がわかりません。おそらく、C#ランタイムの何か低いレベルですか?わからない。
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stopwatch
{
class Program
{
static void Main(string[] args)
{
var stopwatch = new StopWatchController();
stopwatch.Start();
stopwatch.Start(); // Second Start() method should throw an exception
stopwatch.Stop();
Console.WriteLine(stopwatch.Duration()); // Error appears here
}
}
}
StopwatchController.cs
using System;
using System.Runtime.CompilerServices;
namespace Stopwatch
{
public class StopWatchController
{
private DateTime _startTime;
private DateTime _finishTime;
private TimeSpan _duration;
private bool _isWatchRunning;
public void Start()
{
if (!_isWatchRunning)
{
_isWatchRunning = !_isWatchRunning;
_startTime = DateTime.Now;
}
else
{
throw new Exception("InvalidArgumentException");
}
}
public void Stop()
{
_isWatchRunning = false;
_finishTime = DateTime.Now;
}
public void Duration() // I'm an idiot
{
_duration = _finishTime - _startTime;
}
}
}
期間を)voidメソッドですが、あなたはそれをWriteLineメソッドしよう。それはうまくいきません。 –
"Public TimeSpan Duration()"を意味しますか? –
あなたはコードブラインドに行かなければなりません:P – BugFinder