Loading...
 
Just started learning c#

C-sharp

C# aka C-sharp.

April 2008,

For most of my working life I've programmed embedded systems. Most times I had a PC (IBM clone, apple 2 clone or Amiga) hooked to it via serial comms of some sort. At the pointy end the micros are programed in assembler, forth or C.
On the PC side the code was basic in the early XT days, z80 turbo pascal on the apples and C on the amiga.
Then turbo vision of the PC and finally Delphi.

I got into delphi just as version 2 came out. It was great.
It was dirt cheap and unlocked so I could run it at work and at home. It wasn't copy protected but I did the right thing and (work) paid for it.
I used V2 for many years then upgraded to V5. I also ran V6 but the license terms for V5 seemed better for writing the odd program for money. I only ran personal editions and they are a bit crippled but not to bad.

Then the price of delphi went through the roof. There was now crap with keys required.
Borland changed names a few time and went back to have some free-ish versions - I installed them but didn't want to commit to them.

Last year (07) my delphi-5 started to get buggy - the bugs went away when compiled on turbo delphi explorer.

Explorer is a free personal edition. Unfortunately it is crippled. Just because you write free code doesn't mean you can work with crippled tools.
A year ago the un-crippled version was advertised at a substantial discount from the normal $555aus - but the online shopping cart still listed it at full price.
All my emails (four to different address) to code-gear (the new owner of Delphi) were unanswered.
I was pissed off and I decided to look for different compiler.

We used commercial C compilers for programming atmel AVRs. Again they were a little expensive but had more locks and license crap to make life difficult. I switched to GCC (winavr) and never looked back.
Now I can have a copy on any (windows) machine I want, all free and I actually like it better.

So GCC was what I had in mind.
I wanted something open sourced, multi-platform and visual.
Unfortunately GCC doesn't fit the bill. GCC on windows needs something like cygwin to run. There is also MinGW. They aren't really delphi substitutes from what I can tell.

Micro-soft have free versions of their visual studios but I have a bad feeling about that. They are free but more registration keys etc and unlikely to be linux friendly.


I ask my code guru Mike for advice and he suggested I look into c# - I think he was right.

C# requires the .NET framework to run.
Being somewhat anti-Microsoft I've done my best to ignore .NET altogether.
As reluctant as I am the praise MS for anything - I have to say I'm impressed.

.NET uses a platform independent code which is compiled to native machine code as required.
In theory a c# binary could run on different types of CPUs without the user needing to recompile it.

C# isn't quite what I expected either. I expected it to be C with extensions - it isn't.
A lot of the c# syntax is identical but some isn't.

This path lead to the mono project.
Quote - Mono provides the necessary software to develop and run .NET client and server applications on Linux, Solaris, Mac OS X, Windows, and Unix. Sponsored by Novell (http://www.novell.com), the Mono open source project has an active and enthusiastic contributing community and is positioned to become the leading choice for development of Linux applications. End-quote

Mono has an IDE called MonoDevelop. This looks ideal expect there is no easy way to build a windows version of it. It can be done but there is no existing installer.
This may be because there is also and open sourced IDE for windows called sharpdevelop or #develop.
Having two IDEs seems like duplicated effort to me.


Installing .NET (3.5) and #develop 2.2 was quite painless. However installing the recommended MS .NET SDK wasn't so easy. It is 1.3 gig and crashed during install - I repaired it.
  1. develop apparently uses the SDK for its help file but I haven't manged to get this to work yet.

The first project I created seemed to be a dud but the second one ran.

Coming from a delphi background the new IDE looks quite similar. Using the form designer to place components (such as buttons and test windows) is easy, so is editing the properties.

Most of the components I use are already installed.



There are some differences in c# to delphi's object pascal. One is objects tend to get destroyed when you don't wish them to be.
For example - Create a form in the main program - open it using a button event - close it with another button.
Everything is fine so far but if you attempt to open the form again to get a crash because the form got destroyed when you close it. You can open a new instance of the form but loose any changes made to old instance. There is a work around.

Another problem is cross-threading. If a control (such as a text window) is created in the main thread it can only be accessed from that thread (yes there is a work around - I haven't tried it yet).


One of my gripes with delphi was the lack of serial port support. Borland in their wisdom didn't support them - I know I'm not the only one who needed serial ports. If borland thought a decade ago simple serial had gone away they were mistaken. We still need serial comms even now - it is still the best solution for many needs (such cheap long distance coms).
I paid for a 3rd party serial component which was discontinued and had all the hassle of finding a new one and converting all my code.

So it was a pleasant surprise to find not only did .NET2 contain a serial component but #develop also has serial as a standard non-visual component.

I downloaded a serial demo program for MS visual-c# and only needed to fix one method name to run it from #develop.

I had several demos which worked but didn't quite do what I wanted and I couldn't figure out how to convert them. Fortunately there is no deadline for learning c# but it was a little frustrating not been able to do what is trivial in Delphi.
There are probably good reasons for limiting access to control created by other threads but I not sure what they are.

It took me a long time to work out how to access the UI controls from another thread (such and a serial receive event). I think some of my problem was many of the examples are still for .NET1
In .NET2 there are "anonymous delegates" which make life a little easier even though some people think "anonymous delegates" are a bad thing.
I finally managed to get a timer event updating a rich text box on the main form.
Next will be a serial receive text box.
Some clues are here http://msdn2.microsoft.com/en-us/magazine/cc163970.aspx

I found some delphi to c# converters and tried two of the demos.
I tried "Delphi2cs" and "Turnsharp".
Neither got very far converting my sonar program.
Turnsharp seems to be best but the demo is so crippled I can't really say.

It is asking a lot of a program to do a conversion like this.
The big problem both for human and machine delphi to c# conversion is that c# doesn't have all the standard delphi controls/components.
C# doesn't have spinedits but it looks to me like numericUpDown is equivalent. I use spinedits a lot and Delphi2cs couldn't convert them.
Another real pain is radiogroups. C# doesn't have radiogroups. You can put radiobuttons inside groupboxes to give the same look and feel but this is quite different at the code end.

Delphi radiogroups have a property called itemindex. This is a simple way to query which radio button is pushed. It is a simple matter to use the index with a "case" statement to execute the appropriate code.
You can still write code to do the job but it is much longer.
In general C# seems to be much wordier the delphi - it looks like C# code turns out around 1/3 longer.

Strangely C# has a checkedListBox with a SelectedIndex property which is similar to a radiogroup except it has select boxes not radio buttons.
One obvious work around if you want to keep the case/switch code is to use the Tag field of the group box.
There doesn't seem to be a "checked" event so we have to use the CheckedChanged event and test if it is checked or not.
Like so.
void RadioButton1CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked) groupBox1.Tag=0;
}

