Solved

Records & Longstrings

Posted on 1997-11-26
3
149 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

11 Experts available now in Live!

Get 1:1 Help Now