5

I am attempting to build a simple stopwatch WPF application.

Here is my code:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Diagnostics;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    public stopWatch = new Stopwatch();

    private void startTimer()
    {
        stopWatch.Start();
        Dispatcher.BeginInvoke(DispatcherPriority.Render, new ThreadStart(ShowElapsedTime));
    }
    void ShowElapsedTime()
    {
        TimeSpan ts = stopWatch.Elapsed;
        lblTime.Text = String.Format("{0:00}:{1:00}.{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
    }
}
}

and here

enter image description here

I am using System.Diagnostics but for some reason I cannot access the Stopwatch

and also I can't find System.Diagnostics in this Dialogue:

enter image description here

Why can't I use System.Diagnostics.Stopwatch and why does System.Diagnostics not appear in the references dialog?

Karlo
  • 1,630
  • 4
  • 33
  • 57
jth41
  • 3,808
  • 9
  • 59
  • 109
  • 1
    Code is text. It's a lot easier to see when you copy and paste it *as text* than when it's in a picture (which is coming up tiny on my current screen). – Jon Skeet Aug 30 '13 at 13:56
  • `System.Diagnostics` is within the `System.dll`. I'd create a new blank Console application -> verify you can access it there. Also, when you say you cannot use it...what's error? We cannot see any error. – Arran Aug 30 '13 at 13:56
  • 2
    You've made a mistake declaring your `stopWatch` field; you forgot the declaration type. :) EDIT: The C# syntax/intellisense checker has gone nuts because of it. – Chris Sinclair Aug 30 '13 at 13:57
  • @JonSkeet Then you cant see the red squiggly lines – jth41 Aug 30 '13 at 13:58
  • @jth41: You could easily have *described* where the error was... ideally with an even shorter program. As it was, my original answer was incorrect because I couldn't read your `using` directives. – Jon Skeet Aug 30 '13 at 14:01
  • 1
    I just realize that I tried mixing VB with C#. – jth41 Aug 30 '13 at 14:01
  • You have way too many mistakes in that code. Don't just keep banging in code until you get completely lost in the error list. – Hans Passant Aug 30 '13 at 14:02

2 Answers2

8

You'll need:

Stopwatch stopWatch = new Stopwatch();

You just have (public stopWatch = new StopWatch) which is not how C# objects are created.

stopWatch is the instance, StopWatch is the class definition.

Darren
  • 68,902
  • 24
  • 138
  • 144
  • Hmm, so if I type Stopwatch stopwatch = new Stopwatch in my code editor, this will lead to System.Diagnostics.Stopwatch appearance in the reference window? – TiredOfProgramming Apr 20 '18 at 02:14
1

I had a similar problem and turns out I'd typed "StopWatch" instead of "Stopwatch". I hate case sensitivity!

Jeff
  • 8,020
  • 34
  • 99
  • 157