[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Is there a shorthand syntax to create a record?

Posted on 2004-09-03
3
Medium Priority
?
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Accepted Solution

by:
LRHGuy earned 150 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 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