String and String[30]

Any advantage in using this style in defining a string type variable?

myName: String;
hisName: String[30];
0
Alan
10/28/2009 11:22:58 PM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

11 Replies
1034 Views

Similar Articles

[PageSpeed] 0

"Alan Tse" <alan_NO_SPAM_pltse@yahoo.com.au> wrote in message 
news:177975@forums.codegear.com...
> Any advantage in using this style in defining a string type variable?
>
> myName: String;
> hisName: String[30];


String[30] is a short string type and is ANSI only. You should use simple 
String type (Unicode in D2009+).

-- 
Wayne Niddery (TeamB)
0
Wayne
10/28/2009 11:49:42 PM
Alan Tse wrote:

> Any advantage in using this style in defining a string type variable?
> 
> myName: String;
> hisName: String[30];

String[3o] is a short string. You should avoid them, if you can. They
only contain AnsiChars, and do not mix well with Unicode. They were the
string type in old, 16 bit times.

-- 
Rudy Velthuis (TeamB)        http://www.teamb.com

"If you were plowing a field, which would you rather use? Two 
 strong oxen or 1024 chickens?"
 -- Seymour Cray (1925-1996), father of supercomputing
0
Rudy
10/29/2009 12:06:15 AM
>> Any advantage in using this style in defining a string type variable?
>>
>> myName: String;
>> hisName: String[30];
>
> String[3o] is a short string. You should avoid them, if you can. They
> only contain AnsiChars, and do not mix well with Unicode. They were the
> string type in old, 16 bit times.

My colleague developed an application 7 years ago use this sort of 
string[5], string[10], ...etc quite often in the record structure.
May be he wanted to simulate the same structure in the database.
eg
Order number in database is char(10), he used String[10] in the structure.
0
Alan
10/29/2009 1:52:32 AM
Alan Tse wrote:

> > > Any advantage in using this style in defining a string type
> > > variable?
> > > 
> > > myName: String;
> > > hisName: String[30];
> > 
> > String[3o] is a short string. You should avoid them, if you can.
> > They only contain AnsiChars, and do not mix well with Unicode. They
> > were the string type in old, 16 bit times.
> 
> My colleague developed an application 7 years ago use this sort of
> string[5], string[10], ...etc quite often in the record structure.
> May be he wanted to simulate the same structure in the database.

Yes, that could be. A string[10] is as static array of 11 AnsiChars,
but the first AnsiChar (index 0) is in fact a byte containing the
length.



-- 
Rudy Velthuis (TeamB)        http://www.teamb.com

"We are condemned to kill time: thus we die bit by bit."
 -- Octavio Paz
0
Rudy
10/29/2009 2:07:44 AM
On Wed, 28 Oct 2009 18:52:32 -0700, Alan Tse
<alan_NO_SPAM_pltse@yahoo.com.au> wrote:

>>> Any advantage in using this style in defining a string type variable?
>>>
>>> myName: String;
>>> hisName: String[30];
>>
>> String[3o] is a short string. You should avoid them, if you can. They
>> only contain AnsiChars, and do not mix well with Unicode. They were the
>> string type in old, 16 bit times.
>
>My colleague developed an application 7 years ago use this sort of 
>string[5], string[10], ...etc quite often in the record structure.
>May be he wanted to simulate the same structure in the database.
>eg
>Order number in database is char(10), he used String[10] in the structure.

A record containing a string type can't be written to disk as is, you
have to write the fields separately and you can't count on the length
of the result.

However, a record containing a string[10] can be written perfectly
well.

