Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Records & Longstrings

Posted on 1997-11-26
3
Medium Priority
?
163 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 450 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

927 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