> I was trying to make this construction: > > type > TRec = record > Numero: Integer; > Nome: String; > end; > > function mTRec(Numero: Integer; Nome: String): TRec; > > type > TTest = class > const Nomes: array[0.2] of TRec = (mTRec(1, 'everton'), mTRec(2, > 'murilo'), mTRec(3, 'vieira')); > end; [.] > Is there any how to do it? Above declaration looks to me as if you define a constant array, that exists only once in memory. You can move the 'const.' Out of the class, with the same effect. ![]() DoDi -- _______________________________________________ Lazarus mailing list. This is a tough one for VCL and inline assembly gurus. I need to dynamically create the Value parameter to pass to the Format function. (Or rewrite Format's existing functionality - ugh) Review of common knowledge: Format has the following declaration: Function Format (const aFormat: String; const values: array of const); Problem: The desired functionality is along the lines of: function TMyRule.ResultString (fmt: String): String; begin Params:= BuildParams; aString:= Format (fmt, Params); end; I have determined that the array of const is implemented as an array of TVarRec's. I am uncertain about whether it is actually a variant array or not. Either way, I can build it. ![]() Pascal Const ArrayWe use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand. Declaring Arrays in Pascal When using arrays in a computer program the array used must first be declared as a new user-defined type. This is done in the type statement and is composed of the following. There is no syntactic model to pass the created array to the function. The Pascal syntax only allows the array to be compiled in. A review of the format function in SysUtils.pas suggests that it should be possible to write the call with an inline assembly call. This is the real meat of the problem. Well, at least in D5 you can do this: type TVarRecArr = array of TVarRec; function BuildParams: TVarRecArr; begin SetLength(result, 5); // here fill the array end; begin Format('test', BuildParams); end; I think that should work for D4, too. For D2/3 you can perhaps do the same, but with this type declaration instead: type TVarRecArr = array [0.$FFFF] of TVarRec; TPVarRecArr = ^TVarRecArr; function BuildParams: TPVarRecArr; begin result:= AllocMem(sizeOf(TVarRecArr ) * 5); // here fill the array end; var params: TPVarRecArr; begin params:= BuildParams; try Format('test', params^); finally FreeMem(params) end; end; Look also at 'Slice'. ![]() Does this help? Regards, Madshi. Format's second parameter is what's called a variant open array. This example, taken from the Delphi 4 help file, shows how to access it and build it up dynamically: ---From the Help ---- Variant open array parameters allow you to pass an array of differently-typed expressions to a single procedure or function. To define a routine with a variant open array parameter, specify array of const as the parameter’s type. Thus procedure DoSomething(A: array of const); declares a procedure called DoSomething that can operate on heterogeneous arrays. The array of const construction is equivalent to array of TVarRec. C++ Const Array InitializationTVarRec, declared in the System unit, represents a record with a variant part that can hold values of integer, Boolean, character, real, string, pointer, class, class reference, interface, and variant types. TVarRec’s VType field indicates the type of each element in the array. Some types are passed as pointers rather than values; in particular, long strings are passed as Pointer and must be typecast to string. The following example uses a variant open array parameter in a function that creates a string representation of each element passed to it and concatenates the results into a single string. The string-handling routines called in this function are defined in SysUtils. > I was trying to make this construction: > > type > TRec = record > Numero: Integer; > Nome: String; > end; > > function mTRec(Numero: Integer; Nome: String): TRec; > > type > TTest = class > const Nomes: array[0.2] of TRec = (mTRec(1, 'everton'), mTRec(2, > 'murilo'), mTRec(3, 'vieira')); > end; [.] > Is there any how to do it? Above declaration looks to me as if you define a constant array, that exists only once in memory. You can move the 'const.' Out of the class, with the same effect. DoDi -- _______________________________________________ Lazarus mailing list. Declaring Arrays in Pascal When using arrays in a computer program the array used must first be declared as a new user-defined type. This is done in the type statement and is composed of the following: • Name of the array: chosen using the rules for variable names. • First element: the subscript value of the first element, eg: 1 • Final element: the subscript value of the final element, eg: 5 • Data type: the Array type, normally, Integer, Real, Char or Boolean. For example, the following code will create the arrays shown below. Books.google.co.th - Introduction to Pascal and Structured Design, provides a concise, accessible introduction to computer science. Download re loader activation. Using Pascal programming as a tool to shape students' understanding of the discipline, the text offers a strong focus on good programming habits and techniques. The smooth integration of programming. Introduction to Pascal and Structured Design. [] [] [] [] 14.4.6 In Object Pascal or Delphi mode, Free Pascal supports the Array of Const construction to pass parameters to a subroutine. This is a special case of the Open array construction, where it is allowed to pass any expression in an array to a function or procedure. The expression must have a simple result type: structures cannot be passed as an argument. This means that all ordinal, float or string types can be passed, as well as pointers, classes and interfaces. The elements of the array of const are converted to a special variant record. Procedure Testit (Args: Array of const); Var I: longint; begin If High(Args). S:=’Ansistring 1’; T:=’AnsiString 2’; Testit ([]); Testit ([1,2]); Testit ([’A’,’B’]); Testit ([TRUE,FALSE,TRUE]); Testit ([’String’,’Another string’]); Testit ([S,T]); Testit ([P1,P2]); Testit ([@testit,Nil]); Testit ([ObjA,ObjB]); Testit ([1.234,1.234]); TestIt ([AClass]); If the procedure is declared with the cdecl modifier, then the compiler will pass the array as a C compiler would pass it. This, in effect, emulates the C construct of a variable number of arguments, as the following example will show. Pascal programming language allows multidimensional arrays. Here is the general form of a multidimensional array declaration − type array-identifier = array [index-type1, index-type2.] of element-type; var a1, a2.: array-identifier; For example, the following declaration creates a three dimensional 5. 4 integer array − var threedim: array[1.5, 1.10, 1.4] of integer; Two-Dimensional Arrays The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size x, y you would write something as follows − var arrayName: array[1.x, 1.y] of element-type; Where element-type can be any valid Pascal data type and arrayName will be a valid Pascal identifier. A two-dimensional array can be visualized as a table, which will have x number of rows and y number of columns. A 2-dimensional array that contains three rows and four columns can be shown as below − Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a. Initializing Two-Dimensional Arrays Multidimensional arrays, like one-dimensional array, are initialized by through assignment, either by specifying a particular subscript or using a for-do loop. For example, var a: array [0.3, 0.3] of integer; i, j: integer; begin for i:= 0 to 3 do for j:= 0 to 3 do a[i,j]:= i * j. End; Accessing Two-Dimensional Array Elements An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example − var val: integer; val:= a[2, 3]; The above statement will take 4th element from the 3rd row of the array. You can verify it in the above diagram. Let us check below program where we have used nested loop to handle a two-dimensional array − program ex2dimarray; var a: array [0.3, 0.3] of integer; i,j: integer; begin for i:=0 to 3 do for j:=0 to 3 do a[i,j]:= i * j; for i:=0 to 3 do begin for j:=0 to 3 do write(a[i,j]:2,' '); writeln; end; end.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |