我有一个函数,我存储一些键值对,当我迭代它们时,我得到两次错误:[dcc32错误] App.pas(137):E2149类没有默认属性.这是我的代码的一部分:
function BuildString: string;
var
i: Integer;
requestContent: TDictionary<string, string>;
request: TStringBuilder;
begin
requestContent := TDictionary<string, string>.Create();
try
// add some key-value pairs
request := TStringBuilder.Create;
try
for i := 0 to requestContent.Count - 1 do
begin
// here I get the errors
request.Append(requestContent.Keys[i] + '=' +
TIdURI.URLEncode(requestContent.Values[i]) + '&');
end;
Result := request.ToString;
Result := Result.Substring(0, Result.Length - 1); //remove the last '&'
finally
request.Free;
end;
finally
requestContent.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)
我需要收集字典中每个项目的信息.我该如何解决?
我正在尝试启动一个我可以随时暂停/恢复的线程.以下是我创建线程的方法:
use strict;
use warnings;
use threads;
use threads::shared;
use Thread::Suspend;
use Thread::Semaphore;
sub createThread {
my $semaphore = Thread::Semaphore->new();
my $thr = threads->create(sub {
local $SIG{KILL} = sub {
die "Thread killed\n";
};
local $SIG{STOP} = sub {
print "sig stop\n";
$semaphore->down();
};
local $SIG{CONT} = sub {
$semaphore->up();
print "sig cont\n";
};
my $test = sub {
while (1) {
$semaphore->down();
print STDERR "Working process\n";
sleep(2);
$semaphore->up();
}
};
return $test->();
});
return $thr->tid();
}
Run Code Online (Sandbox Code Playgroud)
检索线程ID(带return $thr->tid(); …
在我的 HTTP 服务器上,我试图允许用户通过这种形式上传图片:
<form action="/?command=saveImage" method="post" enctype="multipart/form-data">
<input type="file" name="images" multiple="multiple"/>
<input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
我正在使用此源上传图像:http : //embarcadero.newsgroups.archived.at/public.delphi.internet.winsock/201107/1107276163.html并更改了 ProcessMimePart 程序以便以正确的格式保存图像:
procedure ProcessMimePart(var aDecoder: TIdMessageDecoder;
var aMsgEnd: Boolean);
var
LMStream: TMemoryStream;
LNewDecoder: TIdMessageDecoder;
fileName, fileExtension: string;
begin
fileName := aDecoder.fileName;
fileExtension := GetFileExtension(fileName);
if (fileExtension <> 'jpg') and (fileExtension <> 'png')
and (fileExtension <> 'bmp') then
begin
Exit;
end;
LMStream := TMemoryStream.Create;
try
LNewDecoder := aDecoder.ReadBody(LMStream, aMsgEnd);
try
LMStream.Position := 0;
TSaveImageController.WriteImage(fileName, fileExtension, LMStream);
except
LNewDecoder.Free;
raise;
end;
aDecoder.Free;
aDecoder …
Run Code Online (Sandbox Code Playgroud) 我有以下几点class
:
TTest = class
private
FId: Integer;
FSecField: Integer;
FThirdField: Integer;
public
constructor Create(AId, ASecField, AThirdField: Integer);
// .....
end;
Run Code Online (Sandbox Code Playgroud)
然后我创建一个TObjectDictionary
这样的:
procedure TMainForm.btnTestClick(Sender: TObject);
var
TestDict: TObjectDictionary<TTest, string>;
Instance: TTest;
begin
TestDict := TObjectDictionary<TTest, string>.Create([doOwnsKeys]);
try
TestDict.Add(TTest.Create(1, 1, 1), '');
if TestDict.ContainsKey(TTest.Create(1, 1, 1)) then
ShowMessage('Match found')
else
ShowMessage('Match not found');
Instance := TTest.Create(1, 1, 1);
TestDict.Add(Instance, 'str');
if TestDict.ContainsKey(Instance) then
ShowMessage('Match found')
else
ShowMessage('Match not found');
finally
TestDict.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)
结果是:“找不到匹配项”、“找到匹配项”。我应该怎么做才能比较每个键的字段值而不是它们的地址?
我正在使用 Delphi XE5 制作一个小的 Delphi 程序。在我的代码中有一个动态布尔数组,我无法更改某些数组元素的值。我尝试在设置长度后初始化数组,但没有帮助。这是代码的一部分:
procedure DoSomething(names: array of string);
var startWithA: array of Boolean;
i: integer;
begin
SetLength(startWithA, Length(names)); // each element is false by default
for i := 0 to Length(names) - 1 do begin
if (names[i].indexOf('A') = 0) then begin
startWithA[i] := true; // the value is not changed after executing this line
end;
end;
end;
Run Code Online (Sandbox Code Playgroud) 我有以下问题:当我点击网站上的按钮(http://domain-location.com
)时,我收到回复给我的信息,并且网址已更改为http://domain-location.com?key=value
.我想创建一个示例http客户端来轻松接收信息.这是我的代码:
function DoRequest(aVal: string): string;
const DOMAIN = 'http://domain-location.com';
var
request: TIdHTTP;
responseStream: TMemoryStream;
responseLoader: TStringList;
urlRequest: string;
begin
request := TIdHTTP.Create(nil);
responseStream := TMemoryStream.Create;
responseLoader := TStringList.Create;
try
try
// accept ranges didn't help
// request.Response.AcceptRanges := 'text/json';
// request.Response.AcceptRanges := 'text/xml';
urlRequest := DOMAIN + '?key=' + aVal;
request.Get(urlRequest, responseStream);
responseStream.Position := 0;
responseLoader.LoadFromStream(responseStream);
Result := responseLoader.Text;
except on E: Exception do
Result := e.Message;
end;
finally
responseLoader.Free;
responseStream.Free;
request.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)
编辑在第一个答案后我编辑了我的功能(仍然无法正常工作): …
我实现了以下类:
type
TUtilProcedure = procedure(var AJsonValue: TJSONObject);
TCallback = class
private
FName: string;
FProcedure: TUtilProcedure;
FAnnotation: string;
public
constructor Create(AName: string; AProcedure: TUtilProcedure; AAnnotation: string); overload;
constructor Create(ACallback: TCallback); overload;
property Name: string read FName;
property Proc: TUtilProcedure read FProcedure;
property Annotation: string read FAnnotation;
end;
Run Code Online (Sandbox Code Playgroud)
然后我有一个全局变量:
procedures: TDictionary<string, TCallback>;
Run Code Online (Sandbox Code Playgroud)
在OnFormActivate
程序I初始化procedures
变量:
procedures := TDictionary<string, TCallback>.Create();
procedures.Add('something', TCallback.Create('sth', @proc, 'annotation'));
// ....
Run Code Online (Sandbox Code Playgroud)
然后在OnFormClose
我释放它:
procedures.Clear;
procedures.Free;
Run Code Online (Sandbox Code Playgroud)
我的代码是否泄漏内存?如果是这样,什么是正确的释放方式dictionary
?据我所知,迭代不是好主意.
我正在尝试实现一个返回包含类元素的json对象的函数.这是我的功能:
procedure ListToJson(AInputList: TList<TRating>;
AResponse: TJSONObject);
var
i: Integer;
jsonPair: TJSONPair;
jsonObject: TJSONObject;
jsonArray: TJSONArray;
begin
jsonArray := TJSONArray.Create();
jsonObject := TJSONObject.Create();
jsonPair := TJSONPair.Create('ratings', jsonArray);
for i := 0 to AInputList.Count - 1 do
begin
jsonObject.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
jsonObject.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
jsonObject.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
jsonObject.AddPair(TJSONPair.Create('description', AInputList[i].Description));
jsonObject.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));
jsonArray.AddElement(jsonObject);
end;
AResponse.AddPair(jsonPair);
end;
Run Code Online (Sandbox Code Playgroud)
当我用包含两个元素的列表测试它时,返回的字符串是:
{
"ratings":[{
"idrating":"1",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:03",
"idrating":"2",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:24"
},{
"idrating":"1",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:03",
"idrating":"2",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:24" …
Run Code Online (Sandbox Code Playgroud) 我有以下两个类:
type
TItemProp = class
private
FItemPropName: string;
FItemPropValue: string;
public
constructor Create(AItemPropName, AItemPropValue: string);
class function GetItemProp(AHTMLElement: OleVariant; AProp: string): TItemProp;
property ItemPropName: string read FItemPropName;
property ItemPropValue: string read FItemPropValue;
end;
TTest = class
private
FName: string;
FProps: TList<TItemProp>;
FTest: TList<TTest>;
public
constructor Create(AName: string);
destructor Destoroy();
property Name: string read FName;
property ItemProps: TList<TItemProp> read FProps write FProps;
property Test: TList<TTest> read FTest write FTest;
end;
Run Code Online (Sandbox Code Playgroud)
这里是构造函数的代码和TTest类的析构函数:
constructor TTest.Create(AName: string);
begin
Self.FName := AName;
Self.FProps := TList<TItemProp>.Create(); …
Run Code Online (Sandbox Code Playgroud) 我试图从给定的目录中获取所有文件和目录,但我不能指定什么是类型(文件/目录).什么都没打印出来.我做错了什么以及如何解决它.这是代码:
sub DoSearch {
my $currNode = shift;
my $currentDir = opendir (my $dirHandler, $currNode->rootDirectory) or die $!;
while (my $node = readdir($dirHandler)) {
if ($node eq '.' or $node eq '..') {
next;
}
print "File: " . $node . "\n" if -f $node;
print "Directory " . $node . "\n" if -d $node;
}
closedir($dirHandler);
}
Run Code Online (Sandbox Code Playgroud) delphi ×8
dictionary ×2
memory-leaks ×2
perl ×2
tdictionary ×2
arrays ×1
destructor ×1
file ×1
file-upload ×1
generics ×1
httpclient ×1
httpserver ×1
indy ×1
indy10 ×1
iteration ×1
json ×1
readdir ×1
subdirectory ×1