Board index » delphi » Back to Basics

Back to Basics


2005-04-20 10:18:31 PM
delphi134
Hi,
I really should know this and have not been able to find anything on the
net, maybe my search criteria is wrong. However, I know delphi has a
TObject class, but is this anagolous to the Java Object class. Insofar as a
procedure in Delphi can take in a parameter of type TObject and then that
Object can be cast into the Object it actually is??
E.g.
procedure TMYClass.GetSomething(Value: TObject);
begin
...
MyTList := "TObejct cast as a list";
MyTList.add(...);
end;
Many thanks
Rudy
 
 

Re:Back to Basics

Yes!
E.g.
procedure TMYClass.GetSomething(Value: TObject);
begin
with Value as MyTList do
add(...);
end;
or
procedure TMYClass.GetSomething(Value: TObject);
var
list: MyTList;
begin
list := Value as MyTList;
list.add(...);
end;
Regars,
"Rudy Hentzen" <XXXX@XXXXX.COM>escreveu na mensagem
Quote
Hi,

I really should know this and have not been able to find anything on the
net, maybe my search criteria is wrong. However, I know delphi has a
TObject class, but is this anagolous to the Java Object class. Insofar as
a
procedure in Delphi can take in a parameter of type TObject and then that
Object can be cast into the Object it actually is??

E.g.

procedure TMYClass.GetSomething(Value: TObject);
begin
...
MyTList := "TObejct cast as a list";
MyTList.add(...);
end;

Many thanks

Rudy


 

Re:Back to Basics

"Rudy Hentzen" wrote
Quote
...
MyTList := "TObejct cast as a list";
Delphi has both guarded and unguarded (aka 'hard') casts
var
myList : TList;
begin
// guarded cast--returns nil if aObject is not a TList
myList := aObject as TList;
if assigned(myList) then
myList.Add(...
or you can make the check yourself and then do a hard cast
var
myList : TList;
begin
if aObject.InheritsFrom(TList) then
begin
myList := TList(aObject);
myList.Add(...
Syntax can be shortened to
if aObject.InheritsFrom(TList) then
TList(aObject).Add(...
bobD
myList := TList(aObject);
 

Re:Back to Basics

"Anderson Farias" <XXXX@XXXXX.COM>a écrit dans le message de news:
XXXX@XXXXX.COM...
Quote
procedure TMYClass.GetSomething(Value: TObject);
var
list: MyTList;
begin
list := Value as MyTList;
list.add(...);
end;
for safety you should do this :
procedure TMYClass.GetSomething(Value: TObject);
var
list: MyTList;
begin
if Value is MyTList then
begin
list := MyTList(Value);
list.add(...);
...
end;
end;
Joanna
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
 

Re:Back to Basics

"Bob Dawson" (in a moment of insanity) wrote
Quote
// guarded cast--returns nil if aObject is not a TList
myList := aObject as TList;
Actually this will raise an Invalid class typecast exception if aObject is
not a TList, so safe practice would require
var
myList : TList;
begin
try
(aObject as TList).Add(...);
except
//action on not-a-TList condition
end;
Knew I should have checked that--I always use the
Quote
if aObject.InheritsFrom(TList) then
approach.
bobD
 

Re:Back to Basics

Cool i knew there would be some form of mechanism, just couldnt find the
syntax...
Is there also a String object, i know there is a String list but is there an
object equivilvent (again as in Java :-) )
Thanks
Rudy
 

Re:Back to Basics

Hi Joanna,
As always safety first. I also asked in Bob Dawson's reply is there is a
String class equivilent so that String can be treated as objects also.
Thanks
Rudy
"Joanna Carter (TeamB)" <XXXX@XXXXX.COM>writes
Quote
"Anderson Farias" <XXXX@XXXXX.COM>a écrit dans le message de
news:
XXXX@XXXXX.COM...

>procedure TMYClass.GetSomething(Value: TObject);
>var
>list: MyTList;
>begin
>list := Value as MyTList;
>list.add(...);
>end;

for safety you should do this :

procedure TMYClass.GetSomething(Value: TObject);
var
list: MyTList;
begin
if Value is MyTList then
begin
list := MyTList(Value);
list.add(...);
...
end;
end;

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker


 

Re:Back to Basics

Quote
Is there also a String object, i know there is a String list but is there an
object equivilvent (again as in Java :-) )
nope
string, integer, boolean, double, .. are primitive types (not objects)
 

Re:Back to Basics

"Lee_Nover" <Lee_Nover[nospam]@delphi-si.com>a écrit dans le message de
Quote
nope
string, integer, boolean, double, .. are primitive types (not objects)
... until you get into Delphi for .NET :-))
Joanna
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
 

