Delphi Const Array
Posted By admin On 21/05/19A look at 'array of const' for fun and profit Posted by Allen on Friday, 1 June 2007 in Blogs Back when Delphi was being developed we began to lament the lack of a function that would be able to format a string with format specifiers and some unknown number of parameters.
The array of const gives you the freedom to add strings, integers, floats and so on and having these formatted into a string. And there is no limit to how many items you can add. The way Delphi deals with this issue is that the array of const really is a array of TVarRec's. Delphi Filling a multidimensional array as a constant I have an array MyArray[1.140,1.6] of String, and as all the data is constant I want to hard-wire it as a constant.
Is there a way in Delphi declaring an array of strings such as following one?
4 Answers
In XE7 you can declare a dynamic array constant like this: Re loader activator 3.0 beta 3.
You can use dynamic arrays and try this:
While this does do a run-time initialization of a dynamic array on the heap, it also shows that Delphi supports a 'pseudo-constructor' on dynamic arrays that allow in-place initialization. (NOTE: the above code isn't thread-safe).
Now all you need to do to find out the length of the array, is use the Length() standard function, or to find the allowed index range, use the Low() and High() standard functions.
If you're using an older version of Delphi, replace the TArray with your own dynamic-array string type such as:
You can do this in a indirect way. Create a function like:
and call this function like:
where:
Not the answer you're looking for? Browse other questions tagged arraysdelphiconst or ask your own question.
I have a few const arrays of the same base type but different sizes, and I need to point to them in the const records. The code below compiles successfully, but finishes with error.
In my code I need to access data from the cOffsetsA/B arrays having a pointer to the record. I tried to do it like this:
and this causes error - 'Access violation .. read of address 000000..'
Can anybody explain is wrong here and how to fix it, how should it be done?
Probably I will also have to add the _length field in the record, which will hold the size of the array the pointer is pointing to; this is not a problem.
Best regards,LUK
3 Answers
While the use of constants like this are okay in Delphi, I just want to add that it's in general not a very good idea to do things this way. Personally, if this code is in some unit, I would use something like this: (D2007 and higher)
Thus, the record gets a constructor which will fill it with the proper data. As a disadvantage, you can't declare it as a const anymore, if you want to use the constructor to fill it with data. However, you can solve this by using the following code in the Initialization section: 320kbps mp3 songs download.
This code will declare the record as a global variable instead of constant, and fill it with your data. Picture of a rainbow to color.
Your constructor could look like this:
Which will fill the record with your data. However, you don't have to use the constructor to create records this way! It's just an added function.
To make it even more interesting, you can add properties for the two points and array, making the fields themselves private and only declaring read method for the properties. This way, you can still make sure it's content is read-only and stays read-only.
But what's the risk of using your code? Well, since you declare them all as constants, there isn't much risk, unless you allow assignable typed constants. ({$J+} is declared..) In that case, a piece of code could change a value in cOffsetsA and it would also change in cRec1. Is that what you want?
Anyway, by using a constructor and by initializing them in code, you make your code more dynamic. However, it still continues to stay a simple record type and the additional properties and methods won't change it's structure or size.
You will need Delphi 2007 or better for this, though..
It looks like I found the answer myself:
is a dynamic array type and setLength must be used with any variable of this type to allocate the memory. What I need is a pointer to an existing constant array, so the only thing which must be changed in order to fix the problem is the declaration of this type. It should look like:
and yes, I have to add the _length field in the records as low(pMyRec^.aOffsets^), high(..) and length(..) do not work.
Best regards,LUK