August 30, 2024

C# on linux -- Tempoerature statistics - using Visual Studio Code

This will be my first project using C# and VS-code. I have a big file with 7 years of temperature data taken once a minute. I want to read that file and calculate things like the high and low temperatures for each year. I wrote such a program using Python not too long ago and thought it would be interesting to try to do the same using C#.

I probably should just take some time out to watch some of the videos about how to use VS-code, but I am impatient, and besides -- beating my head against the wall just trying to use it will give me more motivation and interest when (and if) I do take time to watch the videos. But be aware that this documents both my thrashing around and bad non-intuitive design of VS-code.

I am continuing to use the C# Dev Kit welcome page in VS-code I use their link to make a new project. I select .NET new project, console app, and hit return a bunch of times to accept defaults and I seem to have a project started. I use the CS Code Explorer to go to it. It is in

cd /u1/home/tom/c-sharp
ls -lrt
-rw-r--r-- 1 tom tom     1009 Aug 30 12:19 ConsoleApp1.sln
drwxr-xr-x 4 tom tom     4096 Aug 30 12:19 ConsoleApp1
I type Ctrl-n to create a new file, fight with it a bit to get it to let me type into the window. Once I win the fight, it is respecting vim keystrokes, so my Vim extension "just works". I place this into Console.cs
Console.WriteLine ( "Some pig" );
I use Run -- Run without debugging and see the output:
Hello, World!
Clearly VS Code and I have different ideas about what should be going on. Amusingly, I find this text in "grunt.cs", which is a file I used a few days ago when I was trying things with Mono. I create a directory "Mono" and move all of that stuff into it and try again. It is still stuck on this. I do
rm -rf vscode
rm -rf .vscode
But then I get a better idea.......

Start over

VS code is too smart for its own good. I need to restrict its options, so I do this:
cd /home/tom
mv c-sharp c-sharpest
mkdir vscode
I also have to exit and restart vscode -- now it tells me "no folder opened". Good!

It gives me a button to open a new folder. I click it, get a file chooser, navigate to select "vscode" but do not enter vscode -- then select it and it seems happy. It is asking if I trust the stuff in the empty folder. I say yes.

Now I use Ctrl-Shift-P (not Ctrl-P as one might think) to get to the command palette. The first entry is a new .NET project. It asks me to sign in again with Microsoft, which I do. Now I understand that when it says I can close the window that something magic has happened and the process has finished.

Now back to Ctrl-Shift-P. Now I can select a Console project. You have to stick with it or it times out and you have to start over. Coming here to log what I am doing is evil, and I have to go back and start over. But now I have some boilerplate files showing up in the Explorer.

I use Ctrl-N to create a new file. The mysterious line telling me to select a language appears. How am I supposed to do that? Click on those words and a menu shows up! What a discovery. I select C# and enter my Console.WriteLine () again. The file has no name. I type Ctrl-S to save it and a dialog appears, suggesting Console.cs -- that is fine and I go with it. I select Run, Run without debugging. It asks me to select a debugger, which seems stupid and needless since I just indicated I want to run without debugging. Then to cap it all, I get the output:

Hello, World!
Some examination makes this clear (sort of). There are a million VScode boilerplate files, but ignoring all but one of those, the file layout looks like this:
vscode
vscode/ConsoleApp1.sln
vscode/Console.cs
vscode/ConsoleApp1
vscode/ConsoleApp1/Program.cs
The file Program.cs has the Hello, World message that is driving me crazy. Apparently just doing Ctrl-N created the file, but did not put it where vscode would notice it. I edit Program.cs and my new message appears when I type Run, Run without debugging. And I am not prompted to select a debugger that won't be used. This is progress! You would think that VS code's right hand would know what its left hand is doing, but apparently not. Or maybe there is some other thing besides Ctrl-N that creates a file and makes it part of the current project.

I try the "run and debug" button on the left. It seems useless. It works as a toggle to open up some debug panes on the left side, but doesn't actually run the program. I guess the idea is to open up those panes, then do something (who knows what?) to actually run the program. Indeed, when the panes are open, a little green triangle appears up top and that is the "go " button to run the program.

Some observations so far

Not having a "new project" thing in one of the main menus seems like a significant oversight. What could be more intuitive than typing Ctrl-Shift-P and selecting it from a menu? In general, VS code is non-intuitive. This doesn't seem to even be on the radar for the team that designed VS code. It may be capable and maybe even powerful, but you are going to have to make an investment to climb the learning curve.
Feedback? Questions? Drop me a line!

Tom's Computer Info / [email protected]