Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable object as parameter

Status
Not open for further replies.

tsd10123

Programmer
Feb 4, 2009
2
Hi,

I'm using 2 types of objects and need the some same procedures for both types, how can I pass them as parameter?

Small Example to show what I mean:
type
obj1=object
nr:int64;
procedure p;
end;
obj2=object
nr:integer;
procedure p;
end;
[...]
var a:eek:bj1; b:eek:bj2;
procedure proc3(var c);(*I Don't know what type I should use, or can I say that's type obj1 or obj2?*)
begin
c.p;
end;
Begin
proc3(a);
proc3(b);
end.

Without a defined type (like it is showed now), it gives an Error62: record, object or class type required. on the c.p; -line
Thx,
Pieter
PS:The solution isn't putting both types togheter.
 
I don't see a way to do this, myself, without changing what you're doing in that code above. You need to typecast the variable somewhere along the way. This is either done in the procedure header (as obj1 or obj2) or in the procedure itself ("Obj1(c).p"). You could use procedure pointers or logic to determine which object was passed to run within the procedure, but this can be rather tedious.

What is it that you are looking to get done? Maybe we can suggest something from there?

Measurement is not management.
 
Couldn't you Overload proc3 as in:

Code:
procedure proc3(var c : obj1); Overload;
begin
....
....
end;

procedure proc3(var c : obj2); Overload;
begin
....
....
end;
 
What about using templates? I don't know about their usage in Delphi, but I use them in C++ all the time. Something like this (mind you, I never did this in Delphi, but this would be the logic as it is in C++):

Code:
//implementation of the procedure
//T is a kind of generic type which will accept different
//real life types, which are in essence similar (like two objects)
Template <class T>
procedure proc3(var c: T);
 begin
  c.p;
 end;

//...
//then just do the regular calling of the procedure
//...
 proc3(a);
 proc3(b);
//...

But, I'm kinda beginner, so maybe I'm missing something. :)

Best regards...
 
Hi,
Maybe I need to add some additional information. I have a rather big program which also uses (in a calculation) sometimes more than 2Gb of memory. I can't reduce the necessary memory, so I thought to add a checkbox which desides to use/not to use the hard drive as memory (like windows does) but only for 1 type of object.

I want to make this work in a way the code stays understandable, and I don't need to copy every procedure (overloading), because it would almost double the code.

So I wanted to save a list, part of the object sometimes in memory, sometimes at the hard drive. Simply change the procedure that uses the memory would mean it always have to check the checkbox. I hoped I could somehow make all procedures work for both types, without overloading them.
 
Another option would be to make a Master object and derive Obj1 and Obj2 from the Master. Big note: This is not the exact code - I haven't coded any object/inheritance stuff for years.

Code:
TObjMaster=Object
  procedure p;
end;

TObj1=TObjMaster
  nr:int64;
end;

TObj2=TObjMaster
  nr:int;
end;

Then your procedure parameter can be declared as TObjMaster which will accept a TObj1 or TObj2 or any other object derived from TObjMaster:
Code:
procedure proc3(var c:TObjMaster);
begin
  if c is TObj1 then
  begin

  else
  if c is TObj2 then
  else

  ...
end;

It might make for cleaner code if you added an integer to the master object which was assigned 1 in Obj1 and 2 in Obj2 so you could do something like:
Code:
procedure proc3(var c:TObjMaster);
begin
  case c.ObjectTag of
   1: //This is TObj1
   2: //This is TObj2
   else: //call some sort of error
  end;
end;

 
Like I already said, the easiest is to use templates.

Best regards...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top