void RadioButton2CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked) groupBox1.Tag=1;
}

Tags are of the type object so need to be cast to integers for the switch statement.
Initializing the tag at design time didn't work and it would spit at runtime. I had the initialized it in the startup code.
It is all getting messy and for small numbers of buttons individual test of the checked property is probably easier.
I guess someone could build a custom checkedListBox to fix all this but it is beyond me at this point.

It is only a small fly in the ointment. I never thought porting my existing code would be practical so it is actually a better outcome than I expected.

I thought I had the cross threading problem linked but I didn't.
Anonymous delegates worked fine for the timer but not for the serial port.

I ended up using a delegate based on a terminal demo I'd downloaded
The bit that write to the text box looks like this.
private void del(string msg)
	{
	richTextBox1.Invoke(new EventHandler(delegate
            {
	         richTextBox1.AppendText(msg);
            }));
	}


I got my SAM7 talking to my program as in dumb terminal mode - it's a start.

Image

More Emserver stuff will be added here

29'th Aug 2008,
I bought turnsharp today. I was sent two copies of the welcomed emails and keys so no doubt I will be billed twice. I've emailed them but no response yet.
With any luck I'll try to convert some code this weekend.

Feb 2008,


It looks like work has begun on a windows version of monodevelop. http://mjhutchinson.com/journal/2009/02/07/monodevelop_vista64

So far c# has been disappointing as a cross platform language. I tried running one of my sharpdev projects under linux mono and failed.
You also can't easily port the source because #dev uses winforms and monodev use widgets.

Having MD on windows might solve these problems. I would still need to rewrite my #dev programs but I haven't got a lot of those.

I can't see this being good for sharpdevelop. All the duplicated effort is a bad thing.

eddie



Created by eddie. Last Modification: Friday 27 of February, 2009 13:50:42 AEDT by eddie.

Main Index

Switch Theme

Shoutbox

eddie, 13:04 AEST, Thu 10 of Aug, 2023: Offline tiki 26 upgrade went badly. Waiting for 26.1. Will limp on.
System Administrator, 18:45 AEST, Wed 26 of Jul, 2023: Recovered from lockout but unable to upgrade to V24
eddie, 23:20 AEST, Sun 29 of Aug, 2021: moving to new server
System Administrator, 17:23 AEST, Thu 12 of Aug, 2021: Image thumbnails not working for gallery images. Problems with the GD extension.
System Administrator, 19:44 AEST, Mon 09 of Aug, 2021: uploaded wiki and database to main server. It didn't work but the old wiki and repaired database seem to mostly work.

Last-Visited Pages

Online Users

60 online users