As the other answers already said the local variable is not initialized.
However what I like to add here is that as
is nil safe. That means you don't need to check if aMsg.OptionalObject
is assigned. You can just write:
var
Options: TLoadOption;
begin
Options := aMsg.OptionalObject as TLoadOption;
if Assigned(Options) and Options.AutoSearch then
DoRefresh;
end;
Keep in mind that it will throw an exception if OptionalObject does not inherit from TLoadOption. The code looks as that is always the case. So if you are sure about that you can use a hardcast:
begin
if Assigned(aMsg.OptionalObject) and TLoadOption(aMsg.OptionalObject).AutoSearch then
DoRefresh;
end;