I have a fairly simple class where the only members are an enumerated type and an object whose members are two integers and a string. Nothing that needs memory management, so I wanted to call my constructor "From" instead of "Create" to signify that no memory allocation takes place, and Free won't be necessary. However, this doesn't seem to work. I created an ObjectDictionary<ItemType, Span> and populated it with Span.From(...) and none of the objects seemed to be properly created (they were just junk data). When I replaced the From constructor with a From class function, it worked properly.
Can someone tell me what is happening in the class function that isn't happening in the constructor that is making it work?
Thank you!
{ Some things left out for brevity }
type
Color = class
public
property Red: Integer;
property Green: Integer;
property Blue: Integer;
constructor From(r, g, b: Integer); overload;
constructor From(hexVal: String); overload;
end;
type
ItemType = (A, B, C);
type
Span = class
public
property Color: Color;
property item: ItemType;
constructor From(item: ItemType, color: Color);
end;
implementation
constructor Span.From(item: ItemType, color: Color);
begin
Color := color;
Item := item;
end;
{ The class function I replaced the constructor with. This works. }
class function Span.From(item: ItemType, color: Color): Span;
begin
result := Span.Create;
result.Color := color;
result.Item := item;
end;