小弟第一次写自定义函数各位高手给提点意见
{================本函数由朱立勋编写-2002.11==================
}
unit myunit;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ImgList, ComCtrls, ShellCtrls,
  te_controls,Registry;
{===========注册表读写函数====}
Function RWReg(RegRW:String;RootKey:String;RegPath:string;RegKey:String;RegValues:string):string;
//(读写:string根键:string,路径:string,键名:string,键值:string)implementation
{===========注册表读写函数====}Function RWReg(RegRW:String;RootKey:String;RegPath:string;RegKey:String;RegValues:string):string;
//(读写:string根键:string,路径:string,键名:string,键值:string)
var
  RegW:TRegistry;
begin
  RegW:=TRegistry.Create;
  if RootKey='HKEY_CLASSES_ROOT' then
    RegW.RootKey:=HKEY_CLASSES_ROOT;
  if RootKey='HKEY_CURRENT_USER' then
    RegW.RootKey:=HKEY_CURRENT_USER;
  if RootKey='HKEY_LOCAL_MACHINE' then
    RegW.RootKey:=HKEY_LOCAL_MACHINE;
  if RootKey='HKEY_USERS' then
    RegW.RootKey:=HKEY_USERS;
  if RootKey='HKEY_CURRENT_CONFIG' then
    RegW.RootKey:=HKEY_CURRENT_CONFIG;
  if RootKey='HKEY_DYN_DATA' then
    RegW.RootKey:=HKEY_DYN_DATA;
  RegW.OpenKey(RegPath,TRUE);
  
  RegW:=TRegistry.Create;
  RegW.OpenKey(RegPath,TRUE);
  if (RegRW='Read') OR (RegRW='R') then
  begin
    Result:=RegW.ReadString(RegKey);
  end;
  if (RegRW='Write') OR(RegRW='W') then
  begin
    RegW.WriteString(RegKey,RegValues);
    Result:='OK';
  end;
  if (RegRW='Delete') OR (RegRW='D') then
  begin
    Result:='OK';
  end;
  RegW.Free;
end;
end.

解决方案 »

  1.   

    怎么这么多的条件?用一个变量case不更简单?/或者用代码表示变量条件!!写得挺有条理的我也是来鸟,共同学习!!
      

  2.   

    要用Case来做字符串的分支,就必须要做一个转换,把字符串变成有序的数据结构。还有就是有些公共部分可以提取出来,程序还可以进一步的优化,继续努力吧:-)
      

  3.   

    真不知道该赞扬什么?~~反正大家一起努力吧~~ :)
    伴水一向都特别喜欢自己编写函数~~
    有兴趣你可以到 http://zswang.51.net/function/index.htm 逛逛~~提供几点参考意见
    No.1 尽量使用标准函数
    No.2 尽量接近系统的处理方法
    No.3 尽量使用同一种处理方法
    No.4 尽量函数不要写太多的语句
    No.5 尽量使函数目的明确
    No.6 尽量保持一致的风格
    ... ...所以,编写自定义函数最好向标准函数学习~~RegW:=TRegistry.Create这条语句执行了两次~~
    就是说,每调用你的这一个函数一次就会浪费一次TRegistry资源~~参考、对比如下代码 :) ~~{
    ================本函数由朱立勋编写-2002.11==================
    ================本函数由zswang修改-2002.11==================
    }
    unit RegFunction; //#编写命名时避免加My这样的前缀interfaceuses
      Windows, SysUtils, Registry; //#尽量减少包含无用的单元{===========注册表读写函数====}function ReadWriteRegistry(mRegRW: string; mRootKey: string; //#函数名最好写完整一点//不要怕命名长~~
      mRegPath: string; mRegKey: string; mRegValues: string): string; //#命名时要避免以系统命名冲突//如:RootKey
    //(读写:string根键:string,路径:string,键名:string,键值:string)implementation{===========注册表读写函数====}
    function ReadWriteRegistry(mRegRW: string; mRootKey: string;
      mRegPath: string; mRegKey: string; mRegValues: string) :string;
    //(读写:string根键:string,路径:string,键名:string,键值:string)
    begin
      Result := ''; //#确保能返回一个确定的值
      with TRegistry.Create do try
        ///////Begin 处理RootKey
        if mRootKey = 'HKEY_CLASSES_ROOT' then //#避免作无效的判断,多使用else
          RootKey := HKEY_CLASSES_ROOT
        else if mRootKey = 'HKEY_CURRENT_USER' then
          RootKey := HKEY_CURRENT_USER
        else if mRootKey = 'HKEY_LOCAL_MACHINE' then
          RootKey := HKEY_LOCAL_MACHINE
        else if mRootKey = 'HKEY_USERS' then
          RootKey := HKEY_USERS
        else if mRootKey = 'HKEY_CURRENT_CONFIG' then
          RootKey := HKEY_CURRENT_CONFIG
        else if mRootKey = 'HKEY_DYN_DATA' then
          RootKey := HKEY_DYN_DATA;
        ///////End 处理RootKey    OpenKey(mRegPath, True);    if (mRegRW = 'Read') or (mRegRW = 'R') then
          Result := ReadString(mRegKey)
        else if (mRegRW = 'Write') or (mRegRW = 'W') then begin
          WriteString(mRegKey, mRegValues);
          Result := 'OK';
        end else if (mRegRW='Delete') or (mRegRW = 'D') then begin
          { TODO -oZswang -c2002.11.16 : 删除的语句没有写哦 }
          Result := 'OK';
        end;
      finally //#手工创建资源时建议用try..finally..end这样的语句来释放资源
        Free;
      end;
    end;end.
    嘻嘻嘻 伴水 :) 到此一灌 嘻嘻嘻嘻
      

  4.   

    我正好有这样的一个函数,不知道怎么样?
    function ReadRegInteger(HKey: Cardinal; const sKey: String; DefaultValue: Integer): Integer;
    var
      Reg: TRegistry;
    begin
      Result := DefaultValue;
      Reg := TRegistry.create;
      try
        Reg.RootKey := HKey;
        if Reg.OpenKey(sKey, false) then
        begin
          if Reg.ValueExists(sKey) then
            Result := Reg.ReadInteger(sKey)
        end;
      finally
        Reg.closekey;
        Reg.free;
      end;
    end;
    调用时象这样:
    aaavalue:=ReadRegInteger(HKEY_CURRENT_USER, 'aaa', 'avalue', -1);
    如果读aaa的avalue的值失败,则aaavalue也得到返回值-1
    HKEY_CURRENT_USER是delphi提供的常数呀,为什么要用字符串来判断呢?