Board index » delphi » Variable type instance variables

Variable type instance variables

I'm defining a class that is a "transaction".  It will have two
instance variables, a send message and a received message.
What I'll really have is a set of ordered pairs of these. They will be
of varying types, all Records but with different layouts for each
transaction.
How do I define the base class with two such variables so that I can
redeclare them in descendant classes with a different type.

jmi...@lhs.org
jmi...@lhs.org
jmi...@teleport.com

 

Re:Variable type instance variables


Quote
Jeff Mills wrote:

> I'm defining a class that is a "transaction".  It will have two
> instance variables, a send message and a received message.
> What I'll really have is a set of ordered pairs of these. They will be
> of varying types, all Records but with different layouts for each
> transaction.
> How do I define the base class with two such variables so that I can
> redeclare them in descendant classes with a different type.

Declare a TTransactionMessage as a class, and define TTransaction as a
class with a Sent and Received TTransactionMessage.

--

http://www.midnightbeach.com/jon   Personal Pages
http://www.midnightbeach.com/jon/pubs Programming Publications
http://www.midnightbeach.com/hs             Home School Resource List

Re:Variable type instance variables


Quote
Jon Shemitz <j...@midnightbeach.com> wrote:
>Jeff Mills wrote:

>> I'm defining a class that is a "transaction".  It will have two
>> instance variables, a send message and a received message.
>> What I'll really have is a set of ordered pairs of these. They will be
>> of varying types, all Records but with different layouts for each
>> transaction.
>> How do I define the base class with two such variables so that I can
>> redeclare them in descendant classes with a different type.
>Declare a TTransactionMessage as a class, and define TTransaction as a
>class with a Sent and Received TTransactionMessage.
>--

Right, but I want to sub class TTransaction to TTransactionSearch,
TTransactionGetXYZ, etc.
TTransactionSearch.SentMessage would have a layout of TRecSearchRqs
but TTransactionGetXYZ.SentMessage would need to have a layout of
TRecGetXYZRqs.

Maybe this is a lot easier than I think.

What I want to provide is a generic class that any programmer can
sub-class and only have create two new TRecMessages for the layout of
the sent message type and the expected response.

Re:Variable type instance variables


Quote
Jeff Mills wrote:

> Jon Shemitz <j...@midnightbeach.com> wrote:

> >Jeff Mills wrote:

> >> I'm defining a class that is a "transaction".  It will have two
> >> instance variables, a send message and a received message.
> >> What I'll really have is a set of ordered pairs of these. They will be
> >> of varying types, all Records but with different layouts for each
> >> transaction.
> >> How do I define the base class with two such variables so that I can
> >> redeclare them in descendant classes with a different type.

> >Declare a TTransactionMessage as a class, and define TTransaction as a
> >class with a Sent and Received TTransactionMessage.

> Right, but I want to sub class TTransaction to TTransactionSearch,
> TTransactionGetXYZ, etc.
> TTransactionSearch.SentMessage would have a layout of TRecSearchRqs
> but TTransactionGetXYZ.SentMessage would need to have a layout of
> TRecGetXYZRqs.

> Maybe this is a lot easier than I think.

> What I want to provide is a generic class that any programmer can
> sub-class and only have create two new TRecMessages for the layout of
> the sent message type and the expected response.

Well,

  type
    TTransactionMessage = class
                          end; // Just a placeholder!
    TTransaction = class
                   Sent, Received: TTransactionMessage;
                   end;

is pretty much what you're asking for, no? Any descendant of
TTransactionMessage "is" a TTransactionMessage, so a TTransaction could
hold any two transaction messages.

I gather that, in addition, you want, eg, TTransactionSearch's methods to
know that Sent and Received are TSearchMessages, not just plain
TTransactionMessages. One solution is to just use casts:

  procedure TTransactionSearch.Method;
  var
    SSent, SReceived: TSearchMessage;
  begin
    SSent := Sent as TSearchMessage;
    {...}
  end;

(You could also do the "is" checking once, in the constructor, and raise an
exception if the message's are of the wrong type; methods could then do
'blind' casting of the "SSent := TSearchMessage(Sent);" form.)

I will agree that "is"/"as" checks are fragile - the programmer is doing
the type-checking, not the compiler - but I don't that there *is* a way to
assert 'every transaction will have a sent and received message field, but
each type of transcation has its own type of message' in Delphi. You could
do it with templates in C++, but I think the best you can do in Object
Pascal is 'every transaction will have a sent and received message field of
type TTransactionMessage', as above.

--

http://www.midnightbeach.com/jon   Personal Pages
http://www.midnightbeach.com/jon/pubs Programming Publications
http://www.midnightbeach.com/hs             Home School Resource List

Other Threads