Solved

Is there a shorthand syntax to create a record?

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 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