Thus the use of string[##] fields were common in stuff that was
written to disk in the pre-Unicode era.
0
Loren
10/29/2009 4:51:50 AM
> A record containing a string type can't be written to disk as is, you
> have to write the fields separately and you can't count on the length
> of the result.
>
> However, a record containing a string[10] can be written perfectly
> well.
>
> Thus the use of string[##] fields were common in stuff that was
> written to disk in the pre-Unicode era.

His structure is something like:

TEmployee = record
  EmployeeName: String[30];
  DOB: String[8];
  Time: String[5];
  Age: Integer;
  .....
end;

Yes, he did  write to disk field by field, not the way to write the record 
in a single string.
0
Alan
10/29/2009 5:53:51 AM
"Alan Tse" <alan_NO_SPAM_pltse@yahoo.com.au> wrote in message 
news:178060@forums.codegear.com...
>> A record containing a string type can't be written to disk as is, you
>> have to write the fields separately and you can't count on the length
>> of the result.
>>
>> However, a record containing a string[10] can be written perfectly
>> well.
>>
>> Thus the use of string[##] fields were common in stuff that was
>> written to disk in the pre-Unicode era.
>
> His structure is something like:
>
> TEmployee = record
>  EmployeeName: String[30];
>  DOB: String[8];
>  Time: String[5];
>  Age: Integer;
>  .....
> end;
>
> Yes, he did  write to disk field by field, not the way to write the record 
> in a single string.

There is no reason to write field by field to the disk, in fact it takes 
some rather difficult code.

The way you do it is to have a var Employee: TEmployee; and fill each field 
in Employee.

Then you write Employee to disk. (I bet that is the way he did it!)

regards Sven
0
Sven
10/29/2009 8:31:43 AM
>> His structure is something like:
>>
>> TEmployee = record
>>  EmployeeName: String[30];
>>  DOB: String[8];
>>  Time: String[5];
>>  Age: Integer;
>>  .....
>> end;
>>
>> Yes, he did  write to disk field by field, not the way to write the 
>> record
>> in a single string.
>
> There is no reason to write field by field to the disk, in fact it takes
> some rather difficult code.
>
> The way you do it is to have a var Employee: TEmployee; and fill each 
> field
> in Employee.
>
> Then you write Employee to disk. (I bet that is the way he did it!)

This is the structure that he used to store the details of an employee from 
the dataset record, something like:

Emp.EmployeeName := dataSetEmployee.FieldByName('EmployeeName').AsString;
......

and vice versa.
0
Alan
10/29/2009 10:14:03 PM
"Alan Tse" <alan_NO_SPAM_pltse@yahoo.com.au> wrote in message 
news:178151@forums.codegear.com...
>
> This is the structure that he used to store the details of an employee 
> from the dataset record, something like:
>
> Emp.EmployeeName := dataSetEmployee.FieldByName('EmployeeName').AsString;


Then there is no need to use short strings here. The AsString property is 
not a shortstring either, but returns/accepts a normal string type. Thus, in 
fact, a conversion is taking place each time an assignment occurs.

For your record definitions you should be able to simply change everything 
to string, without the lengths, and it should still work.

You are still responsible for making sure a string entered via the UI does 
not exceed the length of a field, but you can get that information from the 
dataset.

-- 
Wayne Niddery (TeamB)
0
Wayne
10/29/2009 11:45:43 PM
On Wed, 28 Oct 2009 22:53:51 -0700, Alan Tse
<alan_NO_SPAM_pltse@yahoo.com.au> wrote:

>> A record containing a string type can't be written to disk as is, you
>> have to write the fields separately and you can't count on the length
>> of the result.
>>
>> However, a record containing a string[10] can be written perfectly
>> well.
>>
>> Thus the use of string[##] fields were common in stuff that was
>> written to disk in the pre-Unicode era.
>
>His structure is something like:
>
>TEmployee = record
>  EmployeeName: String[30];
>  DOB: String[8];
>  Time: String[5];
>  Age: Integer;
>  .....
>end;
>
>Yes, he did  write to disk field by field, not the way to write the record 
>in a single string.

Ancient code or something?
0
Loren
10/31/2009 3:36:07 PM
> {quote:title=Alan Tse wrote:}{quote}
> Any advantage in using this style in defining a string type variable?
> 
> myName: String;
> hisName: String[30];

In PASCAL you only have string[]
and the size is in hisName[0] si like a char  is limited to 255(eigth bit byte=
If you need more the PASCAL doesn't make it . You have to do it youself

In the code it s different
When the compiler find  myName: String; it's create just a pointer  (perhaps more for the size)
when you use tjhe string the application call the memory man,agher tto allocate the size
SetLength(myName,X)
You dont know(!!!!) where is the size. You need tocall GetLength

If ou use  ifname: String[30]; the compiler take 31 (30+1) byte on YOUR global memory
Thé string is pre allocate and the size is in  isName[0] and you can use it

Sample

 myName: String;
 hisName: String[30];

myName:='Pascal Euler'
-the compiler compute the length :12
Look if the string exitCall the memory manager (12  bytes)
Store the size in the header
Move the 12 char

hisName:='Pascal Euler'
The application compute the size 12
move the memory  and put the length in hisName[0]

Using get the lenght

1- look is the string exit
2-getb tyhe adrress
3 go back 4
4 read the value 

L:= hisName[0]
1- read the memory always the same address


Delete 
myName:='Pascal Euler'
isName:='Pascal Euler'
SetLengtht(see the doc) allocate a new string and move the memory


In fact , like it had been said, use the string if 
you use string greater than 255 char and 
use string[] you use ASCI not UniCode


To resume:

> myName: String;             UNICODE
> hisName: String[30];       ASCII Faster

If make mistakes (surrely) please correct me!

Jerôme
0
Jerome
11/3/2009 7:45:16 AM
Reply: