Jump to content

Typecasting


dkeene

Recommended Posts

Hello, all. 

If I create a new unigui component at runtime:

var

aNewUniComponent: TObject;

begin

   aNewUniComponent:=TUniDBMemo.Create(self);

   ... 

 

then I wish to associate it with a Data Field. I know it's a data-aware control, so I should be able to do the following:

   TUniDBEdit(aNewUniComponent).DataField:='aFieldName';

whether it's a TUniDBEdit, TUniDBMemo, TUniDBNumberEdit, etc.

This fails, however, with an access violation, although I am not sure why, and only works with the exact same class: 

   TUniDBMemo(aNewUniComponent).DataField:='aFieldName';

otherwise I may get an Invalid Pointer Operation error.

 

My Question is if there is a Class from which all data-aware unicontrols descend such that I can typecast without access violations, such as

   TUniDBControl(aNewUniComponent).DataField:='aFieldName';

Other wise I need to use a series of if then else statements like:

   if ClassName='TUniDBEdit' then

      TUniDBEdit(aNewComponent).DataField:='aFieldName' else

   if ClassName='TUniDBMemo' then

      TUniDBMemo(aNewComponent).DataField:='aFieldName' else

   if ClassName='TUniDBNumberEdit' then

      TUniDBNumberEdit(aNewComponent).DataField:='aFieldName' else

etc.

or perhaps there is a better way?
Thank you in advance

Doug

  • Upvote 1
Link to comment
Share on other sites

may be
 

if assigned(aNewComponent) then
begin
  if aNewComponent is TUniDBEdit then 
  begin
    TUniDBEdit(aNewComponent).DataSource := ...;
	TUniDBEdit(aNewComponent).DataField := ...;
  end;

  if aNewComponent is TUniMemoEdit then 
    ...

end;
  

 

Link to comment
Share on other sites

Thank you x11. I am trying to avoid a giant list of "if aNewcomponent is TUniDBEdit else if aNewComponent is TUniDBNumberEdit .. else if aNewComponent is TUniDB...

there might be a way of saying TUniDBBaseControl(aNewComponent).Datafield...

Thanks

Doug

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...