Sunday, 14 March 2010

Csharp Notes

Notes on CSharp, CSharp IDEs, Windows Forms and Related Stuff

Just started digging into C#. I find that I am almost as well informed as the rest of the population in some respects.

Editing

I use Emacs and Visual Studio. Emacs because I like it VS because I have to sometimes. I use csharp-mode but found that when I installed the latest cc-mode it stopped working because of a couple of missing macros. This problem was solved for me by aaron mayerson. All you have to do is add these lines to csharp-mode.el:

;;;c-paren-re and c-identifier-re were helper macros that
;;;got removed from cc-mode, as noted in the changelog
;;;for 2002-09-10.
;;;If you add these to csharp-mode.el, things seem to
;;;work ok.

;;; Helpers for building regexps.
(defmacro c-paren-re (re)
`(concat "\\(" ,re "\\)"))
(defmacro c-identifier-re (re)
`(concat "\\[^_]"))

Dennis Haney, the maintainer of csharp-mode.el also pointed out that Emacs can be used as the default editor by Visual Studio, see the VisEmacs web site.

Building and testing

I've started using NUnit and NAnt. It was a very frustrating experience to begin with because it seems that you can't have two programs calling each other that try to use versions of mscorlib from different versions of the .Net Framework. See BuildingDotNet for more information.

Threading

Haven't had to deal with this explicitly for a long time (6502, 6800, Z80 assembler, Turbo Pascal co-routine implementation, MPL, Software Description Language (SDL), etc.; all more than a decade in the past). Now I need to get to grips with threading again and my goodness, haven't the gurus made it complicated. Why can't we have a decent message passing system instead of the cumbersome objects plus function call way of doing it. Good news! The is a language that doesn't block! It is called E which makes a rather difficult name for a Wiki page; I'll call it NonBlockingLanguage.

Well no point dwelling on the stupidity of modern software construction, just have to live with it. Go to SharpThreads for some more notes on the subject.

File Name Globbing

I can't find any clear definition of it in the .NET SDK but the args array given to Main is simply an array containing the white space delimited and double quote delimited tokens found on the command line. If a, non-quoted, argument was a file glob then it will not have been expanded to a list of matching file names if you are running in a Microsoft environment.

This, is of course, because .NET simply takes whatever the shell gives it. Caught me out because I am using zsh on Windows 2000 and zsh processes globs correctly as befits a properly written Unix shell. If I run the same program in an ordinary DOS box the answers are different.

If you are writing cross platform programs that use the command line you might need to cope with this by detecting the shell type and expanding globs yourself.

Program Structure

A program must contain a public class with a static method called Main. Note the capital M. Note also that it isn't uppercase, that just says which case of type the printer (a human being) got the type from and as this probably won't ever be printed on paper uppercase makes no sense; we should resurrect (or rescue from antiquarian use) the fine words majuscule and minuscule. See IrrelevantAsides for more jibes at the world.

This method is the entry point of the program. For a console program that is all you need to know. For a windowing program (not necessarily a Windows program) this should contain a constructor that creates the necessary controls for a form; the Main method should call the Run method the Application class passing it a new instance of the main class. Note that it is usual for Main to be a static method of the main class although, as far as I can tell, it is not required that this be so. It must of course be static or it would not be possible to call it until the object had been created.

    [STAThread]
      public static void Main(string[] args)
      {
        Application.Run(new MainForm());
      }

Note that the Main method has been marked with the STAThread (static thread?) attribute, this is not a requirement for all programs.

Links to C# related pages

Here is a page of CsharpLinks.

IDEs

I heartily recommend SharpDevelop. It's free and it seems to work. Unlike most free IDEs this one also has a working forms designer, well almost. Not quite compatible with the one in Visual Studio so editing VS designed forms can be a pain but I daresay that will improve.

Actually I'm not so sure about SharpDevelop now because the 'round tripping' seems unreliable. There are several Emacs modes for C#, the one I am using at the moment is from http://davh.dk/script/. There is a more sophisticated development environment, csde; when I have downloaded all the components and go it working I might add some notes.

Windows Forms

I'm using Windows Forms for two reasons: firstly because it is the only set of GUI classes that I have on my PC at the moment, secondly because it means that I am more likely to be able to share my code with my colleagues who are captives of Microsoft.

Plotting Pixels

Well, Microsoft didn't make it easy but I finally worked out how to plot pixels. The trick is to use a Bitmap object and plot the pixels using the SetPixel method. Look at the PictureBox page for example code.

Eric Gunnerson has written a column that shos how to use unsafe code for bitmap manipulation that should let me use grey scale bitmaps for this which should be faster again.

Splitters

I found splitters very difficult to understand to start with and Mahesh xxxx's tutorial didn't help much, probably my fault rather than his. Anyway by the time I had found the example 'Using the Splitter Control' in http://abstractvb.com/code.asp I had already figured it out .

See AddSplitters for an example.

No comments:

Post a Comment

Blog Archive

Followers