Re: Referencing an object via variable.

From: AlanGLLoyd (alanglloyd_at_aol.com)
Date: 09/19/04


Date: 19 Sep 2004 11:00:43 GMT

In article <414c0f2b$0$36861$e4fe514c@news.xs4all.nl>, "Maarten Wiltink"
<maarten@kittensandcats.net> writes:

>You can use the name, and Alan has told you how, but you already have
>the pointer you need at the start. It's often (not always) simpler to
>keep those pointers in some place of your own and get them from there
>when you need them again.
>

You might also like to think whether you actually do _need_ the name. Mostly
when Delphi calls an event handler it passes the reference of the
object-causing-the-event in its event handler parameter. Just typecast that
TObject to what it is.

Also one could store the reference to objects in the Tag property of some
enclosing object (maybe a TGroupBox or TPanel), typecast it into storage and
out of storage. Delphi also sometimes (as in a treeview) TTreeNode provides a
Data property for such storage.

Better programming style minimises use of the Name property, because you may
not know it, and getting the reference is somewhat of a pain, interrogating an
array or somthing like that, and it might change. Its only some random (even if
meaningful) text associated with the object which does everything.

If you have a fixed set of options, think of using enumerated values to
identify some option - these can be meaningfully named, and also used as an
index into an array holding (for example) objects, or descriptive strings. I
find that I am more likely to want the string from the object (usually for
display) than the object from the string.

Alan Lloyd
alanglloyd@aol.com



Relevant Pages

  • Re: pesky Pointers !!
    ... > and the function takes it as a reference instead of a copy. ... function may access the string passed directly, ... > *px dereferences the pointer to get the value ... If pTest is a pointer-to-string, *pTest is the string it points to ...
    (alt.comp.lang.learn.c-cpp)
  • Re: String Reference Type
    ... pointer to the value. ... lblClassName name to "My String". ... everything else is a true object and thus a reference type. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: String Reference Type
    ... pointer to the value. ... lblClassName name to "My String". ... everything else is a true object and thus a reference type. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Freeing a record which contains a string field leads to a memory/string leak ?!
    ... Since there *is no string*, there is no reference count, so it can't be ... Let the compiler manage it exclusively. ... > alive but so that delphi does destroy the string when the pointer ...
    (alt.comp.lang.borland-delphi)
  • Re: Why does string not require a new like other reference types?
    ... then nothing else is needed and the storage is all done on the stack. ... if String is a reference type why do you NOT ...
    (microsoft.public.dotnet.languages.vb)