Solved

Dinamic array

Posted on 2001-09-05
8
212 Views
Last Modified: 2010-04-04
I want some code regarding to make an array in run time
any number of items I want.
number of items will be defined at the moment I need the array.

thanks in advance
 
0
Comment
Question by:marquinho
8 Comments
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6458021
If you are using D5 of D5 you can use SetLength. Below is some sample code so adoped to your needs

var
      sWhereGen: array of string;
     i: Integer;

begin

     for i := 0 to WhatEver
              SetLength(sWhereGen, i);

end;


The Crazy One
0
 
LVL 44

Accepted Solution

by:
CrazyOne earned 100 total points
ID: 6458030
Here is what Delphi help has to say about SetLength


Sets the length of a string or dynamic-array variable.

Unit

System

Category

string handling routines

procedure SetLength(var S; NewLength: Integer);

Description

S is a string or dynamic-array variable.

For a short-string variable, SetLength simply sets the length-indicator character (the character at S[0]) to the given value. In this case, NewLength must be a value between 0 and 255.

For a long-string or dynamic-array variable, SetLength reallocates the string or array referenced by S to the given length. Existing characters in the string or elements in the array are preserved, but the content of newly allocated space is undefined. If there is not enough memory available to reallocate the variable, an EOutOfMemory exception is raised. Following a call to SetLength, S is guaranteed to reference a unique string or array?that is a string or array with a reference count of one.
0
 
LVL 3

Expert Comment

by:VSF
ID: 6458055
This shows a way to do it! Showed to me by frodo!
Hope this helps!

VSF
www.victory.hpg.com.br

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, Buttons, Menus, ComCtrls;

Type
  Mat_Array = Array of array of Real; (* 2-D Matrix of Reals *)
  Int_Array = Array of Integer; (* 1-D Matrix of Integers     *)

.
.
.

Var
  Form1: TForm1;

  COEFF_MAT   : Mat_Array; (* User Input : Coefficient Matrix of system   *)
  orDER_VECT  : Int_Array; (* Defined to pivot rows where necessary       *)

.
.
.

Procedure tform1.Initialize_Array( Var Coeff_Mat : Mat_Array ; Var order_Vect : Int_Array);
(*** This Procedure initializes all matrices to be used in Program       ***)
Var
  I, J : Integer;
begin
 //Dynamic Arrays support
 j:=stringgrid1.rowcount; //This is where size comes from
 //Vector
 SetLength(order_Vect, j);
 //Matrice
 SetLength(Coeff_Mat, j);
 for i:= Low(Coeff_Mat) to High(Coeff_Mat) do
  SetLength(Coeff_Mat[i], j);
end;
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:VSF
ID: 6458067
Qualquer problema marquinho... e so perguntar!
0
 
LVL 2

Expert Comment

by:bugroger
ID: 6458068
Hi,


Some basics:
Var
 DynArray : array of "Byte,Word,Dword, String..."

Use "SetLength" to set the item count of a dynamic array!!!

SetLength(DynArray, 5); // = array[0..4] of "....."

if you close your app. you should set the dynamic array
item count to 0

SetLength(DynArray, 0);

Example:

Var
 DynArray  : array of array of byte;
 z         : Byte;

Begin
 SetLength(DynArray, 5);
 For z := 0 to 4 do
  SetLength(DynArray[z], 5);
 // = array[0..4, 0..4] of byte;

 DynArray[0, 1] := 7;

 SetLength(DynArray, 1);

 DynArray[0, 2] := 8;


 SetLength(DynArray, 0);
End;


GL
Bug




0
 
LVL 1

Author Comment

by:marquinho
ID: 6458447
CrazyOne
is any way to setup an array?
I mean once you need a new item adding or deleting without deleting the rest of the items.
Thanks a lot CrazyOne
also I have to say thanks VSF and bugroger

0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6458660
Well when you increase the size of the array it will retain what is already in it so you don't lose anything that is already there. I believe when you decrease the size of the array the data in the array will be retained except obviously the portion of the array that is getting chopped off.

For an example let us say the array is a string and we have now dimensioned the array for 3 elements and we have populated the array like this

('a', 'b', 'c')

now we increase it by one so there are 4 elements to the array and we have as of yet populated this new element it would look like this

('a', 'b', 'c', '')

now we populate the 4th element with a d so now it looks like this

('a', 'b', 'c', 'd')

now we want to remove two of the elements so we would be left with two elements it would look like this

('a', 'b')

now if data that you wanted to retain was this instead

('b', 'd')

I think one would need to have a companion secondary array to hold the data desired and after dimensioning the array repopulate the array with the secondary array.
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6458674
I am curios if you meant to give a B grade for this question. Not looking to start a debate with you about the grade but I am tring to clarify if the grading defaults to a B or if the questioner is setting the grade to a B. Lots of Experts have being getting B's lately which seems to indicate that the system is defaulting to a B. If you truly feel my comment is only worth a B that is cool with me I have no problem with that but like I said I am trying to find out if it is something the EE system is doing, :>)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple image collision 13 80
Unique identifier on a terminal server (rdp) 4 67
Installshield for Embarcadero EX 10.1 Berlin 4 60
Tidtcpserver listening on multiports? 1 35
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

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