I'm trying to create a stop watch form. I want to be able to have a button I press to get a Lap time just like a normal stop watch. For reasons I don't understand I can't define a Stopwatch variable and call it's constructor in another method. How would I go about making a StopWatch
variable so that I can use it in other methods? If I'm not supposed to use a StopWatch
what should I be doing?
namespace _Hard0002_StopWatch {
public partial class StopWatch : Form {
StateEnum state;
StopWatch stopWatch;
public StopWatch() {
InitializeComponent();
state = StateEnum.Stoped;
}
private void StartButton_Click(object sender, EventArgs e) {
if (state == StateEnum.Stoped) {
stopWatch = Stopwatch.StartNew();
state = StateEnum.Started;
ChangeFormTittle("Started");
TimeKeeperText.Text = "0.0";
}
}
private void LapButton_Click(object sender, EventArgs e) {
//save time elapsed to a string array
stopWatch.getElapsedTime()//I know this isn't how you call it I just worded it to be readable.
}
This is the error I'm getting when I try to declare stopWatch
in my StartButton_Click()
Cannot implicitly convert type 'System.Diagnostics.Stopwatch' to '_Hard0002_StopWatch.StopWatch'
C#
's stopwatch isStopwatch
. Your class isStopWatch
. You declare your variable:then initialize it:
using two different objects. Best to rename your form to something like
StopwatchForm
to prevent from confusing yourself more.