Solved

Is there a shorthand syntax to create a record?

Posted on 2004-09-03
3
152 Views
Last Modified: 2010-04-04
I need to pass a record as an argument to a function, and I have all the record fields as seperate variables. Is there an easy way to do this?

At the moment I am writing:

---

procedure SomeProc;
var
   x, y  : integer;
   pt    : TPoint;
begin
  <---  get x and y somehow --->
  pt.x := x;
  pt.y := y;
  somePointFunction(pt);
end;

---

I'd quite like to be able to write something more like:

---

somePointFunction(TPoint(x: x; y: y));

---

but that doesn't compile.

Of course, it's not too much bother when the record has just two fields, but if it is a long record it can work out as a lot of code.

Thanks for your time,


Rich
0
Comment
Question by:ricbrad
3 Comments
 
LVL 7

Accepted Solution

by:
LRHGuy earned 50 total points
ID: 11974499
You can do something like this!

type
  myrec=record
    a,b,c:integer;
  end;

procedure yo2(a:myrec);
begin
  {use a}
end;

function makemyrec(a,b,c:integer):myrec;
begin
  result.a:=a;
  result.b:=b;
  result.c:=c;
end;


yo2(makemyrec(1,2,3));

0
 
LVL 4

Expert Comment

by:Evarest
ID: 11975142
And for TPoint itself, you can simply call

MyFunction(Point(X,Y));

Evarest
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11978041
Shorthand? Nope, not unless you write one yourself. There are some shorthand methods for some Windows API records like Point for a point and Rect for a rect. But in general, assignments of fields in records are just one field at the time. Like LRHGuy just told you...

In general, when you do need to fill some record structure quite often, write a shorthand function for it. Again, LRHGuy gave the example.

There is, however, some other trick that you might in Delphi 5 and higher, which is called function overloading. Means you can have a function somePointFunction that accepts just a TPoint record and a second function with the same make that accepts two fields from a record. Something like:

procedure somePointFunction(Pt:TPoint); overload;
Procedure somePointFunction(X, Y: Integer); overload;

I tend to use overloaded functions quite regular this way, just letting one method fill a record, then call the other. Like this:

procedure somePointFunction(Pt:TPoint); overload;
begin
  somePointFunction(Pt.X, Pt.Y)
end;

Or this:

Procedure somePointFunction(X, Y: Integer); overload;
var Pt: TPoint;
begin
  Pt.X := X;
  Pt.Y := Y;
  somePointFunction(Pt);
end;

Don't use both at the same time, though. You'd end up in an endless loop. ;-)
Sometimes your code becomes much easier by using method overloading, although it does have some limitations too. (For example, the compiler must be able to determine which overloaded method you're calling and can only do this if both methods have different parameter types. But it does allow you to write:

  somePointFunction(x; y);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A short film showing how OnPage and Connectwise integration works.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now