我有两个问题:
是否需要使用Interlocked类来访问布尔值?默认情况下,是否读取或写入布尔值atomic?
我尝试在布尔上使用Interlocked.CompareExchange并得到以下错误:
bool value = true;
Interlocked.CompareExchange<bool>(ref value, false, true);
Run Code Online (Sandbox Code Playgroud)
错误:类型'bool'必须是引用类型才能在泛型类型或方法'System.Threading.Interlocked.CompareExchange(ref T,T,T)'中将其用作参数'T'
我该如何解决这个问题?
我上课了
type
TLoadOption = class
private
FAutoSearch: Boolean;
public
property AutoSearch: Boolean read FAutoSearch write FAutoSearch;
end;
Run Code Online (Sandbox Code Playgroud)
在其中一个函数中,我在堆栈中创建类的对象
procedure MyView.InitializeForm(const aMsg: MyMsg);
//---------------------------------------------------------------------------
var
Options: TLoadOption;
begin
if aMsg.OptionalObject <> nil then
Options := aMsg.OptionalObject as TLoadOption;
if Assigned(Options) and Options.AutoSearch then
DoRefresh;
end;
Run Code Online (Sandbox Code Playgroud)
我没有在aMsg中传递任何内容,所以理想情况下没有设置选项.
在Delphi XE中默认选项设置为nil,因此不会调用此DoRefresh但是当我在Delpi XE4中执行相同的代码时,选项会使用一些随机值进行初始化,并且AutoSearch始终变为true,这会导致调用此DoRefresh函数,这是不需要.
我想知道是否有任何编译器选项将默认值设置为未初始化的变量.我现在唯一的解决方案就是这样
procedure MyView.InitializeForm(const aMsg: MyMsg);
//---------------------------------------------------------------------------
var
Options: TLoadOption;
begin
Options := nil;
if aMsg.OptionalObject <> nil then
Options := aMsg.OptionalObject as TLoadOption;
if Assigned(Options) and Options.AutoSearch then
DoRefresh;
end;
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?
我想在任何提供的范围内只创建一个随机素数.我有这个代码:
print [x for x in range(5000, 5200)
if not [t for t in range(2, x)
if not x % t]]
Run Code Online (Sandbox Code Playgroud)
但我的程序提供了一组素数.我想修改我的代码并在任何提供的范围内只创建一个随机数,但我找不到如何做.请帮忙.
我正在使用Lazarus,因为我非常喜欢这些TChart组件.我已经制定了一个解决一些不平等问题的程序,例如:

(Delta =判别式)2x ^ 2 + 6x + 4> 0的解是x<-2和x>-1.这是正确的,但正如你所看到的,我在TPicture下面有一个组件,显示带有图表的图片.我用Gimp做到了.
我想使用一些适当的组件制作图表.正如我已经说过的那样,我在Lazarus上找到了它们,但是你知道Delphi XE5上是否有任何方法呢?我在哪里可以看一下?
我正在尝试在泛型中实现TCollection和TCollectionItem功能.要解决这个问题,我们需要做一个TGenericCollectionItem或者前面的声明TGenericCollection.实际(XE6)Delphi编译器不支持使用泛型类型的前向声明.有一个黑客如何制作它.但是我们仍然有一个分配所有者的问题TCollectionItem.
TBaseElement = class // hack for forward declaration of a generic type
end;
TGenericCollection<T: TBaseElement> = class(TObjectList<TBaseElement>)
protected
procedure Notify(const Value: TBaseElement; Action: TCollectionNotification); override;
end;
TGenericCollectionItem = class(TBaseElement)
public
Owner: TGenericCollection<TBaseElement>;
end;
procedure TGenericCollection<T>.Notify(
const Value: TBaseElement; Action: TCollectionNotification);
begin
inherited;
if (Action = cnAdded) then
begin
if (Value is TGenericCollectionItem) then
(Value as TGenericCollectionItem).Owner := Self; //here is error
end;
end;
Run Code Online (Sandbox Code Playgroud)
E2010不兼容的类型:
'TGenericCollection<TBaseElement>'和'TGenericCollection<TGenericCollection<T>.T>' …
delphi ×3
c# ×2
collections ×1
delphi-xe ×1
delphi-xe4 ×1
delphi-xe6 ×1
generics ×1
lazarus ×1
mvvm ×1
python ×1
winforms ×1