Re:Back to Basics

Hmmm.... How would i then send a String or Integer to a procedure that is
expecting a TObject?? Which could then manipulate, as an example, the String
and Integer Differently, without overloading or is that the only way.
Java had an Object that corresponded to the primitive types, is there a work
around in delphi??
 

Re:Back to Basics

"Rudy Hentzen" wrote
Quote

Is there also a String object,
There's no TString object as such--strings are treated as primitive data
types with a couple of pieces of compiler magic going on underneath.
so
var
myStr1, myStr2 : string;
begin
myStr1 := 'This is a string'; //no constructor/destructor
myStr2 := myStr1;
//there is now one copy of the actual string in memory,
// with myStr1 and myStr2 both pointing to it.
// The string data is cached and reference counted
// behind the scene
myStr2 := myStr2 + ' modified';
//Delphi has a copy-on-modify scheme, so now myStr1 and mrStr2 point to
two different strings
bobD
 

Re:Back to Basics

"Rudy Hentzen" wrote
Quote
Hmmm.... How would i then send a String or Integer to a procedure that is
expecting a TObject?? Which could then manipulate, as an example, the
String
and Integer Differently, without overloading or is that the only way.
You could overload. I think that would be the 'normal' approach--
TsomeObj.SomeProc(var aString : string); overload;
TsomeObj.SomeProc(var anInt : Integer); overload;
TsomeObj.SomeProc(var aObj : TObject); overload;
Quote
Java had an Object that corresponded to the primitive types, is there a
work
around in delphi??
Or if you really want to, create your own primative type wrapper class.
bobD
 

Re:Back to Basics

Cool thanks a million for the ideas, need to get a pen and paper out and do
some "thinking".
Cheers
Rudy
 

Re:Back to Basics

"Joanna Carter (TeamB)" <XXXX@XXXXX.COM>escreveu na mensagem
Quote

for safety you should do this :

Yes! sorry for being too "Simple"! Thanks Joana! :-)
T+
 

Re:Back to Basics

"Rudy Hentzen" <XXXX@XXXXX.COM>writes
Quote
Hmmm.... How would i then send a String or Integer to a procedure that is
expecting a TObject?? Which could then manipulate, as an example, the
String and Integer Differently, without overloading or is that the only
way.
With a little difficulty. However, typically this is not a huge problem.
There are several alternatives:
1. Overloaded functions for basic types.
2. Typically you will often be wanting to get two classes to interact, and you
implementation will be different from strings and integers - so you will pass
a pointer to a containing class, and the two classes will have a
3. You could use variants (but they suck).
4. You could use a reference to a variant record.
Quote
Java had an Object that corresponded to the primitive types, is there a
work around in delphi??
Those are the three I can think of.
Note hoever that there's a difference here between Java, Eiffel, C# etc and
Delphi because (with the possible exception of Eiffel) they are all
interpreted languages, and can go for a higher level of abstraction over
efficiency. One of Delphi's design goals was to be as fast as C++, and
compatible with older borland versions of pascal, hence the basic types.
Having said that, there are probably some fancy class encapsulations out
there somewhere...
MH.