Solved

Store Procedure

Posted on 2004-04-24
8
189 Views
Last Modified: 2010-04-05
How can I create a procedure that stores a value to a list of variables.

I tried this:

Procedure StoreInt( value:integer; var arr:array of integer);
var i:integer;
begin
      For i:=1 to high(arr) do
      arr[i]:=value;      
end;


var x,y,z:Integer
........
StoreInt(10,[x,y,z]);


When I compile the application I receive the following error: "Constant object cannot be passed as var parameter"
0
Comment
Question by:ronnix
  • 3
  • 2
8 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  type
     DynamicArray = array of integer;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Procedure StoreInt( value : integer; var arr : DynamicArray);
var
  i : integer;
begin
  For i:= 1 to high(arr) do
     arr[i]:=value;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  arr : DynamicArray;
begin
  StoreInt(10,arr);
end;

end.
0
 

Author Comment

by:ronnix
Comment Utility
I didn't understood me. i want to store a single value to more variables not to the items of an array;

i.e
I replace
x:=10;
y:=10;
z:=10;

with StoreInt(10,x,y,z);

So that, how can i make x,y,z be equal with 10 after calling StoreInt as above?
0
 

Author Comment

by:ronnix
Comment Utility
You didn't understood me. i want to store a single value to more variables not to the items of an array;

i.e
I replace
x:=10;
y:=10;
z:=10;

with StoreInt(10,x,y,z);

So that, how can i make x,y,z be equal with 10 after calling StoreInt as above?
0
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 150 total points
Comment Utility

// after StoreInt you can make a procedure which transform the variant array A into array of Integers

Procedure StoreInt( value : integer; var arr : Variant);
var
  counter    : Integer;
begin
  For counter := VarArrayLowBound(arr, 1) to VarArrayHighBound(arr, 1) do
    begin
      arr[counter] := value;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x, y, z : Integer;
  a : variant;
begin
  a := VarArrayOf([x, y, z]);
  StoreInt(10, a);

  Label1.Caption := VarToStr(a[0]);
  Label2.Caption := VarToStr(a[1]);
  Label3.Caption := VarToStr(a[2]);
end;
0
 

Author Comment

by:ronnix
Comment Utility
it works but after calling the StoreInt procedure I must convert the values:
i.e
x:=vartoint(a[0]); etc

and is quicker to use the classic method:
x:=10 etc.

Don't you know any other method?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

11 Experts available now in Live!

Get 1:1 Help Now