分割字符串时出现抽象错误 [英] Abstract error when splitting a string
问题描述
使用此过程时,我收到一个抽象错误(仅此而已)。我在其他项目中使用了此程序,这是我第一次遇到该程序。我不确定是否与输入字符串的语法有关。
when using this procedure, i am getting an abstract error (that's all it says). i use this procedure in other projects, this is the first time i've run into it. i'm not sure if it has to do with the syntax of the input string.
procedure SplitString(const Delimiter: Char; Input: string; const Strings: TStrings);
begin
//Delimits or splits the received string, returns TStrings array
Assert(Assigned(Strings)) ;
Strings.Clear;
Strings.Delimiter := Delimiter;
Strings.StrictDelimiter := True; //needed otherwise whitespace is used to delimit
Strings.DelimitedText := Input;
end;
应用程序调用如下:
try
LBOMPartLine := TStrings.Create;
SplitString(',','C:\DATA\Parts\PART4.PS.0,10,10',LBOMPartLine);
...
我删除了一些调试代码,这些代码强调了该过程失败,在此之前或之后都没有。我可以不使用逗号作为分隔符吗?
I've removed some of the debug code that highlighted the fact that the procedure fails, nothing after or before. Can I not use a comma as a separator?
推荐答案
问题出在这一行:
LBOMPartLine := TStrings.Create;
TStrings
是一个抽象类,您无法创建它的实例。您必须创建一个后代实例,例如 TStringList
。 文档明确指出(强调添加):
TStrings
is an abstract class, and you can't create an instance of it. You have to create an instance of a descendant, such as TStringList
instead. The documentation clearly says (emphasis added):
从TStrings派生一个类来存储和操作字符串列表。 TStrings包含抽象或C ++术语中的纯虚拟方法,不应直接实例化。
您通常使用 TStrings
作为函数或过程接收的参数的类型,以便您可以接受任何 TStrings
后代例如 TStringList
, TComboBox.Items'或
TMemo.Lines`。
You typically use TStrings
as the type of a parameter that a function or procedure receives, so that you can accept any TStrings
descendant such as a TStringList
, TComboBox.Items', or
TMemo.Lines`.
解决方案是创建后代的实例:
The solution is to create an instance of a descendant:
var
LBOMPartLine: TStrings; // or more clearly, TStringList
begin
LBOMPartLine := TStringList.Create;
try
SplitString(',','C:\DATA\Parts\PART4.PS.0,10,10',LBOMPartLine);
// Do whatever else
finally
LBOMPartLine.Free;
end;
end;
这篇关于分割字符串时出现抽象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!