Chee Wee, Chua - 蔡志伟

Part 1: Why use Delphi over C#?

I was chatting with Tim,and he’s trying to append an ArrayList to another ArrayList.

One would assume you can do something like, B.CopyTo(A) unfortunately, it doesn’t work because CopyTo expects a parameter of Array. So if you try B.CopyTo(A.ToArray), it doesn’t really work either.

ArrayList A, B;
A = new ArrayList(3);
B = new ArrayList(5);
for (int i = 1; i < 4; i++)
A.Add(i);

for (int i = 4; i < 8; i++)
B.Add(i);

B.CopyTo(A.ToArray());

Why can’t you just perform B.CopyTo(A) naturally? Did someone forget to declare public virtual void CopyTo(ArrayList)?

Well, Delphi’s class helper to the rescue. In Delphi’s documentation, "A class helper is a type that - when associated with another class - introduces additional method names and properties which may be used in the context of the associated class (or its descendants)."

C# 3.0 of course has extension methods, which is actually Delphi’s class helpers in disguise. And both class helpers and extension methods were invented by the same person (or group of people), Charles Jazdzewski (commonly known as Chuck Jazdzewski, who was the previous Delphi Architect), who has moved on to Microsoft to work on XAML. But C# 3.0 isn’t here yet.

In C#, if we wanted to add an ArrayList to another, the code would be:

static void Main(string[] args)
{
ArrayList A, B;
A = new ArrayList(3);
B = new ArrayList(5);
for (int i = 1; i < 4; i++)
A.Add(i);

for (int i = 4; i < 10; i++)
B.Add(i);

foreach (object I in B)
A.Add(I);

foreach (object I in A)
Console.WriteLine(I);

}

Notice that we have to enumerate all the members of B in order to add it to A, and it’s not intuitive what is being done.
What if we wanted to write B.CopyTo(A) naturally? Can we do it? With Delphi and class helpers, we can!

{$APPTYPE CONSOLE}

{%DelphiDotNetAssemblyCompiler ‘$(SystemRoot)microsoft.netframeworkv1.1.4322System.dll’}

uses
SysUtils, System.Collections;

type
TCopyToArray = class helper for ArrayList
procedure CopyTo(AArrayList: ArrayList); overload;
end;

procedure TCopyToArray.CopyTo(AArrayList: ArrayList);
var
O: &Object;
begin
for O in Self do
AArrayList.Add(O);
end;

var
A, B: ArrayList;
I: Integer;

begin
A := ArrayList.Create(3);
B := ArrayList.Create(5);

for I := 1 to 3 do
A.Add(I);
for I := 5 to 9 do
B.Add(I);

B.CopyTo(A);

for s in B do
WriteLn(s);
end.

Of course, the great Danny Thorpe once said,

The simplest answer is often the best: personal preference.
Why do people prefer one kind of car over another? There are many different kinds of cars, but they all serve basically the same purpose and are built pretty much the same way. Some believe that the car they drive is an expression of their personality or status some believe their car is actually technically superior in manufacture to his neighbor’s; some believe their car is an unreliable pile of junk and modify their daily pattern to accommodate their paranoia. There are many rationalizations for choice, but only one consistent answer: emotion. Personal preference. We prefer what we are comfortable with, or what excites us, or what makes sense to us, individually. And we will rationallize to absurd extremes to justify that emotional choice.

Cognitive psychologists and linguists hold that language not only represents our thoughts, but that the structure of a language can influence the structure of our thoughts, how we think about an issue or visualize an idea. A subtle distinction that can be expressed in a word in one language may require pages in another. If you are using the latter, would you express that nuance regularly, or even at all? No! It’s too much work. The idea doesn’t fit well into your language’s range of expression.

Are programming languages that different from spoken languages? Does a Fortran programmer deconstruct a programming problem differently than a Cobol programmer? Absolutely! Not only are the syntaxes different, but the data handling concepts behind these languages are fundamentally different as well.

Should a Fortran programmer be forced to write code in Cobol? Probably not, particularly if the programmer relates well to Fortran. The cliche’ here would normally be "Use the right tool for the job" but what many people overlook is that the tool, the thing that solves problems, is not the programming language - the critical asset to solving the programming objective is the programmer. The programmer can be inhibited by ill-fitting tools built for a different view of the world, or the programmer can be empowered by comfy tools where things make sense and ideas click into place. No two people think alike, so no one tool can be a best fit for everyone.

For best results, use the right tool for the programmer.

-Danny Thorpe
Delphi Compiler Architect
Borland

Posted by Chee Wee, Chua on December 23rd, 2005 under Delphi |



19 Responses to “Part 1: Why use Delphi over C#?”

  1. Atul Says:

    Relly, exciting.

  2. Danny Heijl Says:

    Correct me if I’m wrong, but doesn’t AddRange append one ArrayList to another?

  3. Liz Says:

    The saddest part about this is that pressing F1 on "helper" doesnt bring up help.

  4. Liz Says:

    PS - it looks like class helpers are in for Win32 too!!

  5. Gregoire GUYON Says:

    Correct me if I’m wrong, Delphi said ObjectHelper is deprecated for developpement.

    Implementation and use are reserved for borland to translate vcl to vlc.net.

  6. Barry Kelly Says:

    I’m not sure anybody uses ArrayLists any more (except if they are stuck on .NET 1.1, I suppose). List<T> allows:

    List<int> x = new List<int>();

    // add to x

    List<int> y = new List<int>();

    x.ForEach(y.Add)

    apart from the obvious AddRange, of course.

  7. Chee Wee Chua Says:

    Hi Barry,

    Thanks for your comments. I freely admit that my knowledge of the .NET Framework is appalling.

    That you for pointing out the AddRange method, which both Tim and I weren’t aware of.

    The name AddRange seems to be a misnomer. One would have assumed that AddRange adds a range, instead of adding a ICollection though.

    Gregoire, my blog reflects only my opinion, my ignorance and the knowledge I’ve learnt.

  8. Gregoire GUYON Says:

    I m sorry, i dont think about ignorance or any thing else.

    It’s difficult for me to explain anything in a blog because i am french people and my english vocabulary is very very poor.

    it’s my ignorance.

    Joyeux Noel from france.

  9. Gregoire GUYON Says:

    With .net framework you can make the same thing with IExtenderProvider interface.

    This interface use (for example) by ToolTip or ErrorProvider.

    If you are intresting by this subject you can find a complete implemtation at this address : http://www.codeproject.com/dotnet/iextenderprovider.asp

    Barry, your comment is true but Delphi is in .net 1.1 for 1 year ago and template is a new fonctionality of v2.

    Happy christmass from france.

  10. CB Says:

    Yes, class helper is a powerful thing. but there are too much trouble to drive me to leave Delphi.

    for example,

    in c#

    int[] A;

    then A will be a sub class of System.Array

    I can call all the methords of Array, such as A.Length()

    but in Delphi

    if I write A:Array of integer;

    I must Array(A).Length

    moreover,

    if i build a assembly with C#, i can use it in another project, but if I do the same thing with Delphi, if i want use it in a Delphi project, it require a dcpil file, why?

    I know there some reason, but why chrome can do the same thing as c# while implement a pascal language?

    I like Delphi language very much but in .net I have to select C#.

  11. Chee Wee Chua Says:

    Well, you can write Length(A) if you want. ;o)

  12. Alex Ronald Says:

    hmm… interesting technology… need to try… Thanks

  13. MrNedBurns Says:

    I don’t give a hoot about Delphi or Fortran, but your comments regarding why programmers choose the languages they do are dead on man. You’re a poet and you don’t know-it.

  14. Try learning the API before writing Says:

    Look up the static Array methods. You’ll find one that will allow you to do this:

    Array.Copy(A, B);

  15. Chee Wee Chua Says:

    A and B are not arrays.

  16. shine Says:

    i am a developer going to migrate from .NET to delphi.is it right idea?

  17. Charles Says:

    I definitely prefer .net over delphi

  18. Robert Rotariu Says:

    I am a Delphi fan (user too, but eing a fan is what matters here) and, after reading, I saw this article is biased.
    Then, I saw the comments and realized the article is written for the wrong reasons. Happened to me a couple of times too.

    For Charles and for shine:

    "I prefer .NET over Delphi"
    equivalent to
    "I prefer apples over apples-on-a-plate"

    Apples-on-a-plate is just a way of handling your apples; one of many ways.

  19. Patrick Ryan Says:

    I really like Delphi, but have you tried to find Delphi jobs in the U.S lately without having to move? The market is drying up. I’m forced to "goto" Java and/or C#. CodeGear doesn’t seem to care about Delphi anymore. Borland did originally. With C# around. Trying to look look more like C# doesn’t cut it! Streamline/simplify DB access and reporting. Add integrated CASE to Delphi. It’s lost the appeal of integration.



Server Response from: blog1.codegear.com