我听过很多程序员,特别是Delphi程序员嘲笑使用'with'.
我认为它使程序运行得更快(只有一个对父对象的引用),并且如果使用得当,它更容易阅读代码(少于十几行代码并且没有嵌套).
这是一个例子:
procedure TBitmap32.FillRectS(const ARect: TRect; Value: TColor32);
begin
with ARect do FillRectS(Left, Top, Right, Bottom, Value);
end;
Run Code Online (Sandbox Code Playgroud)
我喜欢用with.我怎么了?
我在设计课程时遇到了一些麻烦.创建客户类看起来很简单:
TCustomer = Class
private
FIdNumber: String;
FName: String;
procedure SetName(const Value: String);
procedure SetNumber(const Value: String);
public
Property Name : String read FName;
Property IdNumber : String read FIdNumber;
Constructor Create(Number, Name : String);
end;
constructor TCustomer.Create(ANumber, AName: String);
begin
SetName(AName);
SetNumber(ANumber);
end;
MyCustomer := TCustomer.Create('1', 'John Doe');
Run Code Online (Sandbox Code Playgroud)
但我的客户有更多的财产:他住的地方,出生日期等.
TCustomer = Class
private
{..snip snip..}
public
Property Name : String read FName;
Property IdNumber : String read FIdNumber;
Property Street : String read FStreet;
Property HouseNumber : Integer : …Run Code Online (Sandbox Code Playgroud) 有没有办法引用使用"with"语句创建的对象实例?
例:
with TAnObject.Create do
begin
DoSomething(instance);
end;
Run Code Online (Sandbox Code Playgroud)
DoSomething将使用实例引用,就像您将实例从变量声明引用传递给创建的对象一样.
例:
AnObject := TAnObject.Create;
Run Code Online (Sandbox Code Playgroud)
谢谢.
我正在尝试正确使用delphi中的语句.
总的来说,做简单的事情似乎相当简单,但我有兴趣找到一些关于使用嵌套语句的好的代码示例和/或教程.例如
with object1, object2, etc... do
begin
statements
end;
Run Code Online (Sandbox Code Playgroud)
我不确定的是以这种方式使用语句时的优先顺序.
任何建议表示赞赏.
有人知道Delphi 2009处理"with"有什么不同吗?
我昨天解决了一个问题,只需将"with"解构为完整引用,如"with Datamodule,Dataset,MainForm".Delphi 2006及更早版本对数据集应用了"Close".Delphi 2009对MainForm应用了"Close"并退出了应用程序!
如你所知,在c#中初始化对象非常方便快捷
StudentName student2 = new StudentName
{
FirstName = "Craig",
LastName = "Playstead",
};
Run Code Online (Sandbox Code Playgroud)
和
List<MyObject>.Add(new MyObject{a=1,b=2})
Run Code Online (Sandbox Code Playgroud)
可以像这样在Delphi中初始化对象吗?
考虑这个随机对象:
Object Test of Class TestClass:
String Name;
Integer Age;
procedure setName(n);
function getName(): String;
Run Code Online (Sandbox Code Playgroud)
在Delphi中,如果我们想要使用对象的许多属性和方法轻松工作,我们可以这样做:
Test.Name = 'EASI';
Test.Age = 34;
Test.setName('Eduardo Alcantara');
ShowMessage(Test.getName);
Run Code Online (Sandbox Code Playgroud)
......或者我们可以这样做:
with Test do
begin
Name = 'EASI';
Age = 32;
setName('Eduardo Alcântara');
ShowMessage(getName);
end
Run Code Online (Sandbox Code Playgroud)
在Java中是否有类似的结构,我们可以在Delphi中缩短语法?
我正在以编程方式创建一个tChart(Delphi2007,TeeChar 7免费版).我想设置图表尺寸并可能更改宽高比,但我没有得到有意义的结果更改宽度和高度属性.我也尝试过改变轴TickLength而没有运气.我从dfm文件中复制了TChart的相关属性,而不是忘记任何有意义的东西.仅当我编辑X和Y max和min值时,图形的方面才会改变,但这还不够.
这是我的原始图表和"重新格式化"图表,因为您可以看到两者的图表尺寸均为400 x 250.是否有用于调整图表大小的特定属性?我希望轴相应调整大小,是否可能?谢谢您的帮助

