Solved

Is there a shorthand syntax to create a record?

Posted on 2004-09-03
3
151 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

16 Experts available now in Live!

Get 1:1 Help Now