Solved

Records & Longstrings

Posted on 1997-11-26
3
157 Views
Last Modified: 2010-04-04
It is my understanding that items in a record can only hold up to 255 characters. I need to be able to store information in a record style structure, but I need longstring support. Is there are method to achieve this, either using records or some other structure?
0
Comment
Question by:JohnE
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
anilms earned 150 total points
ID: 1352030
Yes, you cannot have more than 255 characters in strings in records. We had a situation wherein the average number of characters to be stored was around 850. Using "String" instead of "String[900]" was not a proper solution. This is how we went about it :

1. New type definition for creating a string upto 1k :
  type
      kstring=record
        kslen:integer;
        ksdata:array[1..1024] of char
      end

2. Procedure to store any string in the kstring
   procedure putks(instring:string;var outks:kstring);
   begin
        outks.kslen := length(instring);
        move(instring[1],outks.ksdata,length(instring));
   end;

3. Function to assign a kstring to any string
   function getks(inks:kstring):string;
   var tmpstr:string;
   begin
        move(inks.ksdata,tmpstr[1],inks.kslen);
        SetLength(tmpstr,inks.kslen);
        getks := tmpstr;
   end;

4. You can now use this kstring anywhere in your records. Example :
  Type
   Emp_Rec = record
    Emp_Id:Cardinal;
    Emp_Name:string[40];
    Emp_Notes:KString
   end;

5. Accessing is also quite simple - examples
   Edit1.Text := getks(Emp_Name)
   PutKs(Emp_Name, Edit4.Text)  
0
 

Author Comment

by:JohnE
ID: 1352031
Thanks for your answer. I get the general idea of what is going on here, but am having trouble implementing the answer (storing string in kstring and then assigning kstring to a string). I get errors regarding the incompatibility between string and kstring or that acutal and formal parameters need to be identical.  Could you please give me a simple example of how to implement this. (I am very new to Delphi)?
0
 

Author Comment

by:JohnE
ID: 1352032
I have been able to implement this with error free code, but when I run ( a sample program see below), I always get a EAccessViolation error. It seems to be coming from the getks function?

Type
kstring = record
kslen: integer;
ksdata:array[1..1024] of char
end;

procedure putks(instring:string;var outks:kstring);
begin
outks.kslen := length(instring);
move(instring[1],outks.ksdata,length(instring));
end;

function getks(inks:kstring):string;
var
tmpstr:string;
begin
move(inks.ksdata,tmpstr[1],inks.kslen);
SetLength(tmpstr,inks.kslen);
getks := tmpstr;
end;

Type
Emp_Rec = record
Emp_Id:string[10];
Emp_Name:string[40];
Emp_Notes:kstring;
end;

var
Employee: Emp_Rec;

procedure TForm2.Button1Click(Sender: TObject);
begin
Employee.Emp_Id := Edit1.Text;
Putks(Edit1.Text, Employee.Emp_Notes);
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
Memo1.Text := getks(Employee.Emp_Notes);
end;

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

813 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

8 Experts available now in Live!

Get 1:1 Help Now