以下是与TChart相关的代码:
procedure CreateChart(parentform: TForm);
//actually formatChart is a CreateChart anf fChart a member of my class
begin
fchart:= TChart.Create(parentform);
fchart.Parent:= parentform;
fchart.AxisVisible := true;
fchart.AutoSize := false;
fChart.color := clWhite;
fchart.BottomAxis.Automatic := true;
fchart.BottomAxis.AutomaticMaximum := true;
fchart.BottomAxis.AutomaticMinimum := true;
fchart.LeftAxis.Automatic := true;
fchart.LeftAxis.AutomaticMaximum := true;
fchart.LeftAxis.AutomaticMinimum := true;
fchart.view3D := false;
end
procedure formatChart(width, height, xmin, xmax, ymin, ymax: double);
//actually formatChart is a method anf fChart a member of my class
begin
with …Run Code Online (Sandbox Code Playgroud) 我怎么画一张照片TPanel?我还想确定X,Y来定义Panel上的确切位置.
我有一些简单的代码问题我根本无法解决.我正在使用Delphi 5并应用了所有更新.
我有这个代码块:
procedure TForm1.LoadBtnClick(Sender: TObject);
var
s1, s2, s3 : Textfile;
sa, sb, RString, SQLString : string;
begin
with sourcequery do begin
Close;
SQL.Clear;
SQL.Add('delete * from source');
ExecSQL;
Close;
AssignFile(S2, 'c:\delphi programs\events\source1.txt');
AssignFile(S1, 'c:\delphi programs\events\source2.txt');
Reset(s2);
Reset(s1);
while not eof(s1) do begin
readln(s1, RString);
SQLString := 'insert into source1(f1, f2) values(';
SQLstring := SQLString+ QuotedStr(copy(Rstring, 1, pos(chr(9), Rstring)-1))+', ';
SQLString := SQLString+QuotedStr(copy(Rstring, pos(chr(9),Rstring)+1,length(Rstring)))+')';
with sourcequery do begin
close;
SQL.Clear;
SQL.Add(SQLString);
ExecSQL;
end;
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译它时,它会在"eof("之后停止:
while not …Run Code Online (Sandbox Code Playgroud) 考虑这个例子:
Memo1.Clear;
Memo1.Add(S);
Memo1.SaveToFile(F);
Run Code Online (Sandbox Code Playgroud)
如果它是在JavaScript中,我们可以这样做:
Memo1.Clear.Add(S).SaveToFile(F);
Run Code Online (Sandbox Code Playgroud)
我怎样才能编写自己的Delphi类来支持这样的链接方法调用?我希望能够编写这样的代码:
MyFileClass.Create('File.txt').OpenForWrite().Add('Test');
Run Code Online (Sandbox Code Playgroud) 利用Indy,我怎么能提取出存在于所有电子邮件地址To,Cc以及Bcc一个领域TIdMessage?由于这些字段可以包含多个地址,我必须解析它们,但我没有找到任何现成的功能(也许我错过了它?).
我创建了一个using()而没有指定对象名.
我的问题是如何访问我的新对象并打印其名称?
class Program
{
static void Main(string[] args)
{
AnimalFactory factory = new AnimalFactory();
using (factory.CreateAnimal())
{
Console.WriteLine("Animal {} created inside a using statement !");
//How can i print the name of my animal ?? something like this.Name ?
}
Console.WriteLine("Is the animal still alive ?");
}
}
public class AnimalFactory
{
public IAnimal CreateAnimal()
{
return new Animal();
}
}
public class Animal : IAnimal
{
public string Name { get; set; }
public Animal()
{
Name = …Run Code Online (Sandbox Code Playgroud) delphi ×12
.net ×1
c# ×1
delphi-2007 ×1
delphi-2009 ×1
delphi-xe3 ×1
delphi-xe4 ×1
draw ×1
image ×1
indy10 ×1
java ×1
panel ×1
teechart ×1
text-files ×1