Saturday, October 11, 2003

Class references and virtual constructors 


What are some of the things that makes Delphi unique? One thing that Delphi provides is virtual constructors. So how do you make a virtual call to a constructor when, by definition, you don't have an instance? This is accomplished by using a one of the other somewhat unique Delphi constructs, the class reference. A class reference variable holds, not a class instance, but a class type. They also follow the same assignment compatibility rules that a class type variable does.

So what is this good for? What if you didn't know what class to construct at compile-time? Well, class references and virtual constructors to the rescue. So you can now have a hierarchy of classes where you can decide at run-time which classes to construct and use.


type
TComponent = class
constructor Create; virtual;
end;

TComponentClass = class of TComponent;

...

TMyComponent = class(TComponent)
constructor Create; override;
end;

TMyOtherComponent = class(TComponent);
constructor Create; override;
end;

function CreateComponent(AComponentRef: TComponentClass): TComponent;
begin
Result := AComponentRef.Create;
end;

procedure CreateComponents;
var
AComponent: TComponent;
AnotherComponent: TComponent;
begin
AComponent := CreateComponent(TMyComponent);
AnotherComponent := CreateComponent(TMyOtherComponent);
end;


In the above example, the function CreateComponent will create any component that is of type TComponent or any descendant thereof. TComponent is declared to have a virtual constructor. By passing in a specific class reference type into CreateComponent, any component descendant can be created.

Still not convinced? Well this is exactly how the Delphi form designer functions. When you register your components into the IDE, you are passing a class reference. This is then stored internally in a class reference variable associated with a particular tool palette item. When the user selects the palette item and clicks on the surface of the form designer, that class reference is passed into the designer where a component instance is constructed by calling the virtual constructor through that class reference.

There you have it. Hopefully you can see the power and simplicity of using virtual constructors and class references.


0 comments

Thursday, October 09, 2003

Chris Anderson found me.. 


Seems Chris Anderson found me... What I find interesting about his blog is the amount of personal information he shares. We all can get to know him quite well, yet we are all anonymous to him. I've personally met Chris several times and can say that he's dedicated and passionate about his work. He's also extremely talented and I count it an honor to have met him. He and I also have another thing in common; we've both worked side-by-side with one of my all-time, most respected engineers, Anders Hejlsberg.

I've always believed that in order for one to grow personally in any area, technically, socially, spiritually, etc.., one should surround themselves with folks that they respect and admire in that area. Anders Hejlsberg was certainly a person of this caliber, both as an engineer and as a person. I also know that Anders only works closely with top notch folks, so it certainly is safe to assume that Chris "has got it goin' on."

I hope that wasn't too sappy for you Chris... ;-)

0 comments

Wednesday, October 08, 2003

Generics in the .NET CLR 


The generics are coming, the generics are coming... The next release of the .NET platform/framework will contain support for generics. If you are going to be doing any kind of .NET programming you should really bone-up on this stuff. These are definately not your father's C++ templates. Generics for cross-language use is going to be really cool...
0 comments

Truth is sometimes stranger than fiction 


CNN.com - Schwarzenegger wins, Davis concedes - Oct. 8, 2003

This is just plain weird. Ah-nold takes the gubernatorial recall race. Whodda thunk it? Well... I suppose that even if he doesn't do anything while in office, that is better than making an even bigger mess. And who knows maybe he'll actually fix something. One thing is certain though, it has certainly woke up the California voters. There was a record showing at the polls. I know I had to actually wait for a parking spot at my polling place. Normally there are only about two or three other voters while I am there. This time there was about 8 or 9.

0 comments

Monday, October 06, 2003

Poetic Justice 


http://www.miami.com/mld/miamiherald/living/columnists/dave_barry/6934584.htm

There *is* such a thing as "poetic justice" in this world....

I implemented my own "do-not-call" list several years ago. Many phone companies offer what is called "Privacy Manager." This little service works with the callerID service by automatically blocking callers that refuse to present their callerID information and diverting them to a special message. In this message they can elect to reveal their callerID number, or identify themselves verbally and wait for the "Privacy Manager" to contact us on their behalf. The interesting thing about this service is that since most telemarketers don't present their CallerID info, they are hit with this first line of defense. Also, since telemarketers work on volume they are just as impatient to wait the minute or so it takes to actually by-pass all those phone menus and messages as you and I would be, so they just hang up and I never even know they called. For those few diligent telemarketers that actually take the time to wrestle with the inconvenience of the privacy manager, I figure it must be important so I'll answer. Oh and this also blocks calls from folks not covered by the do-not-call list.

2 comments

Boring drivel 


I hope I'm at least as boring as this guy feels about the .NET bloggers.... If I were spoofed, at least I'd know someone was listening ;-)..

/dev/null

Maybe I'll start posting information on IOTANotifier..

0 comments

Disclaimer:
The opinions expressed here are mine. They should in no way be construed as being the opinion of my employer, Borland Software Corp. If you actually think that my opinions are a reflection of Borland, then I have a bridge I can sell you.

Subscribe to my RSS feed.

This page is powered by Blogger. Isn